Skip to content

grep: don't read the inputs when the pattern list is empty - #215

Merged
sylvestre merged 2 commits into
uutils:mainfrom
HarpeLm:empty-pattern-list-skips-read
Nov 21, 2026
Merged

sylvestre merged 2 commits into
uutils:mainfrom
HarpeLm:empty-pattern-list-skips-read

Conversation

@HarpeLm

@HarpeLm HarpeLm commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #204

An empty pattern list (-f /dev/null, or -f on an empty file) can't match any line, so GNU grep exits 1 without opening any input, as with -m 0. uu_grep searched every file anyway: it reported missing files (exit 2) and printed a 0 count per file with -c.

This returns the "no match" status right after compiling the matcher, next to the existing -m 0 and -v '' short-circuits. It only applies without -v and -L: with -v every line is selected and with -L every file is listed, so both still read the inputs (the issue notes both tools already agree there). A pattern file holding one empty line is not an empty list (it matches every line), and -f on an empty file combined with -e still searches.

Tests

New tests in tests/test_grep.rs:

  • the issue's test: -c -f none.pat birds.txt gone.txt exits 1 with no output
  • -G, -E and -F with a missing file (GNU's skip-read test)
  • standard input is not read either
  • -v still prints every line, -L still lists every file
  • a pattern file with one empty line still matches every line
  • an empty -f combined with -e still searches

Without the fix, 3 of them fail.

  • cargo test: all pass
  • cargo fmt --all -- --check
  • cargo clippy --all-targets -- -D warnings

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing HarpeLm:empty-pattern-list-skips-read (da67b1b) with main (6754749)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.19%. Comparing base (9598faf) to head (da67b1b).
⚠️ Report is 112 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #215      +/-   ##
==========================================
+ Coverage   95.28%   97.19%   +1.91%     
==========================================
  Files           6        6              
  Lines        1422     2106     +684     
  Branches      140      230      +90     
==========================================
+ Hits         1355     2047     +692     
+ Misses         66       59       -7     
+ Partials        1        0       -1     
Flag Coverage Δ
macOS_latest 97.80% <100.00%> (+1.70%) ⬆️
ubuntu_latest 97.80% <100.00%> (+1.70%) ⬆️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread tests/test_grep.rs Resolved
Comment thread tests/test_grep.rs Resolved
Comment thread src/lib.rs Resolved
@sylvestre
sylvestre merged commit 07ed60c into uutils:main Nov 21, 2026
24 of 26 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants