Skip to content

Commit ca54214

Browse files
committed
interp: don't crash on declaring empty names
Updates mvdan#332.
1 parent 2df1ac9 commit ca54214

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

interp/interp.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,11 @@ func (r *Runner) cmd(ctx context.Context, cm syntax.Command) {
876876
for _, as := range x.Assigns {
877877
for _, as := range r.flattenAssign(as) {
878878
name := as.Name.Value
879+
if !syntax.ValidName(name) {
880+
r.errf("declare: invalid name %q\n", name)
881+
r.exit = 1
882+
return
883+
}
879884
vr := r.lookupVar(as.Name.Value)
880885
vr.Value = r.assignVal(as, valType)
881886
if global {

interp/interp_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,8 @@ set +o pipefail
18691869
{"a=x=y; declare $a; echo $a $x", "x=y y\n"},
18701870
{"a='x=(y)'; declare $a; echo $a $x", "x=(y) (y)\n"},
18711871
{"a='x=b y=c'; declare $a; echo $x $y", "b c\n"},
1872+
{"declare =bar", "declare: invalid name \"=bar\"\nexit status 1 #JUSTERR"},
1873+
{"declare $unset=$unset", "declare: invalid name \"\"\nexit status 1 #JUSTERR"},
18721874

18731875
// export
18741876
{"declare foo=bar; env | grep '^foo='", "exit status 1"},

0 commit comments

Comments
 (0)