From 92dbba6c5d4b0d5e7f2e9d6efb09fc733ee83184 Mon Sep 17 00:00:00 2001 From: Addison Crump Date: Tue, 8 Mar 2022 18:17:15 +0000 Subject: [PATCH] Prevent breaks without loop or switch from causing panics (#1860) This PR changes the following: - Replaces a panic with a syntax error when a break is used outside of a loop or switch - Adds a test for that --- boa_engine/src/bytecompiler.rs | 6 +++++- boa_engine/src/tests.rs | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/boa_engine/src/bytecompiler.rs b/boa_engine/src/bytecompiler.rs index ad709525e5..3dd8cac706 100644 --- a/boa_engine/src/bytecompiler.rs +++ b/boa_engine/src/bytecompiler.rs @@ -1596,7 +1596,11 @@ impl<'b> ByteCompiler<'b> { } else { self.jump_info .last_mut() - .expect("no jump information found") + .ok_or_else(|| { + self.context.construct_syntax_error( + "unlabeled break must be inside loop or switch", + ) + })? .breaks .push(label); } diff --git a/boa_engine/src/tests.rs b/boa_engine/src/tests.rs index a8ee0730b3..5500d3d34e 100644 --- a/boa_engine/src/tests.rs +++ b/boa_engine/src/tests.rs @@ -442,6 +442,20 @@ fn test_invalid_break_target() { assert!(Context::default().eval(src).is_err()); } +#[test] +fn test_invalid_break() { + let mut context = Context::default(); + let src = r#" + break; + "#; + + let string = forward(&mut context, src); + assert_eq!( + string, + "Uncaught \"SyntaxError\": \"unlabeled break must be inside loop or switch\"" + ); +} + #[test] fn test_invalid_continue_target() { let mut context = Context::default();