Skip to content

grep: suppress matches in input blocks containing a NUL byte - #231

Open
yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:fix-grep-block-nul-suppression
Open

yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:fix-grep-block-nul-suppression

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

Fixes #210

Summary

When an input file contains a NUL byte, GNU grep treats it as binary and suppresses matching lines in the input block containing that NUL byte.

Previously in uu_grep:

  • On the line-at-a-time engine (session_run), binariness was only tested line-by-line (memchr(0, line)), printing any earlier matching lines in the same input block before encountering the line with the NUL byte.
  • On the fast buffer engine (fast_print), NUL bytes were only scanned up to line_end of each match, printing matching lines prior to the NUL in the same chunk.

Solution

  • Track has_nul in LineBuffer on each read into the buffer (when line_terminator != 0).
  • Expose has_nul via LineBuffer::read_line and LineBuffer::read_chunk so both session_run and fast_print know whether the current input block contains a NUL byte.
  • Suppress matches in blocks containing a NUL byte, emitting only the binary file matches notice (unless --binary-files=without-match).
  • Matches in earlier blocks before the block containing the NUL byte remain printed, matching GNU grep semantics (e.g. GNU test null-byte).
  • Added nul_later_in_small_file_hides_earlier_matches reproducer test cases in tests/test_grep.rs.

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.36%. Comparing base (9aa25a6) to head (b42046b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #231      +/-   ##
==========================================
+ Coverage   97.32%   97.36%   +0.04%     
==========================================
  Files           6        6              
  Lines        2315     2317       +2     
  Branches      247      249       +2     
==========================================
+ Hits         2253     2256       +3     
+ Misses         62       61       -1     
Flag Coverage Δ
macOS_latest 98.25% <100.00%> (+0.04%) ⬆️
ubuntu_latest 98.25% <100.00%> (+0.04%) ⬆️
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.

@codspeed

codspeed Bot commented Nov 20, 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:fix-grep-block-nul-suppression (b42046b) with main (9aa25a6)

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: 76 / FAILED: 31 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 75 / FAILED: 32 / SKIPPED: 21

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

Test improvements (1):
  + null-byte

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for the work on this!
The codspeed regression (-24.5%) is the blocker: the new NUL check scans every byte a second time alongside the old scans, see the inline comments.

Comment thread src/searcher.rs Outdated
let mut nl_cursor = 0;
let mut nl_before = 0u64;

if detect_binary && !binary && has_nul {

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 fast path excludes null_data, so has_nul now covers the whole chunk.
could you please remove the old nul_scanned memchr scans below? they scan every byte a second time, probably the main cause of the codspeed regression

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.

Removed. has_nul already covers the whole chunk on this path, so the nul_scanned memchr loop is gone.

Comment thread src/searcher.rs Outdated
// Any null byte flips us into binary mode.
if !self.session_mark_binary_if(|| memchr(0, line).is_some()) {
// Any null byte in the current line or input block flips us into binary mode.
if !self.session_mark_binary_if(|| has_nul || memchr(0, line).is_some()) {

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.

when the terminator is not NUL, memchr(0, line) is redundant with has_nul, so we scan everything twice.
could we only keep it for -z?

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.

Yes. The per-line memchr(0, line) is now only used with -z, where the block flag is not set.

Comment thread src/line_buffer.rs Outdated
}

/// Returns true if the buffer currently contains or has encountered a NUL byte.
pub fn has_nul(&self) -> bool {

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 never called, do we need it?
either this accessor or the extra tuple field, not both

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.

Dropped the accessor; the tuple field is what the searcher uses.

Comment thread src/line_buffer.rs
Ok(Some((line, line_start, self.has_nul)))
};
}
if self.line_terminator != 0

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 same block as in read_chunk, could be dedup, 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.

Folded both sites into LineBuffer::note_new_bytes.

@yunusarda-sudo
yunusarda-sudo force-pushed the fix-grep-block-nul-suppression branch from 5131998 to 4d5aab0 Compare November 22, 2026 07:57
@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21

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

Test improvements (1):
  + null-byte

@yunusarda-sudo
yunusarda-sudo force-pushed the fix-grep-block-nul-suppression branch from 4d5aab0 to f4f9a4b Compare November 22, 2026 08:58
@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21

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

Test improvements (1):
  + null-byte

@yunusarda-sudo
yunusarda-sudo force-pushed the fix-grep-block-nul-suppression branch from f4f9a4b to 57ced54 Compare November 22, 2026 13:53
@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

Addressed in 57ced54 (rebased onto current main): the second NUL scan is gone, the unused has_nul accessor is gone, and the two read_line/read_chunk sites share note_new_bytes. CodSpeed should recover now that we no longer scan every byte twice.

@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21

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

Test improvements (1):
  + null-byte

@yunusarda-sudo
yunusarda-sudo force-pushed the fix-grep-block-nul-suppression branch from 57ced54 to ab43d71 Compare November 24, 2026 20:24
@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21

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

Test improvements (1):
  + null-byte

@yunusarda-sudo
yunusarda-sudo force-pushed the fix-grep-block-nul-suppression branch from ab43d71 to 6337b42 Compare November 25, 2026 16:08
@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21

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

Test improvements (1):
  + null-byte

Comment thread src/searcher.rs Outdated
// marks the file binary and suppresses output for this block.
if detect_binary
&& !binary
&& (memchr(0, chunk).is_some() || std::str::from_utf8(line).is_err())

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 runs memchr(0, chunk) again for every match until the file is binary, so a text chunk with many matches is scanned many times.
could you please check the chunk once, before the loop?

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 chunk is scanned once, before the loop.

Comment thread src/searcher.rs Outdated
let mut nl_before = 0u64;

// If an earlier block already matched, a NUL in this block marks the file binary.
if detect_binary && !binary && matched && memchr(0, chunk).is_some() {

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.

why only when matched? with a NUL in a block without match and a hit in a later block, we now print the hit, GNU doesn't (the old end-of-chunk check handled that).
please drop the matched condition and add a test for it

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: dropped matched, new test nul_in_earlier_block_without_match_hides_later_match (fails without the change).

Comment thread src/line_buffer.rs Outdated
next_line_start: u64,
/// Set once a file read has returned EOF.
eof: bool,
/// Lazily populated: whether the current buffered input contains a NUL byte.

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.

it isn't lazy, it is set on every read. Also only in read_line, read_chunk never sets it

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.

Fixed the doc comment: only read_line maintains it, the fast path scans chunks itself.

@yunusarda-sudo
yunusarda-sudo force-pushed the fix-grep-block-nul-suppression branch from 6337b42 to b42046b Compare December 4, 2026 04:51
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 78 / FAILED: 29 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21

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

Test improvements (1):
  + null-byte

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 - matching lines before a NUL byte are printed; GNU suppresses them when the NUL is in the same input block

2 participants