Skip to content

Commit

Permalink
Fix backslash parsing in $'' so \' doesn't end quote context.
Browse files Browse the repository at this point in the history
  • Loading branch information
landley committed Nov 23, 2024
1 parent cfef2aa commit 97b5ea1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 5 additions & 2 deletions tests/sh.test
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,12 @@ testing '[[1<2]] is alphabetical, not numeric' '[[ 123 < 19 ]] && echo yes' \
'yes\n' '' ''
testing '[[~]]' '[[ ~ == $HOME ]] && echo yes' 'yes\n' '' ''

$BROKEN testing 'quoting contexts nest' \
$'echo -n "$(echo "hello $(eval $\'echo -\\\\\\ne \\\'world\\n \\\'\')")"' \
# The trailing space is because the \n gets stripped off otherwise
testing 'quoting contexts nest' \
$'echo -n "$(echo "hello $(eval $\'echo -\\\\\\ne \\\'world\\n \\\'\')")"' \
'hello world\n ' '' ''
testing "\$'' suppresses variable expansion" \
$'echo $\'$(abc\'' '$(abc\n' '' ''

testing 'if; is a syntax error but if $EMPTY; is not' \
'if $NONE; then echo hello; fi' 'hello\n' '' ''
Expand Down
6 changes: 3 additions & 3 deletions toys/pending/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ static char *parse_word(char *start, int early)
else if (qq==254) return start+1;
else if (qq==255) toybuf[quote-1] = ')';
} else if (ii==')') quote--;
} else if (ii==qq) quote--; // matching end quote
} else if (ii==(qq&127)) quote--; // matching end quote
else if (qq!='\'') end--, ii = 0; // single quote claims everything
if (ii) continue; // fall through for other quote types

Expand All @@ -1146,11 +1146,11 @@ static char *parse_word(char *start, int early)

// \? $() ${} $[] ?() *() +() @() !()
else {
if (ii=='$' && -1!=(qq = stridx("({[", *end))) {
if (ii=='$' && qq != 0247 && -1!=(qq = stridx("({['", *end))) {
if (strstart(&end, "((")) {
end--;
toybuf[quote++] = 255;
} else toybuf[quote++] = ")}]"[qq];
} else toybuf[quote++] = ")}]\247"[qq]; // last is '+128
} else if (*end=='(' && strchr("?*+@!", ii)) toybuf[quote++] = ')';
else {
if (ii!='\\') end--;
Expand Down

0 comments on commit 97b5ea1

Please sign in to comment.