Skip to content

Commit

Permalink
POSIX compliance fix: apply 'set -u' to $1, $2, ...
Browse files Browse the repository at this point in the history
POSIX requires[*] that expanding any unset parameter other than $@
and $* is an error when 'set -u'/'set -o nounset' is active.
However, on ksh93, $1, $2, ... were exempt as well. That is a bug.
[*] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_25

src/cmd/ksh93/sh/macro.c:
- varsub(): Backport code for handling 'set -u' for positional
  parameters from the ast 2016-10-01-beta branch.

src/cmd/ksh93/tests/options.sh:
- Add relevant regression tests.

src/cmd/ksh93/sh.1:
- Document that $@ and $* are exempt from 'set -u'.

(cherry picked from commit f954c6be0748c4c38a680a75f27564965fbd328e)
  • Loading branch information
McDutchie committed Jun 11, 2020
1 parent e2a648b commit 36da314
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ For full details, see the git log at:

Any uppercase BUG_* names are modernish shell bug IDs.

2020-06-08:

- If 'set -u'/'set -o nounset' is active, then the shell now errors out if a
nonexistent positional parameter such as $1, $2, ... is accessed, as other
shells do and POSIX requires. (This does *not* apply to "$@" and "$*".)

2020-06-06:

- The 'times' command is now a builtin command that conforms to POSIX
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* David Korn <[email protected]> *
* *
***********************************************************************/
#define SH_RELEASE "93u+m 2020-06-06"
#define SH_RELEASE "93u+m 2020-06-08"
4 changes: 4 additions & 0 deletions src/cmd/ksh93/sh.1
Original file line number Diff line number Diff line change
Expand Up @@ -7041,6 +7041,10 @@ Sort the positional parameters lexicographically.
.TP 8
.B \-u
Treat unset parameters as an error when substituting.
.B "$@"
and
.B "$\(**"
are exempt.
.TP 8
.B \-v
Print shell input lines as they are read.
Expand Down
8 changes: 8 additions & 0 deletions src/cmd/ksh93/sh/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,14 @@ static int varsub(Mac_t *mp)
}
else
v = 0;
/* Handle 'set -u'/'set -o nounset' for positional parameters */
if(!v && sh_isoption(SH_NOUNSET))
{
d=fcget();
fcseek(-1);
if(!(d && strchr(":+-?=",d)))
errormsg(SH_DICT,ERROR_exit(1),e_notset,ltos(c));
}
break;
case S_ALP:
if(c=='.' && type==0)
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/ksh93/tests/options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ $SHELL -uc 'var=foo;unset var;: ${!var}' >/dev/null 2>&1 && err_exit '${!var} sh
$SHELL -uc 'var=foo;unset var;: ${#var}' >/dev/null 2>&1 && err_exit '${#var} should fail with set -u'
$SHELL -uc 'var=foo;unset var;: ${var-OK}' >/dev/null 2>&1 || err_exit '${var-OK} should not fail with set -u'
$SHELL -uc 'var=foo;nset var;: ${var:-OK}' >/dev/null 2>&1 || err_exit '${var:-OK} should not fail with set -u'
(set -u -- one two; : $2) 2>/dev/null || err_exit "an unset PP failed with set -u"
(set -u -- one two; : $3) 2>/dev/null && err_exit "a set PP failed to fail with set -u"
(set -u --; : $@ $*) 2>/dev/null || err_exit '$@ and/or $* fail to be exempt from set -u'

z=$($SHELL 2>&1 -uc 'print ${X23456789012345}')
[[ $z == *X23456789012345:* ]] || err_exit "error message garbled with set -u got $z"
Expand Down

0 comments on commit 36da314

Please sign in to comment.