mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 18:10:56 +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();
|
NonnullRefPtr<NewExpression> parse_new_expression();
|
||||||
RefPtr<FunctionExpression> try_parse_arrow_function_expression(bool expect_parens);
|
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:
|
private:
|
||||||
int operator_precedence(TokenType) const;
|
int operator_precedence(TokenType) const;
|
||||||
|
|
|
@ -374,7 +374,11 @@ JS::Interpreter& Document::interpreter()
|
||||||
|
|
||||||
JS::Value Document::run_javascript(const StringView& source)
|
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:";
|
dbg() << "Document::run_javascript('" << source << "') will run:";
|
||||||
program->dump(0);
|
program->dump(0);
|
||||||
return document().interpreter().run(*program);
|
return document().interpreter().run(*program);
|
||||||
|
|
|
@ -59,7 +59,11 @@ void HTMLScriptElement::children_changed()
|
||||||
if (source.is_empty())
|
if (source.is_empty())
|
||||||
return;
|
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);
|
document().interpreter().run(*program);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +94,11 @@ void HTMLScriptElement::inserted_into(Node& new_parent)
|
||||||
return;
|
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);
|
document().interpreter().run(*program);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -328,9 +328,14 @@ JS::Value ReplObject::load_file(JS::Interpreter& interpreter)
|
||||||
} else {
|
} else {
|
||||||
source = file_contents;
|
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)
|
if (dump_ast)
|
||||||
program->dump(0);
|
program->dump(0);
|
||||||
|
|
||||||
|
if (parser.has_errors())
|
||||||
|
continue;
|
||||||
|
|
||||||
interpreter.run(*program);
|
interpreter.run(*program);
|
||||||
if (print_last_result)
|
if (print_last_result)
|
||||||
print(interpreter.last_value());
|
print(interpreter.last_value());
|
||||||
|
@ -345,10 +350,16 @@ void repl(JS::Interpreter& interpreter)
|
||||||
if (piece.is_empty())
|
if (piece.is_empty())
|
||||||
continue;
|
continue;
|
||||||
repl_statements.append(piece);
|
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)
|
if (dump_ast)
|
||||||
program->dump(0);
|
program->dump(0);
|
||||||
|
|
||||||
|
if (parser.has_errors()) {
|
||||||
|
printf("Parse error\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
interpreter.run(*program);
|
interpreter.run(*program);
|
||||||
if (interpreter.exception()) {
|
if (interpreter.exception()) {
|
||||||
printf("Uncaught exception: ");
|
printf("Uncaught exception: ");
|
||||||
|
@ -634,11 +645,17 @@ int main(int argc, char** argv)
|
||||||
} else {
|
} else {
|
||||||
source = file_contents;
|
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)
|
if (dump_ast)
|
||||||
program->dump(0);
|
program->dump(0);
|
||||||
|
|
||||||
|
if (parser.has_errors()) {
|
||||||
|
printf("Parse Error\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
auto result = interpreter->run(*program);
|
auto result = interpreter->run(*program);
|
||||||
|
|
||||||
if (interpreter->exception()) {
|
if (interpreter->exception()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue