NetBSD Problem Report #55320

From jschauma@netmeister.org  Sat May 30 15:50:39 2020
Return-Path: <jschauma@netmeister.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 7D1921A921A
	for <gnats-bugs@gnats.NetBSD.org>; Sat, 30 May 2020 15:50:39 +0000 (UTC)
Message-Id: <20200530155035.570AC85861@panix.netmeister.org>
Date: Sat, 30 May 2020 11:50:35 -0400 (EDT)
From: jschauma@netmeister.org
Reply-To: jschauma@netmeister.org
To: gnats-bugs@NetBSD.org
Subject: /bin/sh redirection in functions does not match other shells
X-Send-Pr-Version: 3.95

>Number:         55320
>Category:       bin
>Synopsis:       /bin/sh redirection in functions does not match other shells
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    kre
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat May 30 15:55:00 +0000 2020
>Closed-Date:    Wed Aug 26 22:09:34 +0000 2020
>Last-Modified:  Wed Aug 26 22:30:01 +0000 2020
>Originator:     Jan Schaumann
>Release:        NetBSD 8.0
>Organization:

>Environment:
$ ident /bin/sh
/bin/sh:
     $NetBSD: crt0.S,v 1.3 2011/07/01 02:59:05 joerg Exp $
     $NetBSD: crt0-common.c,v 1.14 2016/06/07 12:07:35 joerg Exp $
     $NetBSD: crti.S,v 1.1 2010/08/07 18:01:35 joerg Exp $
     $NetBSD: crtbegin.S,v 1.2 2010/11/30 18:37:59 joerg Exp $
     $NetBSD: alias.c,v 1.15 2014/06/18 18:17:30 christos Exp $
     $NetBSD: arith_token.c,v 1.4.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: arithmetic.c,v 1.2.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: cd.c,v 1.47.6.2 2017/07/23 14:58:14 snj Exp $
     $NetBSD: echo.c,v 1.14 2008/10/12 01:40:37 dholland Exp $
     $NetBSD: error.c,v 1.39.8.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: eval.c,v 1.140.2.3 2018/07/13 14:29:15 martin Exp $
     $NetBSD: exec.c,v 1.47.2.3 2018/07/13 14:29:15 martin Exp $
     $NetBSD: expand.c,v 1.110.2.4 2018/07/13 14:32:01 martin Exp $
     $NetBSD: histedit.c,v 1.48.8.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: input.c,v 1.56.2.2 2017/08/09 05:35:18 snj Exp $
     $NetBSD: jobs.c,v 1.85.2.2 2017/11/17 14:56:52 martin Exp $
     $NetBSD: mail.c,v 1.16.90.1 2017/06/05 08:10:24 snj Exp $
     $NetBSD: main.c,v 1.70.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: memalloc.c,v 1.29.62.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: miscbltin.c,v 1.44 2017/05/13 15:03:34 gson Exp $
     $NetBSD: mystring.c,v 1.17 2013/04/28 17:01:28 dholland Exp $
     $NetBSD: options.c,v 1.49 2017/05/29 14:03:23 kre Exp $
     $NetBSD: parser.c,v 1.132.2.4 2018/05/06 09:32:57 martin Exp $
     $NetBSD: redir.c,v 1.57.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: show.c,v 1.42.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: trap.c,v 1.40.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: output.c,v 1.36.2.1 2017/11/23 13:26:01 martin Exp $
     $NetBSD: var.c,v 1.55.2.2 2017/08/31 11:43:44 martin Exp $
     $NetBSD: test.c,v 1.41 2016/09/05 01:00:07 sevan Exp $
     $NetBSD: kill.c,v 1.27.36.1 2017/07/23 14:58:13 snj Exp $
     $NetBSD: syntax.c,v 1.3.26.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: printf.c,v 1.37.8.1 2018/07/13 15:58:25 martin Exp $
     $NetBSD: crtend.S,v 1.1 2010/08/07 18:01:34 joerg Exp $
     $NetBSD: crtn.S,v 1.1 2010/08/07 18:01:35 joerg Exp $

Architecture: x86_64
Machine: amd64
>Description:

When using /bin/sh, it appears that a stderr redirection inside a
function affects subsequent calls when it ought to only apply to the
statements within that function.

>How-To-Repeat:

$ cat >/tmp/a.sh <<"EOF"
f() { :; }
g() { f >&2; }

v=$(g; echo foo; )

echo "v=$v"
EOF
$ 

Expected output:

$ sh /tmp/a.sh
v=foo

Actual output:
$ sh /tmp/a.sh
foo
v=
$ sh /tmp/a.sh 2>/dev/null
v=
$ 


That is, the ">&2" redirection applied to the invocation of f()
inside g() appears to continue to be applied _after_
g() has been invoked.

This is different in bash, zsh, and ksh:

$ ksh /tmp/a.sh 2>/dev/null
v=foo


Note: if we replace the call to f() with e.g., a call to a command
or even a shell builtin (e.g., 'echo'), then we get the expected behavior:

g() { echo >&2; }
v=$(g; echo foo; )
echo "v=$v"



>Fix:


>Release-Note:

>Audit-Trail:

Responsible-Changed-From-To: bin-bug-people->kre
Responsible-Changed-By: kre@NetBSD.org
Responsible-Changed-When: Sat, 30 May 2020 17:10:58 +0000
Responsible-Changed-Why:
I am looking into this PR


From: Robert Elz <kre@munnari.OZ.AU>
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: bin/55320: /bin/sh redirection in functions does not match other shells
Date: Sun, 31 May 2020 05:10:27 +0700

 This is fixed in HEAD (and that almost certainly means also in -9
 though I don't have a bootable -9 around to test right at the minute).

 A bunch of fixes that went into -9 didn't get pulled up to -8 because
 some of the code has just changed too much (it is easier to build a
 sh from HEAD on -8 than it would be to implement some of the fixes).

 Right now I don't even remember this particular issue, or what was
 involved in fixing it ... (I did a very quick scan of some likely cvs log
 entries and didn't see it.)

 kre

State-Changed-From-To: open->closed
State-Changed-By: kre@NetBSD.org
State-Changed-When: Wed, 26 Aug 2020 22:09:34 +0000
State-Changed-Why:
I have now confirmed that this bug is no longer present in -9 or HEAD.

If you need a fix on -8 it would be simpler to simply import the -9
or HEAD src/bin/sh and build that - it isn't difficult, and I'm willing
to assist if there are any issues.


From: Jan Schaumann <jschauma@netmeister.org>
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: bin/55320 (/bin/sh redirection in functions does not match other
 shells)
Date: Wed, 26 Aug 2020 18:28:23 -0400

 kre@NetBSD.org wrote:
 > If you need a fix on -8 it would be simpler to simply import the -9
 > or HEAD src/bin/sh and build that - it isn't difficult, and I'm willing
 > to assist if there are any issues.

 That's alright - having it fixed in 9 is perfectly
 sufficient.  Thanks!

 -Jan

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.