From a9eab38d88ce85ca0f5d40d818a1fa6d92c68ca6 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Thu, 9 Jan 2020 00:34:35 +1100 Subject: [PATCH] Ensure JS RegExp does not contain line terminators --- src/err.rs | 4 ++++ src/unit/script/js.rs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/err.rs b/src/err.rs index 62ff3db..3f9b865 100644 --- a/src/err.rs +++ b/src/err.rs @@ -3,6 +3,7 @@ pub enum ErrorType { NoSpaceBeforeAttr, UnterminatedCssString, UnterminatedJsString, + UnterminatedJsRegExp, CharNotFound { need: u8, got: u8 }, MatchNotFound(&'static [u8]), NotFound(&'static str), @@ -25,6 +26,9 @@ impl ErrorType { ErrorType::UnterminatedJsString => { format!("Unterminated JavaScript string.") } + ErrorType::UnterminatedJsRegExp => { + format!("Unterminated JavaScript regular expression.") + } ErrorType::CharNotFound { need, got } => { format!("Expected {} (U+{:X}), got {} (U+{:X}).", need as char, need, got as char, got) } diff --git a/src/unit/script/js.rs b/src/unit/script/js.rs index aa2798f..d2f5642 100644 --- a/src/unit/script/js.rs +++ b/src/unit/script/js.rs @@ -126,6 +126,11 @@ fn parse_literal_regex(proc: &mut Processor) -> ProcessingResult<()> { loop { let c = proc.accept()?; + // We've already accepted char, so we can't use proc.match_line_terminator. + // Line terminator cannot be escaped and is always invalid in a RegExp literal. + if c == b'\r' || c == b'\n' { + return Err(ErrorType::UnterminatedJsRegExp); + }; if c == b'\\' { // If already escaping, then ignore backslash (interpret literally) and continue.