aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-01-25 14:07:40 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-01-25 14:07:40 +0100
commitcba79a87f86f4f8d3c5b21ff7024b392402cf287 (patch)
tree02f18db88a950e77a471960aa3147ca87ab9f2a0
parent0ca3198f9333b363ced46bfabf51c0d6b111c875 (diff)
downloadbusybox-w32-cba79a87f86f4f8d3c5b21ff7024b392402cf287.tar.gz
busybox-w32-cba79a87f86f4f8d3c5b21ff7024b392402cf287.tar.bz2
busybox-w32-cba79a87f86f4f8d3c5b21ff7024b392402cf287.zip
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>
-rw-r--r--shell/hush.c13
-rw-r--r--shell/hush_test/hush-vars/var_bash_repl_empty_pattern.right2
-rwxr-xr-xshell/hush_test/hush-vars/var_bash_repl_empty_pattern.tests3
-rw-r--r--shell/hush_test/hush-vars/var_bash_repl_empty_var.right2
-rwxr-xr-xshell/hush_test/hush-vars/var_bash_repl_empty_var.tests3
5 files changed, 21 insertions, 2 deletions
diff --git a/shell/hush.c b/shell/hush.c
index a9183c82f..cf3c731bc 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -5643,6 +5643,10 @@ static char *replace_pattern(char *val, const char *pattern, const char *repl, c
5643 unsigned res_len = 0; 5643 unsigned res_len = 0;
5644 unsigned repl_len = strlen(repl); 5644 unsigned repl_len = strlen(repl);
5645 5645
5646 /* Null pattern never matches, including if "var" is empty */
5647 if (!pattern[0])
5648 return result; /* NULL, no replaces happened */
5649
5646 while (1) { 5650 while (1) {
5647 int size; 5651 int size;
5648 char *s = strstr_pattern(val, pattern, &size); 5652 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
5809 * and if // is used, it is encoded as \: 5813 * and if // is used, it is encoded as \:
5810 * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> 5814 * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL>
5811 */ 5815 */
5812 /* Empty variable always gives nothing: */
5813 // "v=''; echo ${v/*/w}" prints "", not "w"
5814 if (val && val[0]) { 5816 if (val && val[0]) {
5815 /* pattern uses non-standard expansion. 5817 /* pattern uses non-standard expansion.
5816 * repl should be unbackslashed and globbed 5818 * 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
5846 val = to_be_freed; 5848 val = to_be_freed;
5847 free(pattern); 5849 free(pattern);
5848 free(repl); 5850 free(repl);
5851 } else {
5852 /* Empty variable always gives nothing */
5853 // "v=''; echo ${v/*/w}" prints "", not "w"
5854 /* Just skip "replace" part */
5855 *p++ = SPECIAL_VAR_SYMBOL;
5856 p = strchr(p, SPECIAL_VAR_SYMBOL);
5857 *p = '\0';
5849 } 5858 }
5850 } 5859 }
5851#endif /* BASH_PATTERN_SUBST */ 5860#endif /* BASH_PATTERN_SUBST */
diff --git a/shell/hush_test/hush-vars/var_bash_repl_empty_pattern.right b/shell/hush_test/hush-vars/var_bash_repl_empty_pattern.right
new file mode 100644
index 000000000..d400a7e31
--- /dev/null
+++ b/shell/hush_test/hush-vars/var_bash_repl_empty_pattern.right
@@ -0,0 +1,2 @@
1v
2Ok:0
diff --git a/shell/hush_test/hush-vars/var_bash_repl_empty_pattern.tests b/shell/hush_test/hush-vars/var_bash_repl_empty_pattern.tests
new file mode 100755
index 000000000..6e8aa2afa
--- /dev/null
+++ b/shell/hush_test/hush-vars/var_bash_repl_empty_pattern.tests
@@ -0,0 +1,3 @@
1v=v
2echo ${v//}
3echo Ok:$?
diff --git a/shell/hush_test/hush-vars/var_bash_repl_empty_var.right b/shell/hush_test/hush-vars/var_bash_repl_empty_var.right
new file mode 100644
index 000000000..892916783
--- /dev/null
+++ b/shell/hush_test/hush-vars/var_bash_repl_empty_var.right
@@ -0,0 +1,2 @@
1
2Ok:0
diff --git a/shell/hush_test/hush-vars/var_bash_repl_empty_var.tests b/shell/hush_test/hush-vars/var_bash_repl_empty_var.tests
new file mode 100755
index 000000000..73a43d38e
--- /dev/null
+++ b/shell/hush_test/hush-vars/var_bash_repl_empty_var.tests
@@ -0,0 +1,3 @@
1v=''
2echo ${v/*/w}
3echo Ok:$?