Skip to content

grep - matching lines before a NUL byte are printed; GNU suppresses them when the NUL is in the same input block #210

Open

Description

@sylvestre

When a file contains a NUL byte GNU grep treats it as binary. In the observed behaviour of GNU
grep 3.12 the decision covers the whole block of input it is processing: in a small file
with a NUL anywhere, no matching line is printed at all, only binary file matches. Only lines
in blocks that come before the NUL (hundreds of KiB earlier) are printed. uu_grep checks for
NUL line by line (and, on the literal fast path, only up to the current match), so every
matching line that precedes the NUL is printed before the notice.

Exposed by the GNU test null-byte (a ~400 KB file xxx, 100000 x yyy, z, then a NUL:
grep -E 'xxx|z' must print only xxx; uu_grep also prints z, which sits in the same
block as the NUL).

Reproduction

$ printf 'apple\nkiwi\n\0pear\n' > nul1

Rust (incorrect)

$ ./target/release/grep -E 'apple|kiwi' nul1
grep: nul1: binary file matches
apple
kiwi
# Exit code: 0

$ ./target/release/grep kiwi nul1
grep: nul1: binary file matches
kiwi
# Exit code: 0

GNU (correct)

$ LC_ALL=C /usr/bin/grep -E 'apple|kiwi' nul1
/usr/bin/grep: nul1: binary file matches
# Exit code: 0

$ LC_ALL=C /usr/bin/grep kiwi nul1
/usr/bin/grep: nul1: binary file matches
# Exit code: 0

Test

#[test]
fn nul_later_in_small_file_hides_earlier_matches() {
    for pattern in ["lime", "l.me", "lime|plum"] {
        let (_s, mut c) = ucmd();
        c.args(&["-E", pattern])
            .pipe_in(&b"plum\nlime\nfig\x00\nlime\n"[..])
            .succeeds()
            .no_stdout()
            .stderr_contains("binary file matches");
    }
}

Exposed by the GNU grep test suite: null-byte.

Activity

  1. added
    bug
    good first issue
    gnu-compat
    on Nov 17, 2026
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

    buggnu-compatgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions