Skip to content

grep - an invalid pattern read with -f FILE is reported without GNU's FILE:LINE: prefix #179

Open

Description

@sylvestre

When a pattern that came from -f FILE fails to compile, GNU grep names the file and the
line it came from: grep: pats:2: Invalid range end (and -:2: for -f -). Patterns from
-e or the positional argument get no prefix. uu_grep never prints the prefix, so a user
with a long pattern file is not told which line to fix, and stderr differs from GNU. Exit
status (2) and output are the same.

Reproduction

Rust (incorrect)

$ printf 'foo\n[z-a]\n' > pats
$ printf 'foo\n' | ./target/release/grep -f pats
grep: Invalid range end
# Exit code: 2

GNU (correct)

$ printf 'foo\n' | LC_ALL=C /usr/bin/grep -f pats
/usr/bin/grep: pats:2: Invalid range end
# Exit code: 2

$ LC_ALL=C /usr/bin/grep -f - /dev/null < pats
/usr/bin/grep: -:2: Invalid range end
# Exit code: 2

-e '[z-a]' prints grep: Invalid range end in both tools. Line numbers count the lines of
that file, including duplicate and empty lines.

Root cause

uumain (src/lib.rs:236-270) splits every -f file into lines and pushes them into the
same flat patterns: Vec<&str> as the -e and positional patterns, so where each one came
from is lost. Matcher::compile then stops at the first failing pattern with only the
pattern text and the engine error (src/matcher.rs:31-35):

pub fn compile(config: &'a Config<'a>) -> UResult<Self> {
    let mut patterns = Vec::with_capacity(config.patterns.len());
    for raw in config.patterns {
        patterns.push(CompiledPattern::compile(raw, config)?);
    }

Test

#[test]
fn pattern_file_error_names_file_and_line() {
    let (scene, mut c) = ucmd();
    scene.fixtures.write("list", "ok\n\nalso-ok\n[y-b]\n");
    c.args(&["-f", "list"])
        .pipe_in("ok\n")
        .fails_with_code(2)
        .no_stdout()
        .stderr_is("grep: list:4: Invalid range end\n");

    let (_s, mut c) = ucmd();
    c.args(&["-f", "-", "/dev/null"])
        .pipe_in("ok\n[y-b]\n")
        .fails_with_code(2)
        .stderr_is("grep: -:2: Invalid range end\n");

    let (_s, mut c) = ucmd();
    c.args(&["-e", "[y-b]"])
        .pipe_in("ok\n")
        .fails_with_code(2)
        .stderr_is("grep: Invalid range end\n");
}

Reported by Anthropic's OSS Scanner (ANT-2026-TQ02YPCO).

Activity

  1. added
    good first issue
    oss-scanner
    gnu-compat
    on Nov 9, 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

    gnu-compatgood first issueoss-scanner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions