sh: Detect dividing the smallest integer by -1.
authorPeter Avalos <pavalos@dragonflybsd.org>
Sun, 13 Feb 2011 01:47:27 +0000 (15:47 -1000)
committerPeter Avalos <pavalos@dragonflybsd.org>
Sun, 13 Feb 2011 01:47:27 +0000 (15:47 -1000)
Obtained-from: FreeBSD

bin/sh/arith_yacc.c
bin/sh/shell.h
tools/regression/bin/sh/expansion/arith11.0 [new file with mode: 0644]

index baa588d..666b191 100644 (file)
@@ -129,6 +129,8 @@ 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;
index 33ff63e..b3a094d 100644 (file)
@@ -63,6 +63,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 */
diff --git a/tools/regression/bin/sh/expansion/arith11.0 b/tools/regression/bin/sh/expansion/arith11.0
new file mode 100644 (file)
index 0000000..d02e558
--- /dev/null
@@ -0,0 +1,12 @@
+# $FreeBSD: src/tools/regression/bin/sh/expansion/arith11.0,v 1.1 2011/02/12 23:44:05 jilles Exp $
+# 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" ]