Source-Changes archive

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

CVS commit: src/bin/sh



Module Name:    src
Committed By:   kre
Date:           Tue Nov 24 13:58:15 UTC 2026

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

Log Message:
/bin/sh -- ${@:-stuff} expansion fix

This comes from the issue described in

        https://mail-index.netbsd.org/pkgsrc-users/2026/11/19/msg043805.html

where pkgsrc/sysutils/sysupgrade was shown to behave differently
when using different shells to execute it.

The relevant code line is:

        for set_name in "${@:-$(shtk_config_get SETS)}"; do

which intends to use "$@" (command line args, I think) if any are set, and the
results from:
        shtk_config_get SETS
otherwise - intending that those results would be split into separate args.

There are a number of issues with this .. first the "${@:-...}"  (and $* instead
of $@, with or without the ':' modifier), is expressly unspecified by POSIX.

Different shells interpret it differently - for some $@ is *never* unset (nor
is $* or most, possibly all, of the shell's special parameters), so testing for
it isn't productive there.   Second, for shells where it is possible, what causes
"$@" to be unset is fairly consistent (no positional parameters set), but what
causes it to be considered "null" (as in the meaning of the ':') modifier, is
even more variable.

So, code like that is simply unsafe and must be fixed.

But assuming we're in a situation where it does work (and several shells do
allow ${@:-...} to work, including ours) how should it be interpreted.

In general, for any variable V "${V:-word}" produces "${V}" if V is set, and
has a non-null (not empty) value, and "word" (which can contain any other
expansions) otherwise.   In fact, every other shell does that (a couple never
ever expand "word" because they treat $@ as always being set and non-null),
except our shell, which instead expands word (rather than "word"), more or
less - that is, for the case in question, it does word splitting upon word
which does not happen with "word" (but I think still managed to avoid pathname
generation (glob)).   Or it did, this fixes that.

As a by-product it will certainly break sysutils/sysupgrade until that is
fixed.   That means changing the line shown above into

        test "$#" -gt 0 || set -- $( shtk_config_get SETS )
        for set_name do

That is, if there are no positional parameters set, set them using the
command that gets the default set, and then simply process them, from $@
(which is what "for" does when the "in word..." clause is omitted.

The "set" there isn't highly safe, but that same expansion, in more or less
this same way, is used elsewhere in sysupgrade, so unless someone feels like
fixing all the other uses, adding this one will not add to the potential
problems it could cause.

CAUTION:  every time the $@ (and almost as often for $*) code is touched,
sh tends to break in weird and mysterious ways, so please be on the lookout
for anything going wrong, and let me know if anything (other than sysupgrade
breaking) is discovered.

I have run all of the sh ATF tests (which have MANY $@ related tests - and in
this commit a whole stack more are being added) - they all pass.  Unfortunately
the tests can only test the things that I can think of to test, not all the
bizarre other possibilities that various scripts attempt.

The added tests (test cases in an existing test program) are all testing POSIX
unspecified behaviour, hence they are all set to atf-skip if run with TEST_SH
(the shell being tested) set to anything except the NetBSD shell.

Once believed stable, this change will certainly be pulled up to -11 and -10
(and if it were possible, many previous releases, probably back to the 0.N days,
this is a very very old issue).

While here, I took the opportunity to clearly distinguish ${0} from ${00} ${000}
etc - $0 (or ${0}) is a special parameter, not a positional parameter, and
special parameters all have 1 character names ($0 #@ $* $- $? $! ...).
${00} (for any number of 0's) would be the zeroth positional parameter, which
does not exist, and there is no way to set.   Hence now ${00} is always unset,
and expands to nothing if referenced.   A single '0' is the only way to get the
special parameter (script or shell file name).   ${00} is unspecified in POSIX.

And as is common, a few changes (extra info) to some DEBUG mode code.

XXX pullup -11 -10 -9 -8 -7 -6 -5 -4 -3 -2.1 -2.0 -1.6 -1.5 ....


To generate a diff of this commit:
cvs rdiff -u -r1.152 -r1.153 src/bin/sh/expand.c

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




Home | Main Index | Thread Index | Old Index