mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 20:07:34 +00:00
LibRegex: Don't try to consume the escaped character if at EOF
Fixes assert on e.g. `new RegExp("\\")`
This commit is contained in:
parent
3bda458735
commit
6394720c87
2 changed files with 33 additions and 19 deletions
|
@ -1114,6 +1114,9 @@ bool ECMA262Parser::parse_atom_escape(ByteCode& stack, size_t& match_length_mini
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (done())
|
||||||
|
return set_error(Error::InvalidTrailingEscape);
|
||||||
|
|
||||||
bool negate = false;
|
bool negate = false;
|
||||||
auto ch = parse_character_class_escape(negate);
|
auto ch = parse_character_class_escape(negate);
|
||||||
if (!ch.has_value()) {
|
if (!ch.has_value()) {
|
||||||
|
@ -1211,6 +1214,11 @@ bool ECMA262Parser::parse_nonempty_class_ranges(Vector<CompareTypeAndValuePair>&
|
||||||
}
|
}
|
||||||
|
|
||||||
if (try_skip("\\")) {
|
if (try_skip("\\")) {
|
||||||
|
if (done()) {
|
||||||
|
set_error(Error::InvalidTrailingEscape);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
if (try_skip("f"))
|
if (try_skip("f"))
|
||||||
return { { .code_point = '\f', .is_character_class = false } };
|
return { { .code_point = '\f', .is_character_class = false } };
|
||||||
if (try_skip("n"))
|
if (try_skip("n"))
|
||||||
|
|
|
@ -474,27 +474,33 @@ TEST_CASE(simple_period_end_benchmark)
|
||||||
|
|
||||||
TEST_CASE(ECMA262_parse)
|
TEST_CASE(ECMA262_parse)
|
||||||
{
|
{
|
||||||
constexpr const char* patterns[] {
|
struct _test {
|
||||||
"^hello.$",
|
const char* pattern;
|
||||||
"^(hello.)$",
|
regex::Error expected_error { regex::Error::NoError };
|
||||||
"^h{0,1}ello.$",
|
|
||||||
"^hello\\W$",
|
|
||||||
"^hell\\w.$",
|
|
||||||
"^hell\\x6f1$", // ^hello1$
|
|
||||||
"^hel(?:l\\w).$",
|
|
||||||
"^hel(?<LO>l\\w).$",
|
|
||||||
"^[-a-zA-Z\\w\\s]+$",
|
|
||||||
"\\bhello\\B",
|
|
||||||
"^[\\w+/_-]+[=]{0,2}$", // #4189
|
|
||||||
"^(?:[^<]*(<[\\w\\W]+>)[^>]*$|#([\\w\\-]*)$)", // #4189
|
|
||||||
"\\/", // #4189
|
|
||||||
",/=-:", // #4243
|
|
||||||
"\\x", // Even invalid escapes are allowed if ~unicode.
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (auto& pattern : patterns) {
|
constexpr _test tests[] {
|
||||||
Regex<ECMA262> re(pattern);
|
{ "^hello.$" },
|
||||||
EXPECT_EQ(re.parser_result.error, Error::NoError);
|
{ "^(hello.)$" },
|
||||||
|
{ "^h{0,1}ello.$" },
|
||||||
|
{ "^hello\\W$" },
|
||||||
|
{ "^hell\\w.$" },
|
||||||
|
{ "^hell\\x6f1$" }, // ^hello1$
|
||||||
|
{ "^hel(?:l\\w).$" },
|
||||||
|
{ "^hel(?<LO>l\\w).$" },
|
||||||
|
{ "^[-a-zA-Z\\w\\s]+$" },
|
||||||
|
{ "\\bhello\\B" },
|
||||||
|
{ "^[\\w+/_-]+[=]{0,2}$" }, // #4189
|
||||||
|
{ "^(?:[^<]*(<[\\w\\W]+>)[^>]*$|#([\\w\\-]*)$)" }, // #4189
|
||||||
|
{ "\\/" }, // #4189
|
||||||
|
{ ",/=-:" }, // #4243
|
||||||
|
{ "\\x" }, // Even invalid escapes are allowed if ~unicode.
|
||||||
|
{ "\\", regex::Error::InvalidTrailingEscape },
|
||||||
|
};
|
||||||
|
|
||||||
|
for (auto& test : tests) {
|
||||||
|
Regex<ECMA262> re(test.pattern);
|
||||||
|
EXPECT_EQ(re.parser_result.error, test.expected_error);
|
||||||
#ifdef REGEX_DEBUG
|
#ifdef REGEX_DEBUG
|
||||||
dbg() << "\n";
|
dbg() << "\n";
|
||||||
RegexDebug regex_dbg(stderr);
|
RegexDebug regex_dbg(stderr);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue