Skip to content

Commit

Permalink
dfa: fix bug in how the reverse DFA is called
Browse files Browse the repository at this point in the history
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
  • Loading branch information
BurntSushi committed Mar 24, 2023
1 parent 32fed94 commit e0539cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/exec.rs
Expand Up @@ -459,7 +459,7 @@ impl<'c> RegularExpression for ExecNoSync<'c> {
self.cache.value(),
true,
&text[start..],
text.len(),
text.len() - start,
) {
dfa::Result::Match(_) => Some(text.len()),
dfa::Result::NoMatch(_) => None,
Expand Down Expand Up @@ -511,7 +511,7 @@ impl<'c> RegularExpression for ExecNoSync<'c> {
self.cache.value(),
true,
&text[start..],
text.len(),
text.len() - start,
) {
dfa::Result::Match(_) => true,
dfa::Result::NoMatch(_) => false,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_default.rs
Expand Up @@ -220,3 +220,13 @@ fn empty_alt_regex_fails() {
let result = Regex::new(r"(?:|){4294967295}");
assert!(result.is_err());
}

// Regression test for: https://github.com/rust-lang/regex/issues/969
#[test]
fn regression_i969() {
use regex::Regex;

let re = Regex::new(r"c.*d\z").unwrap();
assert_eq!(Some(6), re.shortest_match_at("ababcd", 4));
assert_eq!(Some(6), re.find_at("ababcd", 4).map(|m| m.end()));
}

0 comments on commit e0539cd

Please sign in to comment.