fix(io): guard sampled_offsets index in get_line() to prevent panic on corrupt cache

Replace direct indexing with .get()? to return None instead of panicking
when sampled_offsets is shorter than expected (e.g. corrupt bincode cache).

Closes #3
This commit is contained in:
dailz
2026-06-03 15:39:05 +08:00
parent 24fe97a457
commit bef0b44e91

View File

@@ -233,7 +233,7 @@ impl LineIndex {
}
let block = idx / BLOCK_SIZE;
let offset_in_block = idx % BLOCK_SIZE;
let mut pos = self.sampled_offsets[block] as usize;
let mut pos = (*self.sampled_offsets.get(block)?) as usize;
for _ in 0..offset_in_block {
match memchr::memchr(b'\n', &data[pos..]) {
Some(rel) => pos = pos + rel + 1,