From 98945c8cb36eaaad293015eca1f47ffac7cd4f75 Mon Sep 17 00:00:00 2001 From: Jason Williams <936006+jasonwilliams@users.noreply.github.com> Date: Sat, 2 Jan 2021 21:57:10 +0000 Subject: [PATCH] wrap the regress panic for now (#1027) --- boa/src/builtins/regexp/mod.rs | 13 ++++++++++--- boa/src/builtins/regexp/tests.rs | 9 +++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/boa/src/builtins/regexp/mod.rs b/boa/src/builtins/regexp/mod.rs index 070695d20e3..91275cad93e 100644 --- a/boa/src/builtins/regexp/mod.rs +++ b/boa/src/builtins/regexp/mod.rs @@ -98,7 +98,7 @@ impl RegExp { pub(crate) const LENGTH: usize = 2; /// Create a new `RegExp` - pub(crate) fn constructor(this: &Value, args: &[Value], _: &mut Context) -> Result { + pub(crate) fn constructor(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let arg = args.get(0).ok_or_else(Value::undefined)?; let (regex_body, mut regex_flags) = match arg { @@ -161,8 +161,15 @@ impl RegExp { sorted_flags.push('y'); } - let matcher = Regex::with_flags(®ex_body, sorted_flags.as_str()) - .expect("failed to create matcher"); + let matcher = match Regex::with_flags(®ex_body, sorted_flags.as_str()) { + Err(error) => { + return Err( + ctx.construct_syntax_error(format!("failed to create matcher: {}", error.text)) + ); + } + Ok(val) => val, + }; + let regexp = RegExp { matcher, use_last_index: global || sticky, diff --git a/boa/src/builtins/regexp/tests.rs b/boa/src/builtins/regexp/tests.rs index a07041ca29b..a9d014bb87b 100644 --- a/boa/src/builtins/regexp/tests.rs +++ b/boa/src/builtins/regexp/tests.rs @@ -98,3 +98,12 @@ fn to_string() { ); assert_eq!(forward(&mut context, "/\\n/g.toString()"), "\"/\\n/g\""); } + +#[test] +fn no_panic_on_invalid_character_escape() { + let mut context = Context::new(); + + // This used to panic, we now return an error + // The line below should not cause Boa to panic + forward(&mut context, r"const a = /,\;/"); +}