diff options
author | Dmitrij Czarkoff <czarkoff@cvs.openbsd.org> | 2016-03-05 12:30:18 +0000 |
---|---|---|
committer | Dmitrij Czarkoff <czarkoff@cvs.openbsd.org> | 2016-03-05 12:30:18 +0000 |
commit | 4891607afdded0cafbf74b6b9c36b7899cd89704 (patch) | |
tree | 0138d5404e98729a920e1922438b779b8f4b3bf5 | |
parent | fae4c9c841e061dff9b9edacae7cce3449beb085 (diff) |
POSIX-compliant behavior of "set -u" regarding "$*" and "$@" specials
All work done by Martijn Dekker
OK millert@
-rw-r--r-- | bin/ksh/eval.c | 8 | ||||
-rw-r--r-- | regress/bin/ksh/obsd-regress.t | 13 |
2 files changed, 16 insertions, 5 deletions
diff --git a/bin/ksh/eval.c b/bin/ksh/eval.c index 7bcf3928076..7124fcec939 100644 --- a/bin/ksh/eval.c +++ b/bin/ksh/eval.c @@ -1,4 +1,4 @@ -/* $OpenBSD: eval.c,v 1.49 2015/12/30 09:07:00 tedu Exp $ */ +/* $OpenBSD: eval.c,v 1.50 2016/03/05 12:30:17 czarkoff Exp $ */ /* * Expansion - quoting, separation, substitution, globbing @@ -707,6 +707,7 @@ varsub(Expand *xp, char *sp, char *word, int slen; char *p; struct tbl *vp; + int zero_ok = 0; if (sp[0] == '\0') /* Bad variable name */ return -1; @@ -715,8 +716,6 @@ varsub(Expand *xp, char *sp, char *word, /* ${#var}, string length or array size */ if (sp[0] == '#' && (c = sp[1]) != '\0') { - int zero_ok = 0; - /* Can't have any modifiers for ${#...} */ if (*word != CSUBST) return -1; @@ -789,6 +788,7 @@ varsub(Expand *xp, char *sp, char *word, xp->split = c == '@'; /* $@ */ state = XARG; } + zero_ok = 1; /* exempt "$@" and "$*" from 'set -u' */ } else { if ((p=strchr(sp,'[')) && (p[1]=='*'||p[1]=='@') && p[2]==']') { XPtrV wv; @@ -835,7 +835,7 @@ varsub(Expand *xp, char *sp, char *word, (((stype&0x80) ? *xp->str=='\0' : xp->str==null) ? /* undef? */ c == '=' || c == '-' || c == '?' : c == '+')) state = XBASE; /* expand word instead of variable value */ - if (Flag(FNOUNSET) && xp->str == null && + if (Flag(FNOUNSET) && xp->str == null && !zero_ok && (ctype(c, C_SUBOP2) || (state != XBASE && c != '+'))) errorf("%s: parameter not set", sp); return state; diff --git a/regress/bin/ksh/obsd-regress.t b/regress/bin/ksh/obsd-regress.t index e7988f5ca03..26759c89cbb 100644 --- a/regress/bin/ksh/obsd-regress.t +++ b/regress/bin/ksh/obsd-regress.t @@ -1,4 +1,4 @@ -# $OpenBSD: obsd-regress.t,v 1.1 2013/12/02 20:39:44 millert Exp $ +# $OpenBSD: obsd-regress.t,v 1.2 2016/03/05 12:30:17 czarkoff Exp $ # # ksh regression tests from OpenBSD @@ -273,3 +273,14 @@ stdin: set foo bar baz ; for out in ; do echo $out ; done --- + +name: shellopt-u-1 +description: + Check that "$@" and "$*" are exempt from 'set -u' (nounset) +stdin: + set -u + : "$@$*$1" +expected-exit: e == 1 +expected-stderr-pattern: + /: 1: parameter not set$/ +--- |