Skip to content

locate: don't panic on a too-short --database file - #718

Merged
sylvestre merged 1 commit into
uutils:mainfrom
leeewee:locate-fix-short-db-header
Jun 9, 2026
Merged

sylvestre merged 1 commit into
uutils:mainfrom
leeewee:locate-fix-short-db-header

Conversation

@leeewee

@leeewee leeewee commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #716

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.

`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

codecov Bot commented Jun 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.85%. Comparing base (62deaf6) to head (f714cf9).
⚠️ Report is 2 commits behind head on main.

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.
📢 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 Jun 9, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing leeewee:locate-fix-short-db-header (f714cf9) with main (8add685)

Open in CodSpeed

@sylvestre
sylvestre merged commit e2d84e9 into uutils:main Jun 9, 2026
22 checks passed
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.

locate panics (slice index underflow) on a 1-byte --database file

2 participants