diff --git a/Cargo.lock b/Cargo.lock index 8f3de16a0d..bef5c59f24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -300,6 +300,7 @@ dependencies = [ "fast-float", "num-bigint", "num-traits", + "regress", "rustc-hash", ] diff --git a/boa_parser/Cargo.toml b/boa_parser/Cargo.toml index a4d5601ce0..f08c366de2 100644 --- a/boa_parser/Cargo.toml +++ b/boa_parser/Cargo.toml @@ -21,3 +21,4 @@ fast-float = "0.2.0" num-traits = "0.2.15" bitflags = "1.3.2" num-bigint = "0.4.3" +regress = "0.4.1" diff --git a/boa_parser/src/lexer/regex.rs b/boa_parser/src/lexer/regex.rs index 41d6270dc4..d416b853a4 100644 --- a/boa_parser/src/lexer/regex.rs +++ b/boa_parser/src/lexer/regex.rs @@ -5,6 +5,7 @@ use bitflags::bitflags; use boa_ast::Position; use boa_interner::{Interner, Sym}; use boa_profiler::Profiler; +use regress::Regex; use std::{ io::{self, ErrorKind, Read}, str::{self, FromStr}, @@ -120,6 +121,13 @@ impl Tokenizer for RegexLiteral { let flags_str = unsafe { str::from_utf8_unchecked(flags.as_slice()) }; if let Ok(body_str) = str::from_utf8(body.as_slice()) { + if let Err(error) = Regex::with_flags(body_str, flags_str) { + return Err(Error::Syntax( + format!("Invalid regular expression literal: {error}").into(), + start_pos, + )); + } + Ok(Token::new( TokenKind::regular_expression_literal( interner.get_or_intern(body_str),