ash,hush: properly handle ${v//pattern/repl} if pattern starts with /
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>
This commit is contained in:
parent
2005d3ff36
commit
c2aa218f23
10
shell/ash.c
10
shell/ash.c
@ -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++;
|
||||
|
@ -0,0 +1,2 @@
|
||||
-dev-ram
|
||||
/dev-am
|
3
shell/ash_test/ash-vars/var_bash_pattern_starting_with_slash.tests
Executable file
3
shell/ash_test/ash-vars/var_bash_pattern_starting_with_slash.tests
Executable file
@ -0,0 +1,3 @@
|
||||
v=/dev/ram
|
||||
echo ${v////-}
|
||||
echo ${v///r/-}
|
@ -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;
|
||||
|
@ -0,0 +1,2 @@
|
||||
-dev-ram
|
||||
/dev-am
|
3
shell/hush_test/hush-vars/var_bash_pattern_starting_with_slash.tests
Executable file
3
shell/hush_test/hush-vars/var_bash_pattern_starting_with_slash.tests
Executable file
@ -0,0 +1,3 @@
|
||||
v=/dev/ram
|
||||
echo ${v////-}
|
||||
echo ${v///r/-}
|
Loading…
Reference in New Issue
Block a user