From 23fc1ecab1b49e3dfd681882946c6729adb78da6 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 29 May 2023 11:20:58 -0500 Subject: [PATCH] Fix escape passthrough bug reported by Mingliang Hu. --- tests/sh.test | 1 + toys/pending/sh.c | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/sh.test b/tests/sh.test index e435d26d..9e39c3d0 100644 --- a/tests/sh.test +++ b/tests/sh.test @@ -127,6 +127,7 @@ testing 'return code 3' 'x=0; while [ $((x++)) -lt 2 ]; do echo $x; done; echo $ testing 'local var +whiteout' \ 'l=X;x(){ local l=47; echo $l;unset l; echo l=$l;};x;echo $l' '47\nl=\nX\n' \ '' '' +testing 'escape passthrough' 'echo -e "a\nb\nc\td"' 'a\nb\nc\td\n' '' '' testing 'trailing $ is literal' 'echo $' '$\n' '' '' # TODO testing 'empty +() is literal' 'echo +()' '+()\n' '' '' diff --git a/toys/pending/sh.c b/toys/pending/sh.c index ed2b6912..36f183b8 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -1903,13 +1903,13 @@ static int expand_arg_nobrace(struct sh_arg *arg, char *str, unsigned flags, for (kk = strlen(ifs); kk && ifs[kk-1]=='\n'; ifs[--kk] = 0); close(jj); } - } else if (cc=='\\' || !str[ii]) { - if (!(qq&1) || (str[ii] && strchr("\"\\$`", str[ii]))) - new[oo++] = str[ii] ? str[ii++] : cc; + } else if (!str[ii]) new[oo++] = cc; + else if (cc=='\\') + new[oo++] = (!(qq&1) || strchr("\"\\$`", str[ii])) ? str[ii++] : cc; // $VARIABLE expansions - } else if (cc == '$' && str[ii]) { + else if (cc == '$') { cc = *(ss = str+ii++); if (cc=='\'') { for (s = str+ii; *s != '\''; oo += wcrtomb(new+oo, unescape2(&s, 0),0)); -- 2.39.2