You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
locate panics (slice index underflow) on a 1-byte --database file #716
Running locate -d <file> <pattern> against a database file that is exactly one byte long panics and exits 101. DbReader::check_db reads one byte, then read_until(b'\0', ...) into a Vec that ends up empty (EOF, nothing appended); buf.len() - 1 underflows to usize::MAX and &buf[..usize::MAX] is an out-of-bounds slice. A graceful implementation should treat the too-short input as an unrecognized database format and exit non-zero, not abort.
Steps to reproduce
$ printf'X'> /tmp/db_1byte.bin
$ ./target/release/locate -d /tmp/db_1byte.bin foothread 'main' panicked at src/locate/mod.rs:494:43:range end index 18446744073709551615 out of range for slice of length 0
$ echo$?101
uutils locate panics with a slice BoundsCheck and exits 101. The release profile has no overflow-checks, so buf.len() - 1 wraps to usize::MAX instead of aborting on the subtraction; the out-of-bounds slice then panics. Boundary behavior: a 0-byte file is handled gracefully (the first read_exact fails -> None -> exit 1); a >=2-byte file leaves buf non-empty and is safe; only the exact 1-byte case crashes. The path is reachable via the user-controlled -d/--database argument.
When the file is exactly 1 byte, the initial read_exact consumes it, then read_until hits EOF immediately and appends nothing, leaving buf empty. buf.len() - 1 underflows to usize::MAX, and &buf[..usize::MAX] is out-of-bounds. The fix is to guard the empty case before slicing — e.g. if buf.is_empty() { return None; } (no valid LOCATE02 header), or use buf.split_last() / buf.strip_suffix(b"\0") instead of buf.len() - 1.
Activity