hush: fix two corner cases in ${v/pattern/repl}. Closes 10686
function old new delta expand_one_var 1592 1618 +26 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
13
shell/hush.c
13
shell/hush.c
@ -5643,6 +5643,10 @@ static char *replace_pattern(char *val, const char *pattern, const char *repl, c
|
|||||||
unsigned res_len = 0;
|
unsigned res_len = 0;
|
||||||
unsigned repl_len = strlen(repl);
|
unsigned repl_len = strlen(repl);
|
||||||
|
|
||||||
|
/* Null pattern never matches, including if "var" is empty */
|
||||||
|
if (!pattern[0])
|
||||||
|
return result; /* NULL, no replaces happened */
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
int size;
|
int size;
|
||||||
char *s = strstr_pattern(val, pattern, &size);
|
char *s = strstr_pattern(val, pattern, &size);
|
||||||
@ -5809,8 +5813,6 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
|
|||||||
* and if // is used, it is encoded as \:
|
* and if // is used, it is encoded as \:
|
||||||
* var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL>
|
* var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL>
|
||||||
*/
|
*/
|
||||||
/* Empty variable always gives nothing: */
|
|
||||||
// "v=''; echo ${v/*/w}" prints "", not "w"
|
|
||||||
if (val && val[0]) {
|
if (val && val[0]) {
|
||||||
/* pattern uses non-standard expansion.
|
/* pattern uses non-standard expansion.
|
||||||
* repl should be unbackslashed and globbed
|
* repl should be unbackslashed and globbed
|
||||||
@ -5846,6 +5848,13 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
|
|||||||
val = to_be_freed;
|
val = to_be_freed;
|
||||||
free(pattern);
|
free(pattern);
|
||||||
free(repl);
|
free(repl);
|
||||||
|
} else {
|
||||||
|
/* Empty variable always gives nothing */
|
||||||
|
// "v=''; echo ${v/*/w}" prints "", not "w"
|
||||||
|
/* Just skip "replace" part */
|
||||||
|
*p++ = SPECIAL_VAR_SYMBOL;
|
||||||
|
p = strchr(p, SPECIAL_VAR_SYMBOL);
|
||||||
|
*p = '\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif /* BASH_PATTERN_SUBST */
|
#endif /* BASH_PATTERN_SUBST */
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
v
|
||||||
|
Ok:0
|
3
shell/hush_test/hush-vars/var_bash_repl_empty_pattern.tests
Executable file
3
shell/hush_test/hush-vars/var_bash_repl_empty_pattern.tests
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
v=v
|
||||||
|
echo ${v//}
|
||||||
|
echo Ok:$?
|
2
shell/hush_test/hush-vars/var_bash_repl_empty_var.right
Normal file
2
shell/hush_test/hush-vars/var_bash_repl_empty_var.right
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
Ok:0
|
3
shell/hush_test/hush-vars/var_bash_repl_empty_var.tests
Executable file
3
shell/hush_test/hush-vars/var_bash_repl_empty_var.tests
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
v=''
|
||||||
|
echo ${v/*/w}
|
||||||
|
echo Ok:$?
|
Reference in New Issue
Block a user