ash,hush: properly handle ${v//pattern/repl} if pattern starts with /
authorDenys Vlasenko <vda.linux@googlemail.com>
Sat, 4 Aug 2018 20:25:28 +0000 (22:25 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 4 Aug 2018 20:25:28 +0000 (22:25 +0200)
Closes 2695

function                                             old     new   delta
parse_dollar                                         762     790     +28
subevalvar                                          1258    1267      +9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 37/0)               Total: 37 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/ash.c
shell/ash_test/ash-vars/var_bash_pattern_starting_with_slash.right [new file with mode: 0644]
shell/ash_test/ash-vars/var_bash_pattern_starting_with_slash.tests [new file with mode: 0755]
shell/hush.c
shell/hush_test/hush-vars/var_bash_pattern_starting_with_slash.right [new file with mode: 0644]
shell/hush_test/hush-vars/var_bash_pattern_starting_with_slash.tests [new file with mode: 0755]

index 03fbbee53eb2f64dd80eadd5acafae23a15a6ad0..5c431c9ffb5fa2c079f502bd46e61bd2f8b63649 100644 (file)
@@ -6854,8 +6854,15 @@ subevalvar(char *p, char *varname, int strloc, int subtype,
        if (subtype == VSREPLACE || subtype == VSREPLACEALL) {
                /* Find '/' and replace with NUL */
                repl = p;
+               /* The pattern can't be empty.
+                * IOW: if the first char after "${v//" is a slash,
+                * it does not terminate the pattern - it's the first char of the pattern:
+                *  v=/dev/ram; echo ${v////-}  prints -dev-ram (pattern is "/")
+                *  v=/dev/ram; echo ${v///r/-} prints /dev-am  (pattern is "/r")
+                */
+               if (*repl == '/')
+                       repl++;
                for (;;) {
-                       /* Handle escaped slashes, e.g. "${v/\//_}" (they are CTLESC'ed by this point) */
                        if (*repl == '\0') {
                                repl = NULL;
                                break;
@@ -6864,6 +6871,7 @@ subevalvar(char *p, char *varname, int strloc, int subtype,
                                *repl = '\0';
                                break;
                        }
+                       /* Handle escaped slashes, e.g. "${v/\//_}" (they are CTLESC'ed by this point) */
                        if ((unsigned char)*repl == CTLESC && repl[1])
                                repl++;
                        repl++;
diff --git a/shell/ash_test/ash-vars/var_bash_pattern_starting_with_slash.right b/shell/ash_test/ash-vars/var_bash_pattern_starting_with_slash.right
new file mode 100644 (file)
index 0000000..439dca5
--- /dev/null
@@ -0,0 +1,2 @@
+-dev-ram
+/dev-am
diff --git a/shell/ash_test/ash-vars/var_bash_pattern_starting_with_slash.tests b/shell/ash_test/ash-vars/var_bash_pattern_starting_with_slash.tests
new file mode 100755 (executable)
index 0000000..b83fb8e
--- /dev/null
@@ -0,0 +1,3 @@
+v=/dev/ram
+echo ${v////-}
+echo ${v///r/-}
index 6852b5f79ce1bb24fd3c14d45a0797e2a9b02832..3407711cdd282a04df737b830f20470a25e7abbe 100644 (file)
@@ -4930,6 +4930,15 @@ static int parse_dollar(o_string *as_string,
                                        end_ch = '}' * 0x100 + '/';
                                }
                                o_addchr(dest, ch);
+                               /* The pattern can't be empty.
+                                * IOW: if the first char after "${v//" is a slash,
+                                * it does not terminate the pattern - it's the first char of the pattern:
+                                *  v=/dev/ram; echo ${v////-}  prints -dev-ram (pattern is "/")
+                                *  v=/dev/ram; echo ${v///r/-} prints /dev-am  (pattern is "/r")
+                                */
+                               if (i_peek(input) == '/') {
+                                       o_addchr(dest, i_getch(input));
+                               }
  again:
                                if (!BB_MMU)
                                        pos = dest->length;
diff --git a/shell/hush_test/hush-vars/var_bash_pattern_starting_with_slash.right b/shell/hush_test/hush-vars/var_bash_pattern_starting_with_slash.right
new file mode 100644 (file)
index 0000000..439dca5
--- /dev/null
@@ -0,0 +1,2 @@
+-dev-ram
+/dev-am
diff --git a/shell/hush_test/hush-vars/var_bash_pattern_starting_with_slash.tests b/shell/hush_test/hush-vars/var_bash_pattern_starting_with_slash.tests
new file mode 100755 (executable)
index 0000000..b83fb8e
--- /dev/null
@@ -0,0 +1,3 @@
+v=/dev/ram
+echo ${v////-}
+echo ${v///r/-}