From 1fc6bbcdc333e13b72a94cce53411d19dc71ca50 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Tue, 15 Mar 2022 01:43:44 +0000 Subject: [PATCH] LibJS: Stop generating switch case statements on block termination After we terminate a block (e.g. break, continue), we cannot generate anymore bytecode for the block. This caused us to crash with this example code: ``` a = 0; switch (a) { case 0: break; console.log("hello world"); } ``` Anything after a block terminating instruction is considered unreachable code, so we can safely skip any statements after it. --- Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 5c8be88eb0..cd2ac0f7a4 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -1627,6 +1627,8 @@ Bytecode::CodeGenerationErrorOr SwitchStatement::generate_bytecode(Bytecod generator.emit(js_undefined()); for (auto& statement : switch_case.children()) { TRY(statement.generate_bytecode(generator)); + if (generator.is_current_block_terminated()) + break; } if (!generator.is_current_block_terminated()) { auto next_block = current_block;