sh: Detect dividing the smallest integer by -1.

This overflows and on some architectures such as amd64 it generates SIGFPE.
Generate an error on all architectures.
This commit is contained in:
Jilles Tjoelker 2011-02-12 23:44:05 +00:00
parent 0a70456882
commit e9749129ad
3 changed files with 16 additions and 0 deletions

View File

@ -125,6 +125,8 @@ static arith_t do_binop(int op, arith_t a, arith_t b)
case ARITH_DIV:
if (!b)
yyerror("division by zero");
if (a == ARITH_MIN && b == -1)
yyerror("divide error");
return op == ARITH_REM ? a % b : a / b;
case ARITH_MUL:
return a * b;

View File

@ -59,6 +59,8 @@ typedef intmax_t arith_t;
#define ARITH_FORMAT_STR "%" PRIdMAX
#define atoarith_t(arg) strtoimax(arg, NULL, 0)
#define strtoarith_t(nptr, endptr, base) strtoimax(nptr, endptr, base)
#define ARITH_MIN INTMAX_MIN
#define ARITH_MAX INTMAX_MAX
typedef void *pointer;
#define MKINIT /* empty */

View File

@ -0,0 +1,12 @@
# $FreeBSD$
# Try to divide the smallest integer by -1.
# On amd64 this causes SIGFPE, so make sure the shell checks.
# Calculate the minimum possible value, assuming two's complement and
# a certain interpretation of overflow when shifting left.
minint=1
while [ $((minint <<= 1)) -gt 0 ]; do
:
done
v=$( eval ': $((minint / -1))' 2>&1 >/dev/null)
[ $? -ne 0 ] && [ -n "$v" ]