Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 13 Mar 2019 21:53:11 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r345117 - in stable/12/bin/sh: . tests/expansion
Message-ID:  <201903132153.x2DLrB2N087278@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jilles
Date: Wed Mar 13 21:53:10 2019
New Revision: 345117
URL: https://svnweb.freebsd.org/changeset/base/345117

Log:
  MFC r342880,r343981,r344902: sh: Fix $((-9223372036854775808))
  
  Since $((9223372036854775808)) overflows, $((-9223372036854775808)) was not
  parsed correctly (with x=-9223372036854775808, $((x)) already worked, since
  that parses the value with the minus sign in one step). Values further from
  zero are still clamped to 9223372036854775807.
  
  Also, allow the full 64 bits in octal and hexadecimal.

Added:
  stable/12/bin/sh/tests/expansion/arith15.0
     - copied, changed from r342880, head/bin/sh/tests/expansion/arith15.0
  stable/12/bin/sh/tests/expansion/arith16.0
     - copied unchanged from r343981, head/bin/sh/tests/expansion/arith16.0
  stable/12/bin/sh/tests/expansion/arith17.0
     - copied unchanged from r343981, head/bin/sh/tests/expansion/arith17.0
Modified:
  stable/12/bin/sh/arith_yacc.c
  stable/12/bin/sh/arith_yacc.h
  stable/12/bin/sh/arith_yylex.c
  stable/12/bin/sh/shell.h
  stable/12/bin/sh/tests/expansion/Makefile
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/bin/sh/arith_yacc.c
==============================================================================
--- stable/12/bin/sh/arith_yacc.c	Wed Mar 13 20:29:10 2019	(r345116)
+++ stable/12/bin/sh/arith_yacc.c	Wed Mar 13 21:53:10 2019	(r345117)
@@ -104,7 +104,7 @@ static arith_t arith_lookupvarint(char *varname)
 	if (str == NULL || *str == '\0')
 		str = "0";
 	errno = 0;
-	result = strtoarith_t(str, &p, 0);
+	result = strtoarith_t(str, &p);
 	if (errno != 0 || *p != '\0')
 		yyerror("variable conversion error");
 	return result;

Modified: stable/12/bin/sh/arith_yacc.h
==============================================================================
--- stable/12/bin/sh/arith_yacc.h	Wed Mar 13 20:29:10 2019	(r345116)
+++ stable/12/bin/sh/arith_yacc.h	Wed Mar 13 21:53:10 2019	(r345117)
@@ -90,4 +90,5 @@ union yystype {
 
 extern union yystype yylval;
 
+arith_t strtoarith_t(const char *restrict nptr, char **restrict endptr);
 int yylex(void);

Modified: stable/12/bin/sh/arith_yylex.c
==============================================================================
--- stable/12/bin/sh/arith_yylex.c	Wed Mar 13 20:29:10 2019	(r345116)
+++ stable/12/bin/sh/arith_yylex.c	Wed Mar 13 21:53:10 2019	(r345117)
@@ -35,6 +35,8 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#include <ctype.h>
+#include <errno.h>
 #include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
@@ -50,6 +52,32 @@ __FBSDID("$FreeBSD$");
 #error Arithmetic tokens are out of order.
 #endif
 
+arith_t
+strtoarith_t(const char *restrict nptr, char **restrict endptr)
+{
+	arith_t val;
+
+	while (isspace((unsigned char)*nptr))
+		nptr++;
+	switch (*nptr) {
+		case '-':
+			return strtoimax(nptr, endptr, 0);
+		case '0':
+			return (arith_t)strtoumax(nptr, endptr, 0);
+		default:
+			val = (arith_t)strtoumax(nptr, endptr, 0);
+			if (val >= 0)
+				return val;
+			else if (val == ARITH_MIN) {
+				errno = ERANGE;
+				return ARITH_MIN;
+			} else {
+				errno = ERANGE;
+				return ARITH_MAX;
+			}
+	}
+}
+
 int
 yylex(void)
 {
@@ -78,7 +106,7 @@ yylex(void)
 		case '7':
 		case '8':
 		case '9':
-			yylval.val = strtoarith_t(buf, &end, 0);
+			yylval.val = strtoarith_t(buf, &end);
 			arith_buf = end;
 			return ARITH_NUM;
 		case 'A':

Modified: stable/12/bin/sh/shell.h
==============================================================================
--- stable/12/bin/sh/shell.h	Wed Mar 13 20:29:10 2019	(r345116)
+++ stable/12/bin/sh/shell.h	Wed Mar 13 21:53:10 2019	(r345117)
@@ -59,8 +59,6 @@
  */
 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
 

Modified: stable/12/bin/sh/tests/expansion/Makefile
==============================================================================
--- stable/12/bin/sh/tests/expansion/Makefile	Wed Mar 13 20:29:10 2019	(r345116)
+++ stable/12/bin/sh/tests/expansion/Makefile	Wed Mar 13 21:53:10 2019	(r345117)
@@ -21,6 +21,9 @@ ${PACKAGE}FILES+=	arith11.0
 ${PACKAGE}FILES+=	arith12.0
 ${PACKAGE}FILES+=	arith13.0
 ${PACKAGE}FILES+=	arith14.0
+${PACKAGE}FILES+=	arith15.0
+${PACKAGE}FILES+=	arith16.0
+${PACKAGE}FILES+=	arith17.0
 ${PACKAGE}FILES+=	assign1.0
 ${PACKAGE}FILES+=	cmdsubst1.0
 ${PACKAGE}FILES+=	cmdsubst2.0

Copied and modified: stable/12/bin/sh/tests/expansion/arith15.0 (from r342880, head/bin/sh/tests/expansion/arith15.0)
==============================================================================
--- head/bin/sh/tests/expansion/arith15.0	Wed Jan  9 09:36:54 2019	(r342880, copy source)
+++ stable/12/bin/sh/tests/expansion/arith15.0	Wed Mar 13 21:53:10 2019	(r345117)
@@ -12,9 +12,9 @@ check() {
 XXX=-9223372036854775808
 check "XXX"		-9223372036854775808
 check "XXX - 1" 	9223372036854775807
-check $(($XXX - 1))	9223372036854775807
-check $(($XXX - 2))	9223372036854775806
-check $((0x8000000000000000 == 0x7fffffffffffffff)) \
+check "$XXX - 1"	9223372036854775807
+check "$XXX - 2"	9223372036854775806
+check "0x8000000000000000 == 0x7fffffffffffffff" \
 			0
 
 exit $((failures != 0))

Copied: stable/12/bin/sh/tests/expansion/arith16.0 (from r343981, head/bin/sh/tests/expansion/arith16.0)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/12/bin/sh/tests/expansion/arith16.0	Wed Mar 13 21:53:10 2019	(r345117, copy of r343981, head/bin/sh/tests/expansion/arith16.0)
@@ -0,0 +1,26 @@
+# $FreeBSD$
+
+failures=0
+
+for x in \
+	0x10000000000000000 \
+	-0x8000000000000001 \
+	0xfffffffffffffffffffffffffffffffff \
+	-0xfffffffffffffffffffffffffffffffff \
+	02000000000000000000000 \
+	9223372036854775808 \
+	9223372036854775809 \
+	-9223372036854775809 \
+	9999999999999999999999999 \
+	-9999999999999999999999999
+do
+	msg=$({
+		v=$((x)) || :
+	} 3>&1 >&2 2>&3 3>&-)
+	r=$?
+	if [ "$r" = 0 ] || [ -z "$msg" ]; then
+		printf 'Failed: %s\n' "$x"
+		: $((failures += 1))
+	fi
+done
+exit $((failures > 0))

Copied: stable/12/bin/sh/tests/expansion/arith17.0 (from r343981, head/bin/sh/tests/expansion/arith17.0)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/12/bin/sh/tests/expansion/arith17.0	Wed Mar 13 21:53:10 2019	(r345117, copy of r343981, head/bin/sh/tests/expansion/arith17.0)
@@ -0,0 +1,3 @@
+# $FreeBSD$
+
+[ $((9223372036854775809)) -gt 0 ]



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