hush: fix a='a\\'; echo "${a%\\\\}"
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 2 Mar 2018 19:48:36 +0000 (20:48 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 2 Mar 2018 19:48:36 +0000 (20:48 +0100)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/ash_test/ash-quoting/squote_in_varexp1.right [new file with mode: 0644]
shell/ash_test/ash-quoting/squote_in_varexp1.tests [new file with mode: 0755]
shell/ash_test/ash-quoting/squote_in_varexp2.right [new file with mode: 0644]
shell/ash_test/ash-quoting/squote_in_varexp2.tests [new file with mode: 0755]
shell/hush.c
shell/hush_test/hush-quoting/squote_in_varexp1.right [new file with mode: 0644]
shell/hush_test/hush-quoting/squote_in_varexp1.tests [new file with mode: 0755]
shell/hush_test/hush-quoting/squote_in_varexp2.right [new file with mode: 0644]
shell/hush_test/hush-quoting/squote_in_varexp2.tests [new file with mode: 0755]
shell/match.c

diff --git a/shell/ash_test/ash-quoting/squote_in_varexp1.right b/shell/ash_test/ash-quoting/squote_in_varexp1.right
new file mode 100644 (file)
index 0000000..9d0add3
--- /dev/null
@@ -0,0 +1,3 @@
+Nothing:
+Nothing:
+Ok:0
diff --git a/shell/ash_test/ash-quoting/squote_in_varexp1.tests b/shell/ash_test/ash-quoting/squote_in_varexp1.tests
new file mode 100755 (executable)
index 0000000..efb380d
--- /dev/null
@@ -0,0 +1,4 @@
+x='\\\\'
+printf Nothing:'%s\n' ${x#\\\\\\\\}
+printf Nothing:'%s\n' "${x#\\\\\\\\}"
+echo Ok:$?
diff --git a/shell/ash_test/ash-quoting/squote_in_varexp2.right b/shell/ash_test/ash-quoting/squote_in_varexp2.right
new file mode 100644 (file)
index 0000000..9d0add3
--- /dev/null
@@ -0,0 +1,3 @@
+Nothing:
+Nothing:
+Ok:0
diff --git a/shell/ash_test/ash-quoting/squote_in_varexp2.tests b/shell/ash_test/ash-quoting/squote_in_varexp2.tests
new file mode 100755 (executable)
index 0000000..806ad12
--- /dev/null
@@ -0,0 +1,4 @@
+x='\\\\'
+printf Nothing:'%s\n' ${x#'\\\\'}
+printf Nothing:'%s\n' "${x#'\\\\'}"
+echo Ok:$?
index da4967a8ad15c878e0e4857b7e476e4ada87faf9..762cc3fe4f520760f152ac82a9e6d04629a69ba2 100644 (file)
@@ -4500,6 +4500,7 @@ static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsign
                        }
                }
                o_addchr(dest, ch);
+               //bb_error_msg("%s:o_addchr('%c')", __func__, ch);
                if (ch == '(' || ch == '{') {
                        ch = (ch == '(' ? ')' : '}');
                        if (!add_till_closing_bracket(dest, input, ch))
@@ -4529,7 +4530,7 @@ static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsign
                        /* \x. Copy verbatim. Important for  \(, \) */
                        ch = i_getch(input);
                        if (ch == EOF) {
-                               syntax_error_unterm_ch(')');
+                               syntax_error_unterm_ch(end_ch);
                                return 0;
                        }
 #if 0
@@ -4540,6 +4541,7 @@ static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsign
                        }
 #endif
                        o_addchr(dest, ch);
+                       //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch);
                        continue;
                }
        }
@@ -5843,7 +5845,13 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
                                if (exp_op == *exp_word)  /* ## or %% */
                                        exp_word++;
                                debug_printf_expand("expand: exp_word:'%s'\n", exp_word);
-                               exp_exp_word = encode_then_expand_string(exp_word, /*process_bkslash:*/ 1, /*unbackslash:*/ 1);
+                               /*
+                                * process_bkslash:1 unbackslash:1 breaks this:
+                                * a='a\\'; echo ${a%\\\\} # correct output is: a
+                                * process_bkslash:1 unbackslash:0 breaks this:
+                                * a='a}'; echo ${a%\}}    # correct output is: a
+                                */
+                               exp_exp_word = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0);
                                if (exp_exp_word)
                                        exp_word = exp_exp_word;
                                debug_printf_expand("expand: exp_exp_word:'%s'\n", exp_word);
diff --git a/shell/hush_test/hush-quoting/squote_in_varexp1.right b/shell/hush_test/hush-quoting/squote_in_varexp1.right
new file mode 100644 (file)
index 0000000..9d0add3
--- /dev/null
@@ -0,0 +1,3 @@
+Nothing:
+Nothing:
+Ok:0
diff --git a/shell/hush_test/hush-quoting/squote_in_varexp1.tests b/shell/hush_test/hush-quoting/squote_in_varexp1.tests
new file mode 100755 (executable)
index 0000000..efb380d
--- /dev/null
@@ -0,0 +1,4 @@
+x='\\\\'
+printf Nothing:'%s\n' ${x#\\\\\\\\}
+printf Nothing:'%s\n' "${x#\\\\\\\\}"
+echo Ok:$?
diff --git a/shell/hush_test/hush-quoting/squote_in_varexp2.right b/shell/hush_test/hush-quoting/squote_in_varexp2.right
new file mode 100644 (file)
index 0000000..9d0add3
--- /dev/null
@@ -0,0 +1,3 @@
+Nothing:
+Nothing:
+Ok:0
diff --git a/shell/hush_test/hush-quoting/squote_in_varexp2.tests b/shell/hush_test/hush-quoting/squote_in_varexp2.tests
new file mode 100755 (executable)
index 0000000..806ad12
--- /dev/null
@@ -0,0 +1,4 @@
+x='\\\\'
+printf Nothing:'%s\n' ${x#'\\\\'}
+printf Nothing:'%s\n' "${x#'\\\\'}"
+echo Ok:$?
index 8f2a2fb3824b11f72d719de2641b94a886421094..ee8abb2db2dddad97fa82c6e9f32d83bc576dfc9 100644 (file)
@@ -75,7 +75,7 @@ char* FAST_FUNC scan_and_match(char *string, const char *pattern, unsigned flags
                        *loc = c;
                } else {
                        r = fnmatch(pattern, loc, 0);
-                       //bb_error_msg("fnmatch('%s','%s',0):%d", pattern, string, r);
+                       //bb_error_msg("fnmatch('%s','%s',0):%d", pattern, loc, r);
                }
                if (r == 0) /* match found */
                        return loc;