Skip to content

Commit bc76a30

Browse files
committed
regex: fix -w when regex can match empty string
This is a weird bug where our optimization for handling -w more quickly than we would otherwise failed. In particular, if the original regex can match the empty string, then our word boundary detection would produce invalid indices to the start the next search at. We "fix" it by simply bailing when the indices are known to be incorrect. This wasn't a problem in a previous release since ripgrep 13 tweaked how word boundaries are detected in commit efd9cfb. Fixes #1891
1 parent 5e81c60 commit bc76a30

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎crates/matcher/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Match {
116116
/// This method panics if `start > self.end`.
117117
#[inline]
118118
pub fn with_start(&self, start: usize) -> Match {
119-
assert!(start <= self.end);
119+
assert!(start <= self.end, "{} is not <= {}", start, self.end);
120120
Match { start, ..*self }
121121
}
122122

@@ -128,7 +128,7 @@ impl Match {
128128
/// This method panics if `self.start > end`.
129129
#[inline]
130130
pub fn with_end(&self, end: usize) -> Match {
131-
assert!(self.start <= end);
131+
assert!(self.start <= end, "{} is not <= {}", self.start, end);
132132
Match { end, ..*self }
133133
}
134134

‎crates/regex/src/word.rs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,15 @@ impl WordMatcher {
111111
}
112112
let (_, slen) = bstr::decode_utf8(&haystack[cand]);
113113
let (_, elen) = bstr::decode_last_utf8(&haystack[cand]);
114-
cand =
115-
cand.with_start(cand.start() + slen).with_end(cand.end() - elen);
114+
let new_start = cand.start() + slen;
115+
let new_end = cand.end() - elen;
116+
// This occurs the original regex can match the empty string. In this
117+
// case, just bail instead of trying to get it right here since it's
118+
// likely a pathological case.
119+
if new_start > new_end {
120+
return Err(());
121+
}
122+
cand = cand.with_start(new_start).with_end(new_end);
116123
if self.original.is_match(&haystack[cand]) {
117124
Ok(Some(cand))
118125
} else {

‎tests/regression.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,3 +1029,11 @@ rgtest!(r1878, |dir: Dir, _: TestCommand| {
10291029
let args = &["-U", "--mmap", r"\Abaz", "test"];
10301030
dir.command().args(args).assert_err();
10311031
});
1032+
1033+
// See: https://github.com/BurntSushi/ripgrep/issues/1891
1034+
rgtest!(r1891, |dir: Dir, mut cmd: TestCommand| {
1035+
dir.create("test", "\n##\n");
1036+
// N.B. We use -o here to force the issue to occur, which seems to only
1037+
// happen when each match needs to be detected.
1038+
eqnice!("1:\n2:\n2:\n", cmd.args(&["-won", "", "test"]).stdout());
1039+
});

0 commit comments

Comments
 (0)