Skip to content

Commit

Permalink
regex-syntax: fix nest limit checker
Browse files Browse the repository at this point in the history
This commit fixes an embarrassing bug where the depth in the nest limit
checker was never decremented during postorder traversal, which means
long but shallow regexes would incorrectly trip the nest limit. We fix
that in this commit and add two regression tests.

Fixes #454
  • Loading branch information
BurntSushi committed Mar 10, 2018
1 parent 649762d commit a89220d
Show file tree
Hide file tree
Showing 3 changed files with 348 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
TBD
===
Bug gixes:

* [BUG #454](https://github.com/rust-lang/regex/pull/454):
Fix a bug in the nest limit checker being too aggressive.


0.2.7 (2018-03-07)
==================
This release includes a ground-up rewrite of the regex-syntax crate, which has
Expand Down
45 changes: 44 additions & 1 deletion regex-syntax/src/ast/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,7 @@ impl<'p, 's, P: Borrow<Parser>> NestLimiter<'p, 's, P> {
fn decrement_depth(&mut self) {
// Assuming the correctness of the visitor, this should never drop
// below 0.
self.depth.checked_sub(1).unwrap();
self.depth = self.depth.checked_sub(1).unwrap();
}
}

Expand Down Expand Up @@ -5254,4 +5254,47 @@ bar
],
})));
}

// This tests a bug fix where the nest limit checker wasn't decrementing
// its depth during post-traversal, which causes long regexes to trip
// the default limit too aggressively.
#[test]
fn regression_454_nest_too_big() {
let pattern = r#"
2(?:
[45]\d{3}|
7(?:
1[0-267]|
2[0-289]|
3[0-29]|
4[01]|
5[1-3]|
6[013]|
7[0178]|
91
)|
8(?:
0[125]|
[139][1-6]|
2[0157-9]|
41|
6[1-35]|
7[1-5]|
8[1-8]|
90
)|
9(?:
0[0-2]|
1[0-4]|
2[568]|
3[3-6]|
5[5-7]|
6[0167]|
7[15]|
8[0146-9]
)
)\d{4}
"#;
assert!(parser_nest_limit(pattern, 50).parse().is_ok());
}
}
296 changes: 296 additions & 0 deletions tests/crazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,299 @@ fn dfa_handles_pathological_case() {
};
assert!(re.is_match(text!(&*text)));
}

#[test]
fn nest_limit_makes_it_parse() {
use regex::RegexBuilder;

RegexBuilder::new(
r#"
2(?:
[45]\d{3}|
7(?:
1[0-267]|
2[0-289]|
3[0-29]|
4[01]|
5[1-3]|
6[013]|
7[0178]|
91
)|
8(?:
0[125]|
[139][1-6]|
2[0157-9]|
41|
6[1-35]|
7[1-5]|
8[1-8]|
90
)|
9(?:
0[0-2]|
1[0-4]|
2[568]|
3[3-6]|
5[5-7]|
6[0167]|
7[15]|
8[0146-9]
)
)\d{4}|
3(?:
12?[5-7]\d{2}|
0(?:
2(?:
[025-79]\d|
[348]\d{1,2}
)|
3(?:
[2-4]\d|
[56]\d?
)
)|
2(?:
1\d{2}|
2(?:
[12]\d|
[35]\d{1,2}|
4\d?
)
)|
3(?:
1\d{2}|
2(?:
[2356]\d|
4\d{1,2}
)
)|
4(?:
1\d{2}|
2(?:
2\d{1,2}|
[47]|
5\d{2}
)
)|
5(?:
1\d{2}|
29
)|
[67]1\d{2}|
8(?:
1\d{2}|
2(?:
2\d{2}|
3|
4\d
)
)
)\d{3}|
4(?:
0(?:
2(?:
[09]\d|
7
)|
33\d{2}
)|
1\d{3}|
2(?:
1\d{2}|
2(?:
[25]\d?|
[348]\d|
[67]\d{1,2}
)
)|
3(?:
1\d{2}(?:
\d{2}
)?|
2(?:
[045]\d|
[236-9]\d{1,2}
)|
32\d{2}
)|
4(?:
[18]\d{2}|
2(?:
[2-46]\d{2}|
3
)|
5[25]\d{2}
)|
5(?:
1\d{2}|
2(?:
3\d|
5
)
)|
6(?:
[18]\d{2}|
2(?:
3(?:
\d{2}
)?|
[46]\d{1,2}|
5\d{2}|
7\d
)|
5(?:
3\d?|
4\d|
[57]\d{1,2}|
6\d{2}|
8
)
)|
71\d{2}|
8(?:
[18]\d{2}|
23\d{2}|
54\d{2}
)|
9(?:
[18]\d{2}|
2[2-5]\d{2}|
53\d{1,2}
)
)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