Repository navigation
grep: suppress matches in input blocks containing a NUL byte - #231
yunusarda-sudo wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #231 +/- ##
==========================================
+ Coverage 97.19% 97.24% +0.04%
==========================================
Files 6 6
Lines 2248 2252 +4
Branches 243 247 +4
==========================================
+ Hits 2185 2190 +5
+ Misses 63 62 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merging this PR will not alter performanceWarning uutils has exceeded the Hobby plan limit of 5 active contributors. Upgrade your plan or apply to the open source program by December 8, 2026 to keep using CodSpeed. 1 warning
Comparing Footnotes
|
|
GNU grep testsuite comparison: |
|
thanks for the work on this! |
| let mut nl_cursor = 0; | ||
| let mut nl_before = 0u64; | ||
|
|
||
| if detect_binary && !binary && has_nul { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Removed. has_nul already covers the whole chunk on this path, so the nul_scanned memchr loop is gone.
| // 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()) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yes. The per-line memchr(0, line) is now only used with -z, where the block flag is not set.
| } | ||
|
|
||
| /// Returns true if the buffer currently contains or has encountered a NUL byte. | ||
| pub fn has_nul(&self) -> bool { |
There was a problem hiding this comment.
this is never called, do we need it?
either this accessor or the extra tuple field, not both
There was a problem hiding this comment.
Dropped the accessor; the tuple field is what the searcher uses.
| Ok(Some((line, line_start, self.has_nul))) | ||
| }; | ||
| } | ||
| if self.line_terminator != 0 |
There was a problem hiding this comment.
this is the same block as in read_chunk, could be dedup, no?
There was a problem hiding this comment.
Folded both sites into LineBuffer::note_new_bytes.
5131998 to
4d5aab0
Compare
|
GNU grep testsuite comparison: |
4d5aab0 to
f4f9a4b
Compare
|
GNU grep testsuite comparison: |
f4f9a4b to
57ced54
Compare
|
Addressed in 57ced54 (rebased onto current main): the second NUL scan is gone, the unused |
|
GNU grep testsuite comparison: |
57ced54 to
ab43d71
Compare
|
GNU grep testsuite comparison: |
ab43d71 to
6337b42
Compare
|
GNU grep testsuite comparison: |
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: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.fast_print), NUL bytes were only scanned up toline_endof each match, printing matching lines prior to the NUL in the same chunk.Solution
has_nulinLineBufferon each read into the buffer (whenline_terminator != 0).has_nulviaLineBuffer::read_lineandLineBuffer::read_chunkso bothsession_runandfast_printknow whether the current input block contains a NUL byte.binary file matchesnotice (unless--binary-files=without-match).null-byte).nul_later_in_small_file_hides_earlier_matchesreproducer test cases intests/test_grep.rs.