Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Feb 2011 23:44:05 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r218626 - in head: bin/sh tools/regression/bin/sh/expansion
Message-ID:  <201102122344.p1CNi5DG018858@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jilles
Date: Sat Feb 12 23:44:05 2011
New Revision: 218626
URL: http://svn.freebsd.org/changeset/base/218626

Log:
  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.

Added:
  head/tools/regression/bin/sh/expansion/arith11.0   (contents, props changed)
Modified:
  head/bin/sh/arith_yacc.c
  head/bin/sh/shell.h

Modified: head/bin/sh/arith_yacc.c
==============================================================================
--- head/bin/sh/arith_yacc.c	Sat Feb 12 21:30:46 2011	(r218625)
+++ head/bin/sh/arith_yacc.c	Sat Feb 12 23:44:05 2011	(r218626)
@@ -125,6 +125,8 @@ static arith_t do_binop(int op, arith_t 
 	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;

Modified: head/bin/sh/shell.h
==============================================================================
--- head/bin/sh/shell.h	Sat Feb 12 21:30:46 2011	(r218625)
+++ head/bin/sh/shell.h	Sat Feb 12 23:44:05 2011	(r218626)
@@ -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 */

Added: head/tools/regression/bin/sh/expansion/arith11.0
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/bin/sh/expansion/arith11.0	Sat Feb 12 23:44:05 2011	(r218626)
@@ -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" ]



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201102122344.p1CNi5DG018858>