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.32% 97.36% +0.04%
==========================================
Files 6 6
Lines 2315 2317 +2
Branches 247 249 +2
==========================================
+ Hits 2253 2256 +3
+ Misses 62 61 -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 performance1 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: |
| // 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()) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Done: the chunk is scanned once, before the loop.
| 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() { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Done: dropped matched, new test nul_in_earlier_block_without_match_hides_later_match (fails without the change).
| 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. |
There was a problem hiding this comment.
it isn't lazy, it is set on every read. Also only in read_line, read_chunk never sets it
There was a problem hiding this comment.
Fixed the doc comment: only read_line maintains it, the fast path scans chunks itself.
6337b42 to
b42046b
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.