Skip to content

searcher: fix runaway output with -v, -m, and -A combined - #3602

Open
tanveer-arch wants to merge 1 commit into
BurntSushi:masterfrom
tanveer-arch:fix-invert-maxcount-after-context
Open

tanveer-arch wants to merge 1 commit into
BurntSushi:masterfrom
tanveer-arch:fix-invert-maxcount-after-context

Conversation

@tanveer-arch

@tanveer-arch tanveer-arch commented Nov 12, 2026 •

Copy link
Copy Markdown

Fixes #3593.

Combining -v, -m, and -A currently bugs out and prints the entire rest of the input as matches, even lines that actually match the pattern.

The root issue is how the limit's None return gets handled under -v:

  • Slow path: Once the limit is hit, shortest_match returns None. Under -v, this reads as an inverted match. The line gets sunk as a match, which resets the after-context counter back to full, so the stop condition is never reached.
  • Fast path: If the limit falls exactly at the end of a read buffer while after-context is owed (like reading from a pipe), the next buffer's lines get sunk as matches. Separately, if a sink returned false, the code treated it as a break, which incorrectly ran the trailing after-context and called the sink again for those same lines.

The fix handles this across three parts in crates/searcher/src/searcher/core.rs:

  • Slow path: When the limit is blown but we still owe after-context, we now check the line with the real matcher (is_match). Matching lines are sunk as after-context. Non-matching lines are sunk as matches, but we just consume one line of after-context instead of resetting it, and we don't count it toward the limit. (If no after-context is owed, e.g. --passthru, it falls through to the old behavior).
  • Fast-path invert stepper: If the limit is already hit when entering the loop, return without sinking so the caller handles trailing after-context. If hit right after sinking, return true (not false) so the caller still runs the trailing context.
  • Fast path main loop: If the stepper returns false, we stop entirely instead of breaking to the trailing context. false now strictly means the sink rejected it.

Testing & Validation:

  • Added 2 unit tests in crates/searcher/src/searcher/glue.rs (slow/fast slices + a chunked-reader simulating a two-read pipe) and integration test r3593 in tests/regression.rs.
  • All 3 tests fail on base 178cc027e2 and pass with this fix.
  • Workspace tests are green: grep-searcher 79/79 (+ 1 doctest), integration 333/333, 0 total failures.
  • Clean cargo fmt, 0 new clippy warnings (37 pre-existing) on 1.99.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-v -m N -A k prints every remaining line as a match when the search takes the slow path or crosses a buffer

1 participant