Skip to content

Commit 7b1599f

Browse files
committed
syntax: fix counted repetition bug
This fixes a bug where the HIR translator would panic on regexes such as `(?i){1}` since it assumes that every repetition operator has a valid sub-expression, and `(?i)` is not actually a sub-expression (but is more like a directive instead). Previously, we fixed this same bug for *uncounted* repetitions in commit 17764ff (for bug #465), but we did not fix it for counted repetitions. We apply the same fix here. Fixes #555
1 parent 67fca6c commit 7b1599f

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Performance improvements:
1010

1111
Bug fixes:
1212

13+
* [BUG #555](https://github.com/rust-lang/regex/issues/555):
14+
Fix a bug where the parser would panic on patterns like `(?m){1,1}`.
1315
* [BUG #557](https://github.com/rust-lang/regex/issues/557):
1416
Fix a bug where captures could lead to an incorrect match.
1517

regex-syntax/src/ast/parse.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,13 @@ impl<'s, P: Borrow<Parser>> ParserI<'s, P> {
11001100
ast::ErrorKind::RepetitionMissing,
11011101
)),
11021102
};
1103+
match ast {
1104+
Ast::Empty(_) | Ast::Flags(_) => return Err(self.error(
1105+
self.span(),
1106+
ast::ErrorKind::RepetitionMissing,
1107+
)),
1108+
_ => {}
1109+
}
11031110
if !self.bump_and_bump_space() {
11041111
return Err(self.error(
11051112
Span::new(start, self.pos()),
@@ -3124,6 +3131,18 @@ bar
31243131
ast: Box::new(lit('a', 0)),
31253132
})));
31263133

3134+
assert_eq!(
3135+
parser(r"(?i){0}").parse().unwrap_err(),
3136+
TestError {
3137+
span: span(4..4),
3138+
kind: ast::ErrorKind::RepetitionMissing,
3139+
});
3140+
assert_eq!(
3141+
parser(r"(?m){1,1}").parse().unwrap_err(),
3142+
TestError {
3143+
span: span(4..4),
3144+
kind: ast::ErrorKind::RepetitionMissing,
3145+
});
31273146
assert_eq!(
31283147
parser(r"a{").parse().unwrap_err(),
31293148
TestError {

tests/regression.rs

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ fn regression_many_repeat_stack_overflow() {
1414
assert_eq!(vec![(0, 1)], findall!(re, "a"));
1515
}
1616

17+
// See: https://github.com/rust-lang/regex/issues/555
18+
#[test]
19+
fn regression_invalid_repetition_expr() {
20+
assert!(regex_new!("(?m){1,1}").is_err());
21+
}
22+
1723
// See: https://github.com/rust-lang/regex/issues/75
1824
mat!(regression_unsorted_binary_search_1, r"(?i)[a_]+", "A_", Some((0, 2)));
1925
mat!(regression_unsorted_binary_search_2, r"(?i)[A_]+", "a_", Some((0, 2)));

0 commit comments

Comments
 (0)