mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 09:34:57 +09:00
LibJS: Do not execute scripts with parse errors
This adds missing checks in several LibJS consumers.
This commit is contained in:
parent
50b6b6ef86
commit
984c290ec0
Notes:
sideshowbarker
2024-07-19 07:38:28 +09:00
Author: https://github.com/sunverwerth
Commit: 984c290ec0
Pull-request: https://github.com/SerenityOS/serenity/pull/1775
4 changed files with 36 additions and 7 deletions
|
@ -72,7 +72,7 @@ public:
|
|||
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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
@ -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: ");
|
||||
|
@ -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()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue