NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

bin/48202: sh +nounset and `for X; do` iteration fails if parameter set empty



>Number:         48202
>Category:       bin
>Synopsis:       sh +nounset and `for X; do` iteration fails if parameter set 
>empty
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Sep 10 05:15:00 +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:



Home | Main Index | Thread Index | Old Index