NetBSD Problem Report #48202

From www@NetBSD.org  Tue Sep 10 05:14:36 2013
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [149.20.53.66])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "Postmaster NetBSD.org" (verified OK))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 3C1B771008
	for <gnats-bugs@gnats.NetBSD.org>; Tue, 10 Sep 2013 05:14:36 +0000 (UTC)
Message-Id: <20130910051434.D8F39715F0@mollari.NetBSD.org>
Date: Tue, 10 Sep 2013 05:14:34 +0000 (UTC)
From: william@25thandclement.com
Reply-To: william@25thandclement.com
To: gnats-bugs@NetBSD.org
Subject: sh +nounset and `for X; do` iteration fails if parameter set empty
X-Send-Pr-Version: www-1.0

>Number:         48202
>Notify-List:    joerg@NetBSD.org
>Category:       bin
>Synopsis:       sh +nounset and `for X; do` iteration fails if parameter set empty
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    ast@NetBSD.org
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Sep 10 05:15:00 +0000 2013
>Closed-Date:    Sun Oct 06 21:16:47 +0000 2013
>Last-Modified:  Sun Oct 06 21:16:47 +0000 2013
>Originator:     William Ahern
>Release:        6.1.1
>Organization:
N/A
>Environment:
NetBSD  6.1.1 NetBSD 6.1.1 (GENERIC) amd64
>Description:
With +nounset option, `for X; do` fails trying to expand "$@" with "sh: @: parameter not set".

Interestingly, if you replace "set -- ${NULL}" with "set -- ${NULL:-}", then the loop iterates once with a zero length parameter.

NetBSD's sh behaves differently than that of FreeBSD, OpenBSD, Linux, Solaris, and OS X. Expected behavior is that the loop iterates 0 times, without complaint or error (if +errexit set).

It's unclear to me what POSIX requires here. bash won't complain if trying to expand $@ when there are no parameters (e.g. "set -e -u; set --; for X; do :; done". OpenBSD's ksh balks at `set -e -u; set --; echo "$@";' but not `set -e -u; set --; for X; do`.


>How-To-Repeat:
# problem
sh -c 'set -e -u; NULL=; set -- ${NULL}; for X; do echo "[$X]"; done;'

# noteworthy behavior
sh -c 'set -e -u; NULL=; set -- ${NULL:-}; for X; do echo "[$X]"; done;'


>Fix:
(Added by ast@)
Apply FreeBSD's solution:

http://svnweb.freebsd.org/base/releng/9.2/bin/sh/expand.c?r1=198453&r2=198454&view=patch

--- head/bin/sh/expand.c	2009/10/24 20:57:11	198453
+++ head/bin/sh/expand.c	2009/10/24 21:20:04	198454
@@ -657,7 +657,7 @@
 	}
		varlen = 0;
			startloc = expdest - stackblock();
			-	if (!set && uflag) {
			+	if (!set && uflag && *var != '@' && *var != '*') {
					switch (subtype) {
							case VSNORMAL:
									case VSTRIMLEFT:

which was "fixed" a long while ago with this commit message:

    Modified Sat Oct 24 21:20:04 2009 UTC (3 years, 10 months ago) by jilles 
    File length: 31256 byte(s) 
    Diff to previous 194977
    sh: Exempt $@ and $* from set -u

    This seems more useful and will likely be in the next POSIX standard.

    Also document more precisely in the man page what set -u does (note that
    $@, $* and $! are the only special parameters that can ever be unset, all
    the others are always set, although they may be empty).

>Release-Note:
The question is do we want to follow FreeBSD or "the next POSIX standard"?
If we change this we should remember to update manual page.

>Audit-Trail:

Responsible-Changed-From-To: bin-bug-people->ast@NetBSD.org
Responsible-Changed-By: ast@NetBSD.org
Responsible-Changed-When: Sat, 14 Sep 2013 07:07:45 +0000
Responsible-Changed-Why:
I'll field the feedback and commit the change in expand.ch and manual page.


State-Changed-From-To: open->feedback
State-Changed-By: ast@NetBSD.org
State-Changed-When: Sat, 14 Sep 2013 07:07:45 +0000
State-Changed-Why:
Found out how FreeBSD solved this, let's wait for feedback.


From: David Holland <dholland-bugs@netbsd.org>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/48202 (sh +nounset and `for X; do` iteration fails if
 parameter set empty)
Date: Sat, 28 Sep 2013 18:19:51 +0000

 On Sat, Sep 14, 2013 at 07:07:45AM +0000, ast@NetBSD.org wrote:
  > State-Changed-From-To: open->feedback
  > State-Changed-By: ast@NetBSD.org
  > State-Changed-When: Sat, 14 Sep 2013 07:07:45 +0000
  > State-Changed-Why:
  > Found out how FreeBSD solved this, let's wait for feedback.

 Can you post this in the PR for the benefit of those of us following
 along at home?

ast> See Fix: above (I had added that)

 -- 
 David A. Holland
 dholland@netbsd.org

From: "Adrian Steinmann" <ast@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/48202 CVS commit: src
Date: Sun, 6 Oct 2013 21:05:50 +0000

 Module Name:	src
 Committed By:	ast
 Date:		Sun Oct  6 21:05:50 UTC 2013

 Modified Files:
 	src/bin/sh: expand.c
 	src/tests/bin/sh: t_expand.sh

 Log Message:
 Fix PR bin/48202 [non-critical/low]:
   sh +nounset and `for X; do` iteration fails if parameter set empty
 by applying and testing FreeBSD's patch of Oct 24 2009 for this; see
   http://svnweb.freebsd.org/base/head/bin/sh/expand.c?r1=198453&r2=198454
 Also created an ATF test in tests/bin/sh/t_expand.sh for this error and
 corrected a space->tabs problem there as well.


 To generate a diff of this commit:
 cvs rdiff -u -r1.89 -r1.90 src/bin/sh/expand.c
 cvs rdiff -u -r1.1 -r1.2 src/tests/bin/sh/t_expand.sh

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: feedback->closed
State-Changed-By: ast@NetBSD.org
State-Changed-When: Sun, 06 Oct 2013 21:16:47 +0000
State-Changed-Why:
Fixed inn rev 1.90 of src/bin/sh/expand.c


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.