Skip to content

grep: withhold only the invalid UTF-8 line, keep printing later matches - #200

Open
yunusarda-sudo wants to merge 5 commits into
uutils:mainfrom
yunusarda-sudo:grep-invalid-utf8-line
Open

yunusarda-sudo wants to merge 5 commits into
uutils:mainfrom
yunusarda-sudo:grep-invalid-utf8-line

Conversation

@yunusarda-sudo

@yunusarda-sudo yunusarda-sudo commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

A selected line that is not valid UTF-8 was handled like a NUL byte: output stopped there and every later match was lost. In a UTF-8 locale GNU grep only leaves that line out, keeps printing the other matches, and writes binary file matches once at the end.

Both the line-at-a-time path and the literal fast path now skip just that line (it still counts as a match, so -m and the exit status are unchanged) and report the notice when the file is done. NUL handling, -a and -I are unchanged.

This only applies when the locale is UTF-8 (judged from LC_ALL, LC_CTYPE, LANG). In a unibyte locale such as C, an encoding error still marks the file binary and stops the output at that line, which keeps the GNU unibyte-binary test passing.

Tests cover both paths in a UTF-8 locale (including -m2) and the unibyte case.

Closes #172

@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 yunusarda-sudo:grep-invalid-utf8-line (169ad13) with main (050ed54)

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. ↩

@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 74 / FAILED: 33 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 75 / FAILED: 32 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: -1
  FAILED: +1

New test failures (1):
  - unibyte-binary

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.52941% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.21%. Comparing base (6754749) to head (169ad13).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/searcher.rs 98.52% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #200      +/-   ##
==========================================
+ Coverage   97.19%   97.21%   +0.01%     
==========================================
  Files           6        6              
  Lines        2106     2152      +46     
  Branches      230      241      +11     
==========================================
+ Hits         2047     2092      +45     
- Misses         59       60       +1     
Flag Coverage Δ
macOS_latest 97.80% <98.52%> (+<0.01%) ⬆️
ubuntu_latest 97.80% <98.52%> (+<0.01%) ⬆️
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 Cargo.toml Outdated
onig = { version = "~6.5.1", default-features = false }
onig_sys = { version = "69.9.3", default-features = false }
uucore = "0.12.0"
uucore = { version = "0.12.0", features = ["i18n-common"] }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this pulls icu_locale and ~20 new crates just to read LC_CTYPE
what is the impact on the binary size?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Measured with cargo build --release: main is 1,889,712 bytes, with i18n-common it is 1,929,208 (+39.5 KB, about 2%), and with a local LC_ALL/LC_CTYPE/LANG lookup it is 1,893,992 (+4.3 KB). Since only the encoding is needed, I went back to the small local lookup (locale_is_utf8) and dropped the feature, so Cargo.toml and Cargo.lock are unchanged now.

Comment thread src/searcher.rs

// An invalid-UTF-8 matching line is withheld, but the search
// goes on and the notice is emitted once at the end.
if encoding_error_line {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost the same bookkeeping as at the end of the loop, could be dedup, no?
only the write_line needs to be skipped

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: the withheld case now only skips the write_line (and its line number), and the count/matched/offset bookkeeping is shared.

Comment thread src/searcher.rs
self.session_last_printed_line = ctx.line_number;
}

if withhold {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test with -A/-B around a withheld line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added invalid_utf8_line_withheld_with_context (-A1, -B1, -n -C1): the withheld line keeps its before-context but is not printed and starts no after-context. The output matches GNU grep in C.UTF-8.

Withholding only the invalid line and printing the later matches is what
happens in a UTF-8 locale. In a unibyte locale such as C the line makes
the file binary and the output stops there, as before. The locale is read
from LC_ALL, LC_CTYPE and LANG.
Use uucore's get_ctype_encoding instead of a local LC_ALL/LC_CTYPE/LANG
lookup.
Comment thread tests/test_grep.rs Outdated
// In the C locale an encoding error makes the file binary: output stops at
// that line and the notice follows. Covers both search paths.
let (scene, _) = ucmd();
scene.fixtures.write_bytes("u", b"a\n\x80\nb\n");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the fixture of the GNU unibyte-binary test
please use your own input

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in commit 169ad13: replaced with a custom multi-line text fixture.

Comment thread src/searcher.rs Outdated
if let Some(positions) = self.session_match_line(line) {
// TODO: GNU grep respects LANG. Here, I'm always checking for valid UTF-8.
if self.config.binary_mode != BinaryMode::WithoutMatch
if !self.withhold_encoding_errors

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the utf-8 check is now in two places (here and session_withholds_line), with opposite locale conditions
could be merged, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit 169ad13: merged both UTF-8 validation branches into a single session_check_encoding method.

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 - a matching line with invalid UTF-8 hides all later matches, not just that line

2 participants