svn commit: r275777 - in stable/10/bin/sh: . tests/parameters

Jilles Tjoelker jilles at FreeBSD.org
Sun Dec 14 18:30:32 UTC 2014


Author: jilles
Date: Sun Dec 14 18:30:30 2014
New Revision: 275777
URL: https://svnweb.freebsd.org/changeset/base/275777

Log:
  MFC r268576: sh: Correctly handle positional parameters beyond INT_MAX on
  64-bit systems.
  
  Currently, there can be no more than INT_MAX positional parameters. Make
  sure to treat all higher ones as unset to avoid incorrect results and
  crashes.
  
  On 64-bit systems, our atoi() takes the low 32 bits of the strtol() and
  sign-extends them.
  
  On 32-bit systems, the call to atoi() returned INT_MAX for too high values
  and there is not enough address space for so many positional parameters, so
  there was no issue.
  
  PR:		195918

Added:
  stable/10/bin/sh/tests/parameters/positional5.0
     - copied unchanged from r268576, head/bin/sh/tests/parameters/positional5.0
Modified:
  stable/10/bin/sh/expand.c
  stable/10/bin/sh/tests/parameters/Makefile
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/bin/sh/expand.c
==============================================================================
--- stable/10/bin/sh/expand.c	Sun Dec 14 18:28:01 2014	(r275776)
+++ stable/10/bin/sh/expand.c	Sun Dec 14 18:30:30 2014	(r275777)
@@ -846,9 +846,11 @@ varisset(const char *name, int nulok)
 		}
 	} else if (is_digit(*name)) {
 		char *ap;
-		int num = atoi(name);
+		long num;
 
-		if (num > shellparam.nparam)
+		errno = 0;
+		num = strtol(name, NULL, 10);
+		if (errno != 0 || num > shellparam.nparam)
 			return 0;
 
 		if (num == 0)

Modified: stable/10/bin/sh/tests/parameters/Makefile
==============================================================================
--- stable/10/bin/sh/tests/parameters/Makefile	Sun Dec 14 18:28:01 2014	(r275776)
+++ stable/10/bin/sh/tests/parameters/Makefile	Sun Dec 14 18:30:30 2014	(r275777)
@@ -13,6 +13,7 @@ FILES+=		optind1.0
 FILES+=		optind2.0
 FILES+=		positional1.0
 FILES+=		positional2.0
+FILES+=		positional5.0
 FILES+=		pwd1.0
 FILES+=		pwd2.0
 

Copied: stable/10/bin/sh/tests/parameters/positional5.0 (from r268576, head/bin/sh/tests/parameters/positional5.0)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/10/bin/sh/tests/parameters/positional5.0	Sun Dec 14 18:30:30 2014	(r275777, copy of r268576, head/bin/sh/tests/parameters/positional5.0)
@@ -0,0 +1,14 @@
+# $FreeBSD$
+
+i=1
+r=0
+while [ $i -lt $((0x100000000)) ]; do
+	t=
+	eval t=\${$i-x}
+	case $t in
+	x) ;;
+	*) echo "Problem with \${$i}" >&2; r=1 ;;
+	esac
+	i=$((i + 0x10000000))
+done
+exit $r


More information about the svn-src-all mailing list