Skip to content

Commit

Permalink
Better fix for backslash issue: parse_word(x, 1) never return NULL.
Browse files Browse the repository at this point in the history
  • Loading branch information
landley committed Sep 5, 2023
1 parent ca6b479 commit 0c13db5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion tests/sh.test
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ testing "trailing \\ in ''" "$SH -c \$'echo \\'one\\\\\\ntwo\\''" \
testing 'trailing \ in ""' "$SH -c \$'echo \"one\\\\\\ntwo\"'" 'onetwo\n' \
'' ''
testing 'vanishing \' "$SH -c \$'echo \\\\\\n a'" 'a\n' '' ''
testing 'command\ arg' "$SH -c \$'echo\\\\\\n abc'" 'abc\n' '' ''
testing "exec3" '$C -c "{ exec readlink /proc/self/fd/0;} < /proc/self/exe"' \
"$(readlink -f $C)\n" "" ""
testing 'arg shift' "$SH -c '"'for i in "" 2 1 1 1; do echo $? $1; shift $i; done'"' one two three four five" \
Expand Down Expand Up @@ -237,7 +238,7 @@ testing 'hidden wildcards' \
testing "backtick1" 'x=fred; echo `echo $x`' 'fred\n' "" ""
testing "backtick2" 'x=fred; echo `x=y; echo $x`; echo $x' 'y\nfred\n' "" ""
testing '$(( ) )' 'echo ab$((echo hello) | tr e x)cd' "abhxllocd\n" "" ""
((++SKIP)); testing '$((x=y)) lifetime' 'a=boing; echo $a $a$((a=4))$a $a' 'boing boing44 4\n' '' ''
testing '$((x=y)) lifetime' 'a=boing; echo $a $a$((a=4))$a $a' 'boing boing44 4\n' '' ''

testing 'quote' "echo \"'\"" "'\n" "" ""

Expand Down
5 changes: 3 additions & 2 deletions toys/pending/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ static char *parse_word(char *start, int early)
} else if (*end=='(' && strchr("?*+@!", ii)) toybuf[quote++] = ')';
else {
if (ii!='\\') end--;
else if (!end[*end=='\n']) return *end ? 0 : end;
else if (!end[*end=='\n']) return (*end && !early) ? 0 : end;
if (early && !quote) return end;
}
end++;
Expand Down Expand Up @@ -1884,6 +1884,7 @@ static int expand_arg_nobrace(struct sh_arg *arg, char *str, unsigned flags,
if (*ss != '<') ss = 0;
else {
while (isspace(*++ss));
// Can't return NULL because guaranteed ) context end
if (!(ll = parse_word(ss, 0)-ss)) ss = 0;
else {
jj = ll+(ss-s);
Expand Down Expand Up @@ -2297,7 +2298,7 @@ static int expand_arg(struct sh_arg *arg, char *old, unsigned flags,
// collect brace spans
if ((TT.options&OPT_B) && !(flags&NO_BRACE)) for (i = 0; ; i++) {
// skip quoted/escaped text
while ((s = parse_word(old+i, 1)) != old+i && s) i += s-(old+i);
while ((s = parse_word(old+i, 1)) != old+i) i += s-(old+i);

// start a new span
if (old[i] == '{') {
Expand Down

0 comments on commit 0c13db5

Please sign in to comment.