From owner-svn-src-all@freebsd.org Sun Mar 12 18:38:04 2017 Return-Path: Delivered-To: svn-src-all@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 C619CD083A8; Sun, 12 Mar 2017 18:38:04 +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 A0A4F15E7; Sun, 12 Mar 2017 18:38:04 +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 v2CIc3kj093374; Sun, 12 Mar 2017 18:38:03 GMT (envelope-from jilles@FreeBSD.org) Received: (from jilles@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v2CIc3di093370; Sun, 12 Mar 2017 18:38:03 GMT (envelope-from jilles@FreeBSD.org) Message-Id: <201703121838.v2CIc3di093370@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jilles set sender to jilles@FreeBSD.org using -f From: Jilles Tjoelker Date: Sun, 12 Mar 2017 18:38:03 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r315169 - in stable/11/bin/sh: . tests/expansion X-SVN-Group: stable-11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 12 Mar 2017 18:38:04 -0000 Author: jilles Date: Sun Mar 12 18:38:03 2017 New Revision: 315169 URL: https://svnweb.freebsd.org/changeset/base/315169 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. Reported by: bdrewery Added: stable/11/bin/sh/tests/expansion/cmdsubst21.0 - copied unchanged from r314686, head/bin/sh/tests/expansion/cmdsubst21.0 stable/11/bin/sh/tests/expansion/cmdsubst22.0 - copied unchanged from r314686, head/bin/sh/tests/expansion/cmdsubst22.0 Modified: stable/11/bin/sh/expand.c stable/11/bin/sh/tests/expansion/Makefile Directory Properties: stable/11/ (props changed) Modified: stable/11/bin/sh/expand.c ============================================================================== --- stable/11/bin/sh/expand.c Sun Mar 12 18:24:45 2017 (r315168) +++ stable/11/bin/sh/expand.c Sun Mar 12 18:38:03 2017 (r315169) @@ -460,7 +460,6 @@ expbackq(union node *cmd, int quoted, in p = grabstackstr(dest); evalbackcmd(cmd, &in); ungrabstackstr(p, dest); - argbackq = saveargbackq; p = in.buf; nnl = 0; @@ -513,12 +512,16 @@ 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); + ungrabstackstr(p, dest); + } TRACE(("expbackq: size=%td: \"%.*s\"\n", ((dest - stackblock()) - startloc), (int)((dest - stackblock()) - startloc), stackblock() + startloc)); + argbackq = saveargbackq; expdest = dest; INTON; } Modified: stable/11/bin/sh/tests/expansion/Makefile ============================================================================== --- stable/11/bin/sh/tests/expansion/Makefile Sun Mar 12 18:24:45 2017 (r315168) +++ stable/11/bin/sh/tests/expansion/Makefile Sun Mar 12 18:38:03 2017 (r315169) @@ -42,6 +42,8 @@ ${PACKAGE}FILES+= cmdsubst17.0 ${PACKAGE}FILES+= cmdsubst18.0 ${PACKAGE}FILES+= cmdsubst19.0 ${PACKAGE}FILES+= cmdsubst20.0 +${PACKAGE}FILES+= cmdsubst21.0 +${PACKAGE}FILES+= cmdsubst22.0 ${PACKAGE}FILES+= export1.0 ${PACKAGE}FILES+= export2.0 ${PACKAGE}FILES+= export3.0 Copied: stable/11/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/11/bin/sh/tests/expansion/cmdsubst21.0 Sun Mar 12 18:38:03 2017 (r315169, 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/11/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/11/bin/sh/tests/expansion/cmdsubst22.0 Sun Mar 12 18:38:03 2017 (r315169, 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 ]