Skip to content

-o should suppress -A/-B/-C context, but uu_grep still prints context lines #67

Closed

Description

@sylvestre

grep - -o should suppress -A/-B/-C context, but uu_grep still prints context lines

Found by the differential fuzzer with args grep ["-e", "\\w", "-o", "-B", "3", "-n", "-B", "2"].

GNU grep ignores the context options (-A, -B, -C) when --only-matching (-o) is in
effect: only the matched text of matching lines is printed, never the surrounding context.
uu_grep still emits the before/after context lines, so its output contains extra lines GNU
never produces.

Reproduction

Input is four lines: aa, bb, cc, xx.

Rust (incorrect)

$ printf 'aa\nbb\ncc\nxx\n' | ./target/release/grep -e 'xx' -o -B 2
# Output:
# bb
# cc
# xx
# Exit code: 0

GNU (correct)

$ printf 'aa\nbb\ncc\nxx\n' | LC_ALL=C /usr/bin/grep -e 'xx' -o -B 2
# Output:
# xx
# Exit code: 0

The same applies to -A (and -C):

$ printf 'aa\nbb\ncc\nxx\n' | ./target/release/grep -e 'aa' -o -A 1    # Rust: "aa", "bb"
$ printf 'aa\nbb\ncc\nxx\n' | LC_ALL=C /usr/bin/grep -e 'aa' -o -A 1   # GNU:  "aa"

With -n the divergence shows up as extra N- context-prefixed lines in the Rust output for
each context line (including empty ones), e.g. 4-, 5- before the matched 6:x.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions