From a5cb58abc8c383d45032cb12e3f93691e4442a03 Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Wed, 18 Nov 2015 21:09:03 +0000 Subject: [PATCH] sh: Fix ""$@, which should not use the special case for "$@". "$@" should expand to no words if there are no positional parameters, but ""$@ should always expand to at least an empty word. --- bin/sh/expand.c | 3 ++- bin/sh/tests/parameters/positional8.0 | 31 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 bin/sh/tests/parameters/positional8.0 diff --git a/bin/sh/expand.c b/bin/sh/expand.c index f7c6735f9e33..621798275bb1 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -249,7 +249,8 @@ argstr(char *p, int flag) case CTLQUOTEMARK: lit_quoted = 1; /* "$@" syntax adherence hack */ - if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=') + if (p[0] == CTLVAR && (p[1] & VSQUOTE) != 0 && + p[2] == '@' && p[3] == '=') break; if ((flag & EXP_FULL) != 0) USTPUTC(c, expdest); diff --git a/bin/sh/tests/parameters/positional8.0 b/bin/sh/tests/parameters/positional8.0 new file mode 100644 index 000000000000..4c4dbd5cf1a6 --- /dev/null +++ b/bin/sh/tests/parameters/positional8.0 @@ -0,0 +1,31 @@ +# $FreeBSD$ + +failures='' +ok='' + +testcase() { + code="$1" + expected="$2" + oIFS="$IFS" + eval "$code" + IFS='|' + result="$#|$*" + IFS="$oIFS" + if [ "x$result" = "x$expected" ]; then + ok=x$ok + else + failures=x$failures + echo "For $code, expected $expected actual $result" + fi +} + +testcase 'shift $#; set -- ""$*' '1|' +testcase 'shift $#; set -- $*""' '1|' +testcase 'shift $#; set -- ""$@' '1|' +testcase 'shift $#; set -- $@""' '1|' +testcase 'shift $#; set -- """$*"' '1|' +testcase 'shift $#; set -- "$*"""' '1|' +testcase 'shift $#; set -- """$@"' '1|' +testcase 'shift $#; set -- "$@"""' '1|' + +test "x$failures" = x