Skip to content

grep - an empty pattern list (-f /dev/null) still opens and reads every file #204

Closed

Description

@sylvestre

-f FILE with an empty file gives grep no pattern at all, so no line can match. GNU grep
then behaves like -m 0: it exits 1 without opening the files, so a missing file is not
reported and -c prints nothing. uu_grep searches every file anyway: it reports missing
files (exit 2) and prints a 0 count per file. (With -v or -L both tools read the files
and agree.)

Exposed by the GNU test skip-read (grep -f /dev/null no-such-file must exit 1 with no
output, with -G, -E and -F).

Reproduction

$ printf 'red\n' > colors

Rust (incorrect)

$ ./target/release/grep -f /dev/null colors absent.txt
grep: absent.txt: No such file or directory
# Exit code: 2

$ ./target/release/grep -c -f /dev/null colors
0
# Exit code: 1

GNU (correct)

$ LC_ALL=C /usr/bin/grep -f /dev/null colors absent.txt
# Output: (none)
# Exit code: 1

$ LC_ALL=C /usr/bin/grep -c -f /dev/null colors
# Output: (none)
# Exit code: 1

Test

#[test]
fn empty_pattern_file_does_not_read_inputs() {
    let (scene, mut c) = ucmd();
    scene.fixtures.write("none.pat", "");
    scene.fixtures.write("birds.txt", "crow\n");
    c.args(&["-c", "-f", "none.pat", "birds.txt", "gone.txt"])
        .fails_with_code(1)
        .no_output();
}

Exposed by the GNU grep test suite: skip-read.

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