From owner-svn-src-head@FreeBSD.ORG Sat Jul 12 21:54:12 2014 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id E475AA00; Sat, 12 Jul 2014 21:54:12 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id B71512764; Sat, 12 Jul 2014 21:54:12 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s6CLsCc9042202; Sat, 12 Jul 2014 21:54:12 GMT (envelope-from jilles@svn.freebsd.org) Received: (from jilles@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s6CLsBt6042199; Sat, 12 Jul 2014 21:54:11 GMT (envelope-from jilles@svn.freebsd.org) Message-Id: <201407122154.s6CLsBt6042199@svn.freebsd.org> From: Jilles Tjoelker Date: Sat, 12 Jul 2014 21:54:11 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r268576 - in head/bin/sh: . tests/parameters X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 12 Jul 2014 21:54:13 -0000 Author: jilles Date: Sat Jul 12 21:54:11 2014 New Revision: 268576 URL: http://svnweb.freebsd.org/changeset/base/268576 Log: 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. Added: head/bin/sh/tests/parameters/positional5.0 (contents, props changed) Modified: head/bin/sh/expand.c head/bin/sh/tests/parameters/Makefile Modified: head/bin/sh/expand.c ============================================================================== --- head/bin/sh/expand.c Sat Jul 12 21:24:28 2014 (r268575) +++ head/bin/sh/expand.c Sat Jul 12 21:54:11 2014 (r268576) @@ -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: head/bin/sh/tests/parameters/Makefile ============================================================================== --- head/bin/sh/tests/parameters/Makefile Sat Jul 12 21:24:28 2014 (r268575) +++ head/bin/sh/tests/parameters/Makefile Sat Jul 12 21:54:11 2014 (r268576) @@ -15,6 +15,7 @@ FILES+= positional1.0 FILES+= positional2.0 FILES+= positional3.0 FILES+= positional4.0 +FILES+= positional5.0 FILES+= pwd1.0 FILES+= pwd2.0 Added: head/bin/sh/tests/parameters/positional5.0 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/bin/sh/tests/parameters/positional5.0 Sat Jul 12 21:54:11 2014 (r268576) @@ -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