summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2017-03-18 16:07:28 +0000
committerjilles <jilles@FreeBSD.org>2017-03-18 16:07:28 +0000
commit8138298519bd938039ecfcd5a8f190a42378cbca (patch)
tree700086891ab1749a0f989bccdf1ea8e20970195c /bin
parent73962cb3eb6172fd88238b83d4308e52a17cc1af (diff)
downloadFreeBSD-src-8138298519bd938039ecfcd5a8f190a42378cbca.zip
FreeBSD-src-8138298519bd938039ecfcd5a8f190a42378cbca.tar.gz
MFC r315005: sh: Fix executing wrong command with ${x#$(y)}$(z).
The parsed internal representation of words consists of a byte string with a list of nodes (commands in command substitution). Each unescaped CTLBACKQ or CTLBACKQ | CTLQUOTE byte corresponds to an entry in the list. If param in ${param#%##%%word} is not set, the word is not expanded (in a deviation of POSIX shared with other ash variants and ksh93). Erroneously, the pointer in the list of commands (argbackq) was not advanced. This caused the wrong command to be executed later if the outer word contained another command substitution. Example: echo "${unsetvar#$(echo a)}$(echo b)" wrote "a" but should write "b".
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/expand.c4
-rw-r--r--bin/sh/tests/expansion/Makefile1
-rw-r--r--bin/sh/tests/expansion/cmdsubst23.05
3 files changed, 9 insertions, 1 deletions
diff --git a/bin/sh/expand.c b/bin/sh/expand.c
index 8de5141..05cd3c0 100644
--- a/bin/sh/expand.c
+++ b/bin/sh/expand.c
@@ -768,8 +768,10 @@ again: /* jump here after setting a variable with ${var=text} */
case VSTRIMLEFTMAX:
case VSTRIMRIGHT:
case VSTRIMRIGHTMAX:
- if (!set)
+ if (!set) {
+ set = 1;
break;
+ }
/*
* Terminate the string and start recording the pattern
* right after it
diff --git a/bin/sh/tests/expansion/Makefile b/bin/sh/tests/expansion/Makefile
index c062232..99ed07f 100644
--- a/bin/sh/tests/expansion/Makefile
+++ b/bin/sh/tests/expansion/Makefile
@@ -44,6 +44,7 @@ ${PACKAGE}FILES+= cmdsubst19.0
${PACKAGE}FILES+= cmdsubst20.0
${PACKAGE}FILES+= cmdsubst21.0
${PACKAGE}FILES+= cmdsubst22.0
+${PACKAGE}FILES+= cmdsubst23.0
${PACKAGE}FILES+= export1.0
${PACKAGE}FILES+= export2.0
${PACKAGE}FILES+= export3.0
diff --git a/bin/sh/tests/expansion/cmdsubst23.0 b/bin/sh/tests/expansion/cmdsubst23.0
new file mode 100644
index 0000000..cde8698
--- /dev/null
+++ b/bin/sh/tests/expansion/cmdsubst23.0
@@ -0,0 +1,5 @@
+# $FreeBSD$
+
+unset n
+x=abcd
+[ "X${n#$(echo a)}X${x#$(echo ab)}X$(echo abc)X" = XXcdXabcX ]
OpenPOWER on IntegriCloud