Repository navigation
Description
Known: #72 (merged PR) — #72; #67 (closed: fixed by #72) — #67
With -o, GNU grep does not print context lines, but it still tracks the context groups and
prints the -- separator (or --group-separator) between matches whose context groups are
not adjacent. uu_grep never prints the separator once -o is given. This is a side effect of
the fix for #67 (PR #72), which zeroes the context and clears has_context whenever -o is
set.
Reproduction
Rust (incorrect)
$ printf 'foo 1\nline 2\nline 3\nfoo 4\nline 5\n' | ./target/release/grep -o -A1 foo
foo
foo
# Exit code: 0
$ printf 'foo 1\nline 2\nline 3\nfoo 4\n' | ./target/release/grep -o --group-separator=XX -A1 foo
foo
foo
# Exit code: 0GNU (correct)
$ printf 'foo 1\nline 2\nline 3\nfoo 4\nline 5\n' | LC_ALL=C /usr/bin/grep -o -A1 foo
foo
--
foo
# Exit code: 0
$ printf 'foo 1\nline 2\nline 3\nfoo 4\n' | LC_ALL=C /usr/bin/grep -o --group-separator=XX -A1 foo
foo
XX
foo
# Exit code: 0Adjacent groups get no separator in either implementation: with -C1 -n on the first input
both print 1:foo and 4:foo with nothing in between (context 1-2 and 3-5 touch).
Root cause
src/lib.rs:329-340:
let (before_context, after_context, has_context) = {
// "-o" overrides any context arguments
if only_matching {
(0, 0, false)
} else {With the context forced to 0 and has_context false, the separator check in
session_write_match_with_context (src/searcher.rs:674) never fires.
Test
#[test]
fn only_matching_with_context_keeps_group_separator() {
let (_s, mut c) = ucmd();
c.args(&["-o", "-A1", "foo"])
.pipe_in("foo 1\nline 2\nline 3\nfoo 4\nline 5\n")
.succeeds()
.stdout_only("foo\n--\nfoo\n");
let (_s, mut c) = ucmd();
c.args(&["-o", "-n", "-C1", "foo"])
.pipe_in("foo 1\nline 2\nline 3\nfoo 4\nline 5\n")
.succeeds()
.stdout_only("1:foo\n4:foo\n");
}Reported by Anthropic's OSS Scanner (ANT-2026-S0YAMG54).
Activity