Skip to content
Myr

printer: fix panic in replacements in look-around corner case

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The abstraction boundary fuck up is the gift that keeps on giving. It turns out that the invariant that the match would never exceed the range given is not always true. So we kludge around it. Also, update the CHANGELOG to include the fix for #2111. Fixes #3180

authored by Andrew Gallant · committed by Andrew Gallant · parent 9164158 · Browse files · 3 files+27 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+27 added−4 removed

  • tests:+13 added−0 removed
  • other:+14 added−4 removed

3 files+27 added−4 removed

modified:crates/printer/src/util.rs+10 added-4 removed
57 unmodified lines
58 ) -> io::Result<()> {59 // See the giant comment in 'find_iter_at_in_context' below for why we60 // do this dance.61 let is_multi_line = searcher.multi_line_with_matcher(&matcher);62 // Get the line_terminator that was removed (if any) so we can add it back62 // Get the line_terminator that was removed (if any) so we can add it63 // back.64 let line_terminator = if is_multi_line {65 if haystack[range.end..].len() >= MAX_LOOK_AHEAD {66 haystack = &haystack[..range.end + MAX_LOOK_AHEAD];67 }
445 unmodified lines
513 // When searching a single line, we should remove the line terminator.514 // Otherwise, it's possible for the regex (via look-around) to observe515 // the line terminator and not match because of it.516 let mut m = Match::new(0, range.end);516 // No need to rember the line terminator as we aren't doing a replace here517 // No need to rember the line terminator as we aren't doing a replace518 // here.519 trim_line_terminator(searcher, bytes, &mut m);520 bytes = &bytes[..m.end()];521 }522 matcher
53 unmodified lines
576 dst.extend(&bytes[last_match..m.start()]);577 last_match = m.end();578 append(caps, dst)579 })?;578 let end = std::cmp::min(bytes.len(), range.end);580 let end = if last_match > range.end {581 bytes.len()582 } else {583 std::cmp::min(bytes.len(), range.end)584 };585 dst.extend(&bytes[last_match..end]);580 // Add back any line terminator586 // Add back any line terminator.587 dst.extend(line_terminator);588 Ok(())589}590
18 unmodified lines
modified:tests/regression.rs+13 added
1652 unmodified lines
1653 eqnice!(cmd().args(&["--files"]).stdout(), ".foo.txt\n");1654 eqnice!(cmd().args(&["--files", "."]).stdout(), "./.foo.txt\n");1655 eqnice!(cmd().args(&["--files", "./"]).stdout(), "./.foo.txt\n");1656});16571658// See: https://github.com/BurntSushi/ripgrep/issues/31801659rgtest!(r3180_look_around_panic, |dir: Dir, mut cmd: TestCommand| {1660 dir.create("haystack", " b b b b b b b b\nc\n");16611662 let got = cmd1663 .arg(r#"(^|[^a-z])((([a-z]+)?)\s)?b(\s([a-z]+)?)($|[^a-z])"#)1664 .arg("haystack")1665 .arg("-U")1666 .arg("-rx")1667 .stdout();1668 eqnice!("xbxbx\n", got);1669});
modified:CHANGELOG.md+4 added
2 unmodified lines
3Unreleased changes. Release notes have not yet been written.45Performance improvements:67* [PERF #2111](https://github.com/BurntSushi/ripgrep/issues/2111):8 Don't resolve helper binaries on Windows when `-z/--search-zip` isn't used.9* [PERF #2865](https://github.com/BurntSushi/ripgrep/pull/2865):10 Avoid using path canonicalization on Windows when emitting hyperlinks.1112Bug fixes:
34 unmodified lines
47* [BUG #3155](https://github.com/BurntSushi/ripgrep/issues/3155):48 Statically compile PCRE2 into macOS release artifacts on `aarch64`.49* [BUG #3173](https://github.com/BurntSushi/ripgrep/issues/3173):50 Fix ancestor ignore filter bug when searching whitelisted hidden files.51* [BUG #3180](https://github.com/BurntSushi/ripgrep/issues/3180):52 Fix a panicking bug when using `-U/--multiline` and `-r/--replace`.5354Feature enhancements:5556* Many enhancements to the default set of file types available for filtering.
1735 unmodified lines