Skip to content

Commit

Permalink
[osh-language] Do additional type checks for arithmetic.
Browse files Browse the repository at this point in the history
Fixes unhandled Python exception with +, etc.
  • Loading branch information
Andy Chu committed Jul 14, 2019
1 parent 714a80b commit 6fd86d4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
6 changes: 6 additions & 0 deletions osh/expr_eval.py
Expand Up @@ -521,6 +521,12 @@ def Eval(self, node, int_coerce=True):
if op_id == Id.Arith_Comma:
return rhs

# Do additional type checking after indexing and comma.
if not isinstance(lhs, int):
e_die('LHS should be an integer, got %s', lhs)
if not isinstance(rhs, int):
e_die('RHS should be an integer, got %s', rhs)

if op_id == Id.Arith_Plus:
return lhs + rhs
if op_id == Id.Arith_Minus:
Expand Down
24 changes: 23 additions & 1 deletion spec/arith.test.sh
Expand Up @@ -130,7 +130,7 @@ echo "[$undef1][$undef2]"
## stdout: [1][1]
## N-I dash stdout: [][]

#### Increment and decrement array
#### Increment and decrement array elements
shopt -u strict-arith || true
a=(5 6 7 8)
(( a[0]++, ++a[1], a[2]--, --a[3] ))
Expand Down Expand Up @@ -393,3 +393,25 @@ echo [$a]
[]
## END
## OK bash status: 0

#### Can't add integer to indexed array
declare -a array=(1 2 3)
echo $((array + 5))
## status: 1
## stdout-json: ""
## BUG bash status: 0
## BUG bash STDOUT:
6
## END
## N-I dash status: 2

#### Can't add integer to associative array
typeset -A assoc
assoc[0]=42
echo $((assoc + 5))
## status: 1
## stdout-json: ""
## BUG bash/mksh/zsh status: 0
## BUG bash/mksh/zsh stdout: 47
## BUG dash status: 0
## BUG dash stdout: 5
5 changes: 4 additions & 1 deletion spec/assoc.test.sh
Expand Up @@ -195,7 +195,10 @@ a["aa"]=b
a["foo"]=bar
a['a+1']=c
echo "${a[a+1]}"
## stdout: c
## stdout-json: ""
## status: 1
## BUG bash stdout: c
## BUG bash status: 0

#### lookup by unquoted string as arithmetic

Expand Down
2 changes: 1 addition & 1 deletion test/spec.sh
Expand Up @@ -575,7 +575,7 @@ append() {

# associative array -- mksh and zsh implement different associative arrays.
assoc() {
sh-spec spec/assoc.test.sh --osh-failures-allowed 9 \
sh-spec spec/assoc.test.sh --osh-failures-allowed 8 \
$BASH $OSH_LIST "$@"
}

Expand Down

0 comments on commit 6fd86d4

Please sign in to comment.