Skip to content

Commit

Permalink
LibJS: Do not execute scripts with parse errors
Browse files Browse the repository at this point in the history
This adds missing checks in several LibJS consumers.
  • Loading branch information
sunverwerth authored and awesomekling committed Apr 13, 2020
1 parent 50b6b6e commit 984c290
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Libraries/LibJS/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Parser {
NonnullRefPtr<NewExpression> parse_new_expression();
RefPtr<FunctionExpression> try_parse_arrow_function_expression(bool expect_parens);

bool has_errors() const { return m_parser_state.m_has_errors; }
bool has_errors() const { return m_parser_state.m_lexer.has_errors() || m_parser_state.m_has_errors; }

private:
int operator_precedence(TokenType) const;
Expand Down
6 changes: 5 additions & 1 deletion Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,11 @@ JS::Interpreter& Document::interpreter()

JS::Value Document::run_javascript(const StringView& source)
{
auto program = JS::Parser(JS::Lexer(source)).parse_program();
auto parser = JS::Parser(JS::Lexer(source));
auto program = parser.parse_program();
if (parser.has_errors()) {
return JS::js_undefined();
}
dbg() << "Document::run_javascript('" << source << "') will run:";
program->dump(0);
return document().interpreter().run(*program);
Expand Down
12 changes: 10 additions & 2 deletions Libraries/LibWeb/DOM/HTMLScriptElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ void HTMLScriptElement::children_changed()
if (source.is_empty())
return;

auto program = JS::Parser(JS::Lexer(source)).parse_program();
auto parser = JS::Parser(JS::Lexer(source));
auto program = parser.parse_program();
if (parser.has_errors())
return;

document().interpreter().run(*program);
}

Expand Down Expand Up @@ -90,7 +94,11 @@ void HTMLScriptElement::inserted_into(Node& new_parent)
return;
}

auto program = JS::Parser(JS::Lexer(source)).parse_program();
auto parser = JS::Parser(JS::Lexer(source));
auto program = parser.parse_program();
if (parser.has_errors())
return;

document().interpreter().run(*program);
}

Expand Down
23 changes: 20 additions & 3 deletions Userland/js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,14 @@ JS::Value ReplObject::load_file(JS::Interpreter& interpreter)
} else {
source = file_contents;
}
auto program = JS::Parser(JS::Lexer(source)).parse_program();
auto parser = JS::Parser(JS::Lexer(source));
auto program = parser.parse_program();
if (dump_ast)
program->dump(0);

if (parser.has_errors())
continue;

interpreter.run(*program);
if (print_last_result)
print(interpreter.last_value());
Expand All @@ -345,10 +350,16 @@ void repl(JS::Interpreter& interpreter)
if (piece.is_empty())
continue;
repl_statements.append(piece);
auto program = JS::Parser(JS::Lexer(piece)).parse_program();
auto parser = JS::Parser(JS::Lexer(piece));
auto program = parser.parse_program();
if (dump_ast)
program->dump(0);

if (parser.has_errors()) {
printf("Parse error\n");
continue;
}

interpreter.run(*program);
if (interpreter.exception()) {
printf("Uncaught exception: ");
Expand Down Expand Up @@ -634,11 +645,17 @@ int main(int argc, char** argv)
} else {
source = file_contents;
}
auto program = JS::Parser(JS::Lexer(source)).parse_program();
auto parser = JS::Parser(JS::Lexer(source));
auto program = parser.parse_program();

if (dump_ast)
program->dump(0);

if (parser.has_errors()) {
printf("Parse Error\n");
return 1;
}

auto result = interpreter->run(*program);

if (interpreter->exception()) {
Expand Down

0 comments on commit 984c290

Please sign in to comment.