Skip to content

Commit

Permalink
fix: index out of range in incomplete escape sequence in regex
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeMasen committed Aug 18, 2024
1 parent 31d1faa commit bd67de0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
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 bd67de0

Please sign in to comment.