Skip to content

Commit d94f955

Browse files
committed
dfa: fix bug in how the reverse DFA is called
In turns out that in *some* calls to Fsm::reverse, we were passing an incorrect start offset. Namely, the haystack we pass is sub-sliced at `&text[start..]`, but in some places, we were passing `text.len()` as the start offset of the reverse search. But of course, it should be `text.len() - start`. This was indeed the case in most places, but it looks like it needed to be corrected in two additional places. I've also added this test to regex-automata's set of regression tests and can confirm that it doesn't happen there. (regex-automata is far more principled about handling offsets like this.) Fixes #969
1 parent 32fed94 commit d94f955

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/exec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'c> RegularExpression for ExecNoSync<'c> {
459459
self.cache.value(),
460460
true,
461461
&text[start..],
462-
text.len(),
462+
text.len() - start,
463463
) {
464464
dfa::Result::Match(_) => Some(text.len()),
465465
dfa::Result::NoMatch(_) => None,
@@ -511,7 +511,7 @@ impl<'c> RegularExpression for ExecNoSync<'c> {
511511
self.cache.value(),
512512
true,
513513
&text[start..],
514-
text.len(),
514+
text.len() - start,
515515
) {
516516
dfa::Result::Match(_) => true,
517517
dfa::Result::NoMatch(_) => false,

tests/test_default.rs

+10
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,13 @@ fn empty_alt_regex_fails() {
220220
let result = Regex::new(r"(?:|){4294967295}");
221221
assert!(result.is_err());
222222
}
223+
224+
// Regression test for: https://github.com/rust-lang/regex/issues/969
225+
#[test]
226+
fn regression_i969() {
227+
use regex::Regex;
228+
229+
let re = Regex::new(r"c.*d\z").unwrap();
230+
assert_eq!(Some(6), re.shortest_match_at("ababcd", 4));
231+
assert_eq!(Some(6), re.find_at("ababcd", 4).map(|m| m.end()));
232+
}

0 commit comments

Comments
 (0)