Skip to content

Commit

Permalink
LibJS: Fix start position of multi-line tokens
Browse files Browse the repository at this point in the history
This broke in case of unterminated regular expressions, causing goofy location
numbers, and 'source_location_hint' to eat up all memory:

Unexpected token UnterminatedRegexLiteral. Expected statement (line: 2, column: 4294967292)
  • Loading branch information
BenWiederhake authored and awesomekling committed Sep 11, 2020
1 parent 0d3a8d5 commit 5d3c437
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
28 changes: 15 additions & 13 deletions Libraries/LibJS/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,17 @@ bool Lexer::slash_means_division() const
{
auto type = m_current_token.type();
return type == TokenType::BigIntLiteral
|| type == TokenType::BoolLiteral
|| type == TokenType::BracketClose
|| type == TokenType::CurlyClose
|| type == TokenType::Identifier
|| type == TokenType::NullLiteral
|| type == TokenType::NumericLiteral
|| type == TokenType::ParenClose
|| type == TokenType::RegexLiteral
|| type == TokenType::StringLiteral
|| type == TokenType::TemplateLiteralEnd
|| type == TokenType::This;
|| type == TokenType::BoolLiteral
|| type == TokenType::BracketClose
|| type == TokenType::CurlyClose
|| type == TokenType::Identifier
|| type == TokenType::NullLiteral
|| type == TokenType::NumericLiteral
|| type == TokenType::ParenClose
|| type == TokenType::RegexLiteral
|| type == TokenType::StringLiteral
|| type == TokenType::TemplateLiteralEnd
|| type == TokenType::This;
}

Token Lexer::next()
Expand Down Expand Up @@ -292,6 +292,8 @@ Token Lexer::next()
}

size_t value_start = m_position;
size_t value_start_line_number = m_line_number;
size_t value_start_column_number = m_line_column;
auto token_type = TokenType::Invalid;

if (m_current_token.type() == TokenType::RegexLiteral && !is_eof() && isalpha(m_current_char)) {
Expand Down Expand Up @@ -511,8 +513,8 @@ Token Lexer::next()
token_type,
m_source.substring_view(trivia_start - 1, value_start - trivia_start),
m_source.substring_view(value_start - 1, m_position - value_start),
m_line_number,
m_line_column - m_position + value_start);
value_start_line_number,
value_start_column_number);

return m_current_token;
}
Expand Down
10 changes: 8 additions & 2 deletions Meta/Lagom/Fuzzers/FuzzJs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
auto lexer = JS::Lexer(js);
auto parser = JS::Parser(lexer);
parser.parse_program();
if (parser.has_errors())
parser.print_errors();
if (parser.has_errors()) {
for (auto& error : parser.errors()) {
if (error.line >= 100000 || error.column >= 100000) {
fprintf(stderr, "%s\n", error.to_string().characters());
ASSERT_NOT_REACHED();
}
}
}
return 0;
}

0 comments on commit 5d3c437

Please sign in to comment.