Skip to content

Commit

Permalink
Bug fix: properly validate the argument to break/continue/return.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed May 1, 2019
1 parent a1f279b commit 64fbb68
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
6 changes: 5 additions & 1 deletion osh/cmd_exec.py
Expand Up @@ -889,7 +889,11 @@ def _Dispatch(self, node, fork_external):
if node.arg_word: # Evaluate the argument
val = self.word_ev.EvalWordToString(node.arg_word)
assert val.tag == value_e.Str
arg = int(val.s) # They all take integers
try:
arg = int(val.s) # They all take integers
except ValueError:
e_die('%r expected a number, got %r',
node.token.val, val.s, word=node.arg_word)
else:
arg = 0 # return 0, exit 0, break 0 levels, etc.

Expand Down
20 changes: 20 additions & 0 deletions spec/bugs.test.sh
Expand Up @@ -86,3 +86,23 @@ no plus or minus ++--++--
## BUG ash status: 0
## N-I dash stdout-json: ""
## N-I dash status: 2

#### single quotes work inside character classes
x='a[[[---]]]b'
echo "${x//['[]']}"
## STDOUT:
a---b
## END
## BUG ash STDOUT:
a[[[---]]]b
## END
## N-I dash stdout-json: ""
## N-I dash status: 2
#### comparison: :- operator with single quoted arg
echo ${unset:-'a'}
echo "${unset:-'a'}"
## STDOUT:
a
'a'
## END
35 changes: 35 additions & 0 deletions spec/loop.test.sh
Expand Up @@ -152,3 +152,38 @@ Should not print
Should not print
. 3
## END

#### bad arg to break
x=oops
while true; do
echo hi
break $x
sleep 0.1
done
## stdout: hi
## status: 1
## OK dash status: 2
## OK bash status: 128

#### too many args to continue
# OSH treats this as a parse error
for x in a b c; do
echo $x
# bash breaks rather than continue or fatal error!!!
continue 1 2 3
done
echo --
## stdout-json: ""
## status: 2
## BUG bash STDOUT:
a
--
## END
## OK bash status: 0
## BUG dash/mksh/zsh STDOUT:
a
b
c
--
## END
## BUG dash/mksh/zsh status: 0
2 changes: 1 addition & 1 deletion test/spec.sh
Expand Up @@ -230,7 +230,7 @@ osh-only() {
# Regress bugs
bugs() {
sh-spec spec/bugs.test.sh ${REF_SHELLS[@]} $ZSH $BUSYBOX_ASH $OSH_LIST "$@" \
--osh-failures-allowed 2
--osh-failures-allowed 3
}

blog1() {
Expand Down

0 comments on commit 64fbb68

Please sign in to comment.