Skip to content

Commit

Permalink
LibRegex: Generate a 'Compare' op for empty character classes
Browse files Browse the repository at this point in the history
Otherwise it would match zero-length strings.
Fixes SerenityOS#6256.
  • Loading branch information
alimpfard authored and awesomekling committed Apr 12, 2021
1 parent 7c98a6b commit 5a14f7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,15 @@ test("brace quantifier with invalid contents", () => {
expect(res.length).toBe(1);
expect(res[0]).toBe("{{lit-746579221856449}}");
});

// #6256
test("empty character class semantics", () => {
// Should not match zero-length strings.
let res = /[]/.exec("");
expect(res).toBe(null);

// Inverse form, should match anything.
res = /[^]/.exec("x");
expect(res.length).toBe(1);
expect(res[0]).toBe("x");
});
3 changes: 3 additions & 0 deletions Userland/Libraries/LibRegex/RegexParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,9 @@ bool ECMA262Parser::parse_character_class(ByteCode& stack, size_t& match_length_

if (match(TokenType::RightBracket)) {
consume();
// Should only have at most an 'Inverse'
VERIFY(compares.size() <= 1);
stack.insert_bytecode_compare_values(move(compares));
return true;
}

Expand Down

0 comments on commit 5a14f7e

Please sign in to comment.