Skip to content

Commit

Permalink
Merge pull request #105 from rusty-ecma/fix/short-escaped-regex
Browse files Browse the repository at this point in the history
fix: index out of range in incomplete escape sequence in regex
  • Loading branch information
FreeMasen authored Aug 18, 2024
2 parents 31d1faa + ff85645 commit cf7ebbf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ress"
version = "0.11.6"
version = "0.11.7"
authors = ["Robert Masen <[email protected]>"]
description = "A scanner/tokenizer for JS files"
keywords = ["JavaScript", "parsing", "JS", "ES", "ECMA"]
Expand Down
17 changes: 15 additions & 2 deletions src/tokenizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<'a> Tokenizer<'a> {
idx: self.stream.idx,
msg: "new line in regex literal".to_string(),
});
} else {
} else if !self.stream.at_end() {
self.stream.skip_bytes(1);
}
} else if is_line_term(c) {
Expand All @@ -139,10 +139,14 @@ impl<'a> Tokenizer<'a> {
if end_of_body {
return self.gen_regex(start_len, body_idx);
}
log::debug!("Error at {}..{}", self.current_start, self.stream.idx);
Err(RawError {
msg: format!(
"unterminated regex at {}",
String::from_utf8_lossy(&self.stream.buffer[self.current_start..self.stream.idx])
String::from_utf8_lossy(
&self.stream.buffer
[(self.current_start.saturating_sub(start_len))..self.stream.idx]
)
),
idx: self.current_start,
})
Expand Down Expand Up @@ -2276,4 +2280,13 @@ mod test {
}
)
}

#[test]
#[should_panic = r#"unterminated regex at /\\"#]
fn two_slash_regex() {
let re = r#"/\"#;
let mut tokenizer = Tokenizer::new(re);
let _token = tokenizer.next(false).unwrap();
tokenizer.next_regex(1).unwrap();
}
}
3 changes: 2 additions & 1 deletion tests/snippets/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ fn regex_pattern() {
location,
token: Token::RegEx(re2),
..
} = scanner.next().unwrap().unwrap() else {
} = scanner.next().unwrap().unwrap()
else {
panic!("Expected regex");
};
assert_eq!(location.start.line, 1);
Expand Down

0 comments on commit cf7ebbf

Please sign in to comment.