Repository navigation
locate: don't panic on a too-short --database file - #718
Merged
Merged
Conversation
`locate -d <file> <pattern>` panicked (slice index underflow, exit 101) when the database file is exactly one byte: `check_db` consumes that byte with `read_exact`, then `read_until(b'\0', ...)` hits EOF and appends nothing, leaving `buf` empty. `&buf[..buf.len() - 1]` then underflows `buf.len() - 1` to `usize::MAX` and slices out of bounds. Strip the trailing nul with `strip_suffix(b"\0")` (falling back to the whole buffer when `read_until` stopped at EOF without one) instead of slicing `buf.len() - 1`. An empty buffer now yields `None` (unrecognized format), so the too-short database is skipped like any other invalid one and locate exits 1 instead of crashing. Adds a regression test.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #718 +/- ##
==========================================
+ Coverage 91.83% 91.85% +0.01%
==========================================
Files 35 35
Lines 7129 7129
Branches 375 375
==========================================
+ Hits 6547 6548 +1
+ Misses 437 436 -1
Partials 145 145 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #716
locate -d <file> <pattern>panicked (slice index underflow, exit 101) when the database file is exactly one byte:check_dbconsumes that byte withread_exact, thenread_until(b'\0', ...)hits EOF and appends nothing, leavingbufempty.&buf[..buf.len() - 1]then underflowsbuf.len() - 1tousize::MAXand slices out of bounds.Strip the trailing nul with
strip_suffix(b"\0")(falling back to the whole buffer whenread_untilstopped at EOF without one) instead of slicingbuf.len() - 1. An empty buffer now yieldsNone(unrecognized format), so the too-short database is skipped like any other invalid one and locate exits 1 instead of crashing. Adds a regression test.