← Commit list

c2aa218f2

Denys Vlasenko
Denys Vlasenko
2018-08-04
SUCCESS Duration: 147s

Subject

ash,hush: properly handle ${v//pattern/repl} if pattern starts with /

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

—

Build Log

(log yok)

Commit Diff GitHub →

shell/ash.c+9 -1
@@ -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++;
shell/ash_test/ash-vars/var_bash_pattern_starting_with_slash.right+2 -0
@@ -0,0 +1,2 @@
+-dev-ram
+/dev-am
shell/ash_test/ash-vars/var_bash_pattern_starting_with_slash.tests+3 -0
@@ -0,0 +1,3 @@
+v=/dev/ram
+echo ${v////-}
+echo ${v///r/-}
shell/hush.c+9 -0
@@ -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;
shell/hush_test/hush-vars/var_bash_pattern_starting_with_slash.right+2 -0
@@ -0,0 +1,2 @@
+-dev-ram
+/dev-am
shell/hush_test/hush-vars/var_bash_pattern_starting_with_slash.tests+3 -0
@@ -0,0 +1,3 @@
+v=/dev/ram
+echo ${v////-}
+echo ${v///r/-}