Skip to content

Commit

Permalink
[osh] Fix crashes on type mismatch s+=([0]=v), a+=([0]=v), `assoc…
Browse files Browse the repository at this point in the history
…+=(0)` (#2221)
  • Loading branch information
akinomyoga authored Jan 4, 2025
1 parent 45a426c commit 25161ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion osh/cmd_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def PlusEquals(old_val, val):
str_to_append = cast(value.Str, UP_val)
val = value.Str(old_val.s + str_to_append.s)

elif tag in (value_e.BashArray, value_e.SparseArray):
elif tag in (value_e.BashArray, value_e.SparseArray, value_e.BashAssoc):
e_die("Can't append array to string")

else:
Expand Down Expand Up @@ -236,13 +236,19 @@ def PlusEquals(old_val, val):
bash_impl.SparseArray_AppendValues(sparse_lhs, strs)
val = sparse_lhs

elif tag == value_e.BashAssoc:
e_die("Can't append an associative array to an indexed array")

else:
raise AssertionError() # parsing should prevent this

elif case(value_e.BashAssoc):
if tag == value_e.Str:
e_die("Can't append string to associative arrays")

elif tag in (value_e.BashArray, value_e.SparseArray):
e_die("Can't append an assoxiative array to indexed arrays")

elif tag == value_e.BashAssoc:
assoc_lhs = cast(value.BashAssoc, UP_old_val)
assoc_rhs = cast(value.BashAssoc, UP_val)
Expand Down
26 changes: 26 additions & 0 deletions spec/append.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,29 @@ echo "${e[@]}"
## N-I bash STDOUT:
e x
## END


#### Type mismatching of lhs+=rhs should not cause a crash
case $SH in mksh|zsh) exit ;; esac
s=
a=()
declare -A d=([lemon]=yellow)

s+=(1)
s+=([melon]=green)

a+=lime
a+=([1]=banana)

d+=orange
d+=(0)

true

## STDOUT:
## END

## OK osh status: 1

## N-I mksh/zsh STDOUT:
## END

0 comments on commit 25161ff

Please sign in to comment.