Handle backslashes properly in locate_brackets_of_type

This needs to be rewritten, I'm pretty sure we have like 6 of these
kinds of ad-hoc "is this quoted" things lying around.

But for now, at least don't just check if the *previous* character was
a backslash.

Fixes #7685.
This commit is contained in:
Fabian Homborg 2021-02-05 22:00:31 +01:00
parent c8a91cb067
commit b5305ce3d3
2 changed files with 11 additions and 3 deletions

View File

@ -109,7 +109,7 @@ static int parse_util_locate_brackets_of_type(const wchar_t *in, wchar_t **begin
wchar_t close_type) {
// open_type is typically ( or [, and close type is the corresponding value.
wchar_t *pos;
wchar_t prev = 0;
bool escaped = false;
bool syntax_error = false;
int paran_count = 0;
@ -118,7 +118,7 @@ static int parse_util_locate_brackets_of_type(const wchar_t *in, wchar_t **begin
assert(in && "null parameter");
for (pos = const_cast<wchar_t *>(in); *pos; pos++) {
if (prev != '\\') {
if (!escaped) {
if (std::wcschr(L"\'\"", *pos)) {
wchar_t *q_end = quote_end(pos);
if (q_end && *q_end) {
@ -148,7 +148,11 @@ static int parse_util_locate_brackets_of_type(const wchar_t *in, wchar_t **begin
}
}
}
prev = *pos;
if (*pos == '\\') {
escaped = !escaped;
} else {
escaped = false;
}
}
syntax_error |= (paran_count < 0);

View File

@ -484,3 +484,7 @@ echo '-n art'
echo banana
# CHECK: -n art
# CHECK: banana
# This used to be a parse error - #7685.
echo (echo hello\\)
# CHECK: hello\