Skip to content

Commit 7b3cf68

Browse files
committed
fix: index out of range in incomplete escape sequence in regex
1 parent f5ac8f3 commit 7b3cf68

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/tokenizer/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<'a> Tokenizer<'a> {
116116
idx: self.stream.idx,
117117
msg: "new line in regex literal".to_string(),
118118
});
119-
} else {
119+
} else if !self.stream.at_end() {
120120
self.stream.skip_bytes(1);
121121
}
122122
} else if is_line_term(c) {
@@ -139,10 +139,14 @@ impl<'a> Tokenizer<'a> {
139139
if end_of_body {
140140
return self.gen_regex(start_len, body_idx);
141141
}
142+
log::debug!("Error at {}..{}", self.current_start, self.stream.idx);
142143
Err(RawError {
143144
msg: format!(
144145
"unterminated regex at {}",
145-
String::from_utf8_lossy(&self.stream.buffer[self.current_start..self.stream.idx])
146+
String::from_utf8_lossy(
147+
&self.stream.buffer
148+
[(self.current_start.saturating_sub(start_len))..self.stream.idx]
149+
)
146150
),
147151
idx: self.current_start,
148152
})
@@ -2326,4 +2330,13 @@ mod test {
23262330
}
23272331
)
23282332
}
2333+
2334+
#[test]
2335+
#[should_panic = r#"unterminated regex at /\\"#]
2336+
fn two_slash_regex() {
2337+
let re = r#"/\"#;
2338+
let mut tokenizer = Tokenizer::new(re);
2339+
let _token = tokenizer.next(false).unwrap();
2340+
tokenizer.next_regex(1).unwrap();
2341+
}
23292342
}

0 commit comments

Comments
 (0)