You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
grep - an invalid pattern read with -f FILE is reported without GNU's FILE:LINE: prefix #179
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):
pubfn compile(config:&'aConfig<'a>) -> UResult<Self>{letmut patterns = Vec::with_capacity(config.patterns.len());for raw in config.patterns{
patterns.push(CompiledPattern::compile(raw, config)?);}
Test
#[test]fnpattern_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