summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/AST.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-10-23 00:40:14 +0100
committerAndreas Kling <kling@serenityos.org>2020-10-23 19:06:57 +0200
commit82ac936a9d50ac888b59b0f3985dabac2a1bdf6c (patch)
treedf44a46a2f93f322a5e20a0bb10fe12735bfb0e9 /Libraries/LibJS/AST.cpp
parenta19d8aade4862556db8b2e653b05f6a4b328e18c (diff)
downloadserenity-82ac936a9d50ac888b59b0f3985dabac2a1bdf6c.zip
LibJS: Check for exception after executing (do)while test expression
Otherwise we crash the interpreter when an exception is thrown during evaluation of the while or do/while test expression - which is easily caused by a ReferenceError - e.g.: while (someUndefinedVariable) { // ... }
Diffstat (limited to 'Libraries/LibJS/AST.cpp')
-rw-r--r--Libraries/LibJS/AST.cpp14
1 files changed, 11 insertions, 3 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp
index 605e1a257f..fec8aeba07 100644
--- a/Libraries/LibJS/AST.cpp
+++ b/Libraries/LibJS/AST.cpp
@@ -258,9 +258,12 @@ Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object
Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
{
Value last_value = js_undefined();
- while (m_test->execute(interpreter, global_object).to_boolean()) {
+ for (;;) {
+ auto test_result = m_test->execute(interpreter, global_object);
if (interpreter.exception())
return {};
+ if (!test_result.to_boolean())
+ break;
last_value = interpreter.execute_statement(global_object, *m_body);
if (interpreter.exception())
return {};
@@ -282,7 +285,7 @@ Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_obj
Value DoWhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
{
Value last_value = js_undefined();
- do {
+ for (;;) {
if (interpreter.exception())
return {};
last_value = interpreter.execute_statement(global_object, *m_body);
@@ -298,7 +301,12 @@ Value DoWhileStatement::execute(Interpreter& interpreter, GlobalObject& global_o
return last_value;
}
}
- } while (m_test->execute(interpreter, global_object).to_boolean());
+ auto test_result = m_test->execute(interpreter, global_object);
+ if (interpreter.exception())
+ return {};
+ if (!test_result.to_boolean())
+ break;
+ }
return last_value;
}