From owner-svn-src-stable-10@freebsd.org Fri Apr 14 21:42:29 2017 Return-Path: Delivered-To: svn-src-stable-10@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 2910DD3C50A; Fri, 14 Apr 2017 21:42:29 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::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 01AC9F2; Fri, 14 Apr 2017 21:42:28 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v3ELgSXH052422; Fri, 14 Apr 2017 21:42:28 GMT (envelope-from jilles@FreeBSD.org) Received: (from jilles@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v3ELgRMK052395; Fri, 14 Apr 2017 21:42:27 GMT (envelope-from jilles@FreeBSD.org) Message-Id: <201704142142.v3ELgRMK052395@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jilles set sender to jilles@FreeBSD.org using -f From: Jilles Tjoelker Date: Fri, 14 Apr 2017 21:42:27 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r316942 - in stable/10/bin/sh: . tests/expansion X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-10@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for only the 10-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 14 Apr 2017 21:42:29 -0000 Author: jilles Date: Fri Apr 14 21:42:27 2017 New Revision: 316942 URL: https://svnweb.freebsd.org/changeset/base/316942 Log: MFC r314686: sh: Fix crash if a -T trap is taken during command substitution. Code like t=$(stat -f %m "$file") segfaulted if -T was active and a trap was taken while the shell was waiting for the child process to finish. What happened was that the dotrap() call in waitforjob() was hit. This re-entered command execution (including expand.c) at a point not expected by expbackq(), and global state (unallocated stack string and argbackq) was corrupted. To fix this, change expbackq() to prepare for command execution to be re-entered. In stable/10, there is more global state that needs to be restored than in stable/11 and head. Reported by: bdrewery Added: stable/10/bin/sh/tests/expansion/cmdsubst21.0 - copied unchanged from r314686, head/bin/sh/tests/expansion/cmdsubst21.0 stable/10/bin/sh/tests/expansion/cmdsubst22.0 - copied unchanged from r314686, head/bin/sh/tests/expansion/cmdsubst22.0 Modified: stable/10/bin/sh/expand.c stable/10/bin/sh/tests/expansion/Makefile Directory Properties: stable/10/ (props changed) Modified: stable/10/bin/sh/expand.c ============================================================================== --- stable/10/bin/sh/expand.c Fri Apr 14 20:15:34 2017 (r316941) +++ stable/10/bin/sh/expand.c Fri Apr 14 21:42:27 2017 (r316942) @@ -439,9 +439,6 @@ expbackq(union node *cmd, int quoted, in p = grabstackstr(dest); evalbackcmd(cmd, &in); ungrabstackstr(p, dest); - ifsfirst = saveifs; - ifslastp = savelastp; - argbackq = saveargbackq; p = in.buf; lastc = '\0'; @@ -479,14 +476,20 @@ expbackq(union node *cmd, int quoted, in close(in.fd); if (in.buf) ckfree(in.buf); - if (in.jp) + if (in.jp) { + p = grabstackstr(dest); exitstatus = waitforjob(in.jp, (int *)NULL); - if (quoted == 0) - recordregion(startloc, dest - stackblock(), 0); + ungrabstackstr(p, dest); + } TRACE(("expbackq: size=%td: \"%.*s\"\n", ((dest - stackblock()) - startloc), (int)((dest - stackblock()) - startloc), stackblock() + startloc)); + ifsfirst = saveifs; + ifslastp = savelastp; + if (quoted == 0) + recordregion(startloc, dest - stackblock(), 0); + argbackq = saveargbackq; expdest = dest; INTON; } Modified: stable/10/bin/sh/tests/expansion/Makefile ============================================================================== --- stable/10/bin/sh/tests/expansion/Makefile Fri Apr 14 20:15:34 2017 (r316941) +++ stable/10/bin/sh/tests/expansion/Makefile Fri Apr 14 21:42:27 2017 (r316942) @@ -41,6 +41,8 @@ FILES+= cmdsubst17.0 FILES+= cmdsubst18.0 FILES+= cmdsubst19.0 FILES+= cmdsubst20.0 +FILES+= cmdsubst21.0 +FILES+= cmdsubst22.0 FILES+= export1.0 FILES+= export2.0 FILES+= export3.0 Copied: stable/10/bin/sh/tests/expansion/cmdsubst21.0 (from r314686, head/bin/sh/tests/expansion/cmdsubst21.0) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/10/bin/sh/tests/expansion/cmdsubst21.0 Fri Apr 14 21:42:27 2017 (r316942, copy of r314686, head/bin/sh/tests/expansion/cmdsubst21.0) @@ -0,0 +1,6 @@ +# $FreeBSD$ + +set -T +trapped='' +trap "trapped=x$trapped" TERM +[ "x$($SH -c "kill $$")y" = xy ] && [ "$trapped" = x ] Copied: stable/10/bin/sh/tests/expansion/cmdsubst22.0 (from r314686, head/bin/sh/tests/expansion/cmdsubst22.0) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/10/bin/sh/tests/expansion/cmdsubst22.0 Fri Apr 14 21:42:27 2017 (r316942, copy of r314686, head/bin/sh/tests/expansion/cmdsubst22.0) @@ -0,0 +1,6 @@ +# $FreeBSD$ + +set -T +trapped='' +trap "trapped=x$trapped" TERM +[ "x$(:; kill $$)y" = xy ] && [ "$trapped" = x ]