Skip to content

Commit 89c5621

Browse files
committed
expand,interp: adapt to Bash 5.0 changes
The order in which associative array keys and values are printed has changed. Before it was sorting by key; now it appears to sort in an arbitrary way, such as via the hash table. 'man bash' still doesn't document what the order is, so let's keep the simple order and have the tests sort manually. '${!a}' now errors if the variable is empty, so do that too. Updates mvdan#346.
1 parent 8fb18ad commit 89c5621

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

expand/param.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ func (cfg *Config) paramExp(pe *syntax.ParamExp) (string, error) {
106106
for k := range vr.Map {
107107
strs = append(strs, k)
108108
}
109-
} else if str != "" {
109+
} else if !syntax.ValidName(str) {
110+
return "", fmt.Errorf("invalid indirect expansion")
111+
} else {
110112
vr = cfg.Env.Get(str)
111113
strs = append(strs, vr.String())
112114
}
@@ -315,15 +317,11 @@ func (cfg *Config) varInd(vr Variable, idx syntax.ArithmExpr) (string, error) {
315317
case Associative:
316318
switch lit := nodeLit(idx); lit {
317319
case "@", "*":
318-
var strs []string
319-
keys := make([]string, 0, len(vr.Map))
320-
for k := range vr.Map {
321-
keys = append(keys, k)
322-
}
323-
sort.Strings(keys)
324-
for _, k := range keys {
325-
strs = append(strs, vr.Map[k])
320+
strs := make([]string, 0, len(vr.Map))
321+
for _, val := range vr.Map {
322+
strs = append(strs, val)
326323
}
324+
sort.Strings(strs)
327325
if lit == "*" {
328326
return cfg.ifsJoin(strs), nil
329327
}

interp/interp.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,10 @@ func (r *Runner) updateExpandOpts() {
116116
}
117117

118118
func (r *Runner) expandErr(err error) {
119-
switch err := err.(type) {
120-
case nil:
121-
case expand.UnsetParameterError:
122-
r.errf("%s\n", err.Message)
119+
if err != nil {
120+
r.errf("%v\n", err)
123121
r.exit = 1
124122
r.setErr(ShellExitStatus(r.exit))
125-
default:
126-
r.setErr(err)
127-
r.exit = 1
128123
}
129124
}
130125

interp/interp_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,12 @@ var fileCases = []struct {
288288
{"a=(a bcd); echo ${#a} ${#a[@]} ${#a[*]} ${#a[1]}", "1 2 2 3\n"},
289289
{"set -- a bc; echo ${#@} ${#*} $#", "2 2 2\n"},
290290
{
291-
"echo ${!a}; a=b; echo ${!a}; b=c; echo ${!a}",
292-
"\n\nc\n",
291+
"echo ${!a}; echo more",
292+
"invalid indirect expansion\nexit status 1 #JUSTERR",
293+
},
294+
{
295+
"a=b; echo ${!a}; b=c; echo ${!a}",
296+
"\nc\n",
293297
},
294298
{
295299
"a=foo; echo ${a:1}; echo ${a: -1}; echo ${a: -10}; echo ${a:5}",
@@ -1834,20 +1838,20 @@ set +o pipefail
18341838
"\n\nb\n\n",
18351839
},
18361840
{
1837-
`declare -A a=([x]=b [y]=c); echo ${a[@]}; echo ${a[*]}`,
1838-
"b c\nb c\n",
1841+
`declare -A a=([x]=b [y]=c); for e in ${a[@]}; do echo $e; done | sort`,
1842+
"b\nc\n",
18391843
},
18401844
{
1841-
`declare -A a=([y]=b [x]=c); echo ${a[@]}; echo ${a[*]}`,
1842-
"c b\nc b\n",
1845+
`declare -A a=([y]=b [x]=c); for e in ${a[*]}; do echo $e; done | sort`,
1846+
"b\nc\n",
18431847
},
18441848
{
1845-
`declare -A a=([x]=a); a["y"]=d; a["x"]=c; echo ${a[@]}`,
1846-
"c d\n",
1849+
`declare -A a=([x]=a); a["y"]=d; a["x"]=c; for e in ${a[@]}; do echo $e; done | sort`,
1850+
"c\nd\n",
18471851
},
18481852
{
1849-
`declare -A a=([x]=a); a[y]=d; a[x]=c; echo ${a[@]}`,
1850-
"c d\n",
1853+
`declare -A a=([x]=a); a[y]=d; a[x]=c; for e in ${a[@]}; do echo $e; done | sort`,
1854+
"c\nd\n",
18511855
},
18521856
{
18531857
// cheating a little; bash just did a=c
@@ -1866,10 +1870,10 @@ set +o pipefail
18661870
// weird assignments
18671871
{"a=b; a=(c d); echo ${a[@]}", "c d\n"},
18681872
{"a=(b c); a=d; echo ${a[@]}", "d c\n"},
1869-
{"declare -A a=([x]=b [y]=c); a=d; echo ${a[@]}", "d b c\n"},
1873+
{"declare -A a=([x]=b [y]=c); a=d; for e in ${a[@]}; do echo $e; done | sort", "b\nc\nd\n"},
18701874
{"i=3; a=b; a[i]=x; echo ${a[@]}", "b x\n"},
18711875
{"i=3; declare a=(b); a[i]=x; echo ${!a[@]}", "0 3\n"},
1872-
{"i=3; declare -A a=(['x']=b); a[i]=x; echo ${!a[@]}", "i x\n"},
1876+
{"i=3; declare -A a=(['x']=b); a[i]=x; for e in ${!a[@]}; do echo $e; done | sort", "i\nx\n"},
18731877

18741878
// declare
18751879
{"declare -B foo", "declare: invalid option \"-B\"\nexit status 2 #JUSTERR"},

interp/module_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var modCases = []struct {
7171
return fmt.Errorf("blacklisted: %s", args[0])
7272
},
7373
src: "a=$(malicious)",
74-
want: "blacklisted: malicious",
74+
want: "blacklisted: malicious\nexit status 1",
7575
},
7676
{
7777
name: "ExecBackground",

0 commit comments

Comments
 (0)