Skip to content

Commit

Permalink
Give a proper error message instead of a crash.
Browse files Browse the repository at this point in the history
When 'a' in (( a++ )) is an array.

Still needs location info.
  • Loading branch information
Andy Chu committed Sep 9, 2018
1 parent e6a067e commit 6aadc3b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
21 changes: 14 additions & 7 deletions core/expr_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,15 @@ def EvalLhs(node, arith_ev, mem, exec_opts):
def _ValToArith(val, word=None):
"""Convert runtime.value to a Python int or list of strings."""
assert isinstance(val, runtime.value), '%r %r' % (val, type(val))
if val.tag == value_e.Undef:

if val.tag == value_e.Undef: # 'nounset' already handled before got here
return 0

if val.tag == value_e.Str:
return _StringToInteger(val.s, word=word)
if val.tag == value_e.StrArray:
return val.strs # Python list of strings
return _StringToInteger(val.s, word=word) # may raise FatalRuntimeError

if val.tag == value_e.StrArray: # array is valid on RHS, but not on left
return val.strs


class ArithEvaluator(_ExprEvaluator):
Expand All @@ -235,8 +238,12 @@ def _EvalLhsToArith(self, node):
int or list of strings, runtime.lvalue
"""
val, lval = EvalLhs(node, self, self.mem, self.exec_opts)
#log('Evaluating node %r -> %r', node, val)
return self._ValToArithOrError(val), lval

if val.tag == value_e.StrArray:
e_die("Can't use assignment like ++ or += on arrays")

i = self._ValToArithOrError(val)
return i, lval

def _Store(self, lval, new_int):
val = runtime.Str(str(new_int))
Expand All @@ -254,7 +261,7 @@ def Eval(self, node):
# can. ${foo:-3}4 is OK. $? will be a compound word too, so we don't have
# to handle that as a special case.

if node.tag == arith_expr_e.ArithVarRef: # $(( x ))
if node.tag == arith_expr_e.ArithVarRef: # $(( x )) (can be array)
val = self._LookupVar(node.name)
return self._ValToArithOrError(val)

Expand Down
9 changes: 8 additions & 1 deletion test/runtime-errors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ string_to_intbase() {
echo 'SHOULD NOT GET HERE'
}

array_arith() {
a=(1 2)
(( a++ )) # doesn't make sense
echo "${a[@]}"
}


#
# Builtins
#
Expand Down Expand Up @@ -324,7 +331,7 @@ all() {
failed_command \
pipefail pipefail_group pipefail_subshell pipefail_func pipefail_while \
nonexistent nounset \
nounset_arith divzero divzero_var \
nounset_arith divzero divzero_var array_arith \
string_to_int_arith string_to_hex string_to_octal \
string_to_intbase string_to_int_bool; do

Expand Down

0 comments on commit 6aadc3b

Please sign in to comment.