Skip to content

Commit

Permalink
[spec/builtin-printf] Specify that integer overflow is an error
Browse files Browse the repository at this point in the history
Also check for overflow in 'trap'.

It's already done to an extent in 'ulimit'.

This is part of #2107.
  • Loading branch information
Andy C committed Oct 28, 2024
1 parent 239e008 commit 298ec0f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
6 changes: 5 additions & 1 deletion builtin/trap_osh.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,12 @@ def _IsUnsignedInteger(s):

# Note: could simplify this by making match.LooksLikeUnsigned()

ok, big_int = mops.FromStr2(s)
if not ok:
raise error.Usage('integer too big: %s' % s, loc.Missing)

# not (0 > s) is (s >= 0)
return not mops.Greater(mops.ZERO, mops.FromStr(s))
return not mops.Greater(mops.ZERO, big_int)


def _GetSignalNumber(sig_spec):
Expand Down
12 changes: 11 additions & 1 deletion spec/builtin-printf.test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## oils_failures_allowed: 2
## oils_failures_allowed: 0
## compare_shells: dash bash mksh zsh ash

# printf
Expand Down Expand Up @@ -1121,9 +1121,12 @@ for fmt in '%u\n' '%d\n'; do
printf "$fmt" '18446744073709551616'
echo
done

## status: 1
## STDOUT:
## END

## OK bash/dash/mksh status: 0
## OK bash/dash/mksh STDOUT:
18446744073709551615
18446744073709551615
Expand All @@ -1133,6 +1136,7 @@ done

## END

## BUG ash status: 0
## BUG ash STDOUT:
18446744073709551615
0
Expand All @@ -1142,6 +1146,7 @@ done

## END

## BUG zsh status: 0
## BUG zsh STDOUT:
1844674407370955161
1844674407370955161
Expand All @@ -1165,9 +1170,12 @@ for fmt in '%u\n' '%d\n'; do
printf "$fmt" '-18446744073709551616'
echo
done

## status: 1
## STDOUT:
## END

## OK bash/dash/mksh status: 0
## OK bash/dash/mksh STDOUT:
1
18446744073709551615
Expand All @@ -1177,6 +1185,7 @@ done

## END

## BUG zsh status: 0
## BUG zsh STDOUT:
16602069666338596455
16602069666338596455
Expand All @@ -1186,6 +1195,7 @@ done

## END

## BUG ash status: 0
## BUG ash STDOUT:
0
0
Expand Down
11 changes: 4 additions & 7 deletions test/runtime-errors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1204,14 +1204,11 @@ test-int-overflow() {
_osh-error-1 'printf %d'" $pos"
_osh-error-1 'printf %d'" $neg"

# TODO
return

_osh-error-1 "trap $pos ERR"
_osh-error-1 "trap $neg ERR"
_osh-error-2 "trap $pos ERR"
_osh-error-2 "trap $neg ERR"

_osh-error-1 "ulimit $pos"
_osh-error-1 "ulimit $neg"
_osh-error-2 "ulimit $pos"
_osh-error-2 "ulimit $neg"
}

#
Expand Down

0 comments on commit 298ec0f

Please sign in to comment.