1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:48:12 +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:
AnotherTest 2020-11-30 17:46:53 +03:30 committed by Andreas Kling
parent 3bda458735
commit 6394720c87
2 changed files with 33 additions and 19 deletions

View file

@ -474,27 +474,33 @@ TEST_CASE(simple_period_end_benchmark)
TEST_CASE(ECMA262_parse)
{
constexpr const char* patterns[] {
"^hello.$",
"^(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.
struct _test {
const char* pattern;
regex::Error expected_error { regex::Error::NoError };
};
for (auto& pattern : patterns) {
Regex<ECMA262> re(pattern);
EXPECT_EQ(re.parser_result.error, Error::NoError);
constexpr _test tests[] {
{ "^hello.$" },
{ "^(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
dbg() << "\n";
RegexDebug regex_dbg(stderr);