Fix assertion error trying to highlight cmdsubs inside unbalanced quotes

I initially put this logic + assertion in another function, where we
always get balanced quotes. Not for highlighting.
This commit is contained in:
Johannes Altmanninger 2021-10-31 13:44:38 +01:00
parent db377385f6
commit c94dec5d0e
2 changed files with 18 additions and 3 deletions

View file

@ -5296,6 +5296,14 @@ static void test_highlighting() {
{L"after\"", highlight_role_t::quote},
{L"param2", highlight_role_t::param},
});
highlight_tests.push_back({
{L"true", highlight_role_t::command},
{L"\"", highlight_role_t::error},
{L"unclosed quote", highlight_role_t::quote},
{L"$(", highlight_role_t::operat},
{L"true", highlight_role_t::command},
{L")", highlight_role_t::operat},
});
// Redirections substitutions.
highlight_tests.push_back({

View file

@ -275,9 +275,16 @@ int parse_util_locate_cmdsubst_range(const wcstring &str, size_t *inout_cursor_o
// quoted but the naïve next caller wouldn't know. Since next caller only cares about
// the next command substitution - (C) - and not about the B part, just advance the
// cursor to the closing quote.
const wchar_t *q_end = quote_end(bracket_range_end, L'"');
assert(q_end && "expect balanced quotes");
*inout_cursor_offset = 1 + q_end - buff;
if (auto *q_end = quote_end(bracket_range_end, L'"')) {
*inout_cursor_offset = 1 + q_end - buff;
} else {
if (accept_incomplete) {
// We want to skip quoted text, so if there is no closing quote, skip to the end.
*inout_cursor_offset = bracket_range_end + std::wcslen(bracket_range_end) - buff;
} else {
return -1;
}
}
}
return ret;