Browse Source

ash: fix handling of single-quoted strings in pattern substitution

function                                             old     new   delta
subevalvar                                          1576    1588     +12

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Denys Vlasenko 2 months ago
parent
commit
c5a1be25ba

+ 5 - 0
shell/ash.c

@@ -7073,6 +7073,11 @@ subevalvar(char *start, char *str, int strloc,
 				repl = NULL;
 				break;
 			}
+			/* Skip over quoted 'str'. Example: ${var/'/'} - second / is not a separator */
+			if ((unsigned char)*repl == CTLQUOTEMARK) {
+				while ((unsigned char)*++repl != CTLQUOTEMARK)
+					continue;
+			}
 			if (*repl == '/') {
 				*repl = '\0';
 				break;

+ 4 - 0
shell/ash_test/ash-quoting/dollar_repl_bash2.right

@@ -0,0 +1,4 @@
+axxb
+axxb
+axxb
+axxb

+ 8 - 0
shell/ash_test/ash-quoting/dollar_repl_bash2.tests

@@ -0,0 +1,8 @@
+v="x/x"
+# The second / is quoted, should not be treated as separator
+echo a${v/'/'}b
+# The second / is escaped, should not be treated as separator
+echo a${v/\/}b
+
+echo "a${v/'/'}b"
+echo "a${v/\/}b"

+ 4 - 0
shell/hush_test/hush-quoting/dollar_repl_bash2.right

@@ -0,0 +1,4 @@
+axxb
+axxb
+axxb
+axxb

+ 8 - 0
shell/hush_test/hush-quoting/dollar_repl_bash2.tests

@@ -0,0 +1,8 @@
+v="x/x"
+# The second / is quoted, should not be treated as separator
+echo a${v/'/'}b
+# The second / is escaped, should not be treated as separator
+echo a${v/\/}b
+
+echo "a${v/'/'}b"
+echo "a${v/\/}b"