Browse Source

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
pull/1913/head
Addison Crump 3 years ago
parent
commit
92dbba6c5d
  1. 6
      boa_engine/src/bytecompiler.rs
  2. 14
      boa_engine/src/tests.rs

6
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);
}

14
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();

Loading…
Cancel
Save