🔴 [H3] 缓存损坏时 get_line() 会 panic #3

Closed
opened 2026-06-03 13:52:51 +08:00 by dailz · 0 comments
Owner

文件: core/io/line_index.rs:236
分类: 缓存 / 健壮性

问题: get_line() 直接用 self.sampled_offsets[block] 索引,不检查越界。如果 bincode 缓存被损坏,普通查询会 运行时 panic 而非返回错误。

let block = idx / BLOCK_SIZE;
let mut pos = self.sampled_offsets[block] as usize;  // ← 越界 panic

建议修复: 添加 LineIndex::validate(data_len) 方法;缓存加载后校验;guard sampled_offsets.get(block)


修复信息:

  • Commit: bef0b44 fix(io): guard sampled_offsets index in get_line() to prevent panic on corrupt cache
  • 修复方式: 将 self.sampled_offsets[block] 直接索引替换为 self.sampled_offsets.get(block)?,越界时返回 None 而非 panic
  • 测试: 全部 38 个 line_index 测试通过,无需新增测试(现有测试已覆盖越界和缓存损坏场景)
**文件**: `core/io/line_index.rs:236` **分类**: 缓存 / 健壮性 **问题**: `get_line()` 直接用 `self.sampled_offsets[block]` 索引,不检查越界。如果 bincode 缓存被损坏,普通查询会 **运行时 panic** 而非返回错误。 ```rust let block = idx / BLOCK_SIZE; let mut pos = self.sampled_offsets[block] as usize; // ← 越界 panic ``` **建议修复**: 添加 `LineIndex::validate(data_len)` 方法;缓存加载后校验;guard `sampled_offsets.get(block)`。 --- **修复信息**: - **Commit**: `bef0b44` fix(io): guard sampled_offsets index in get_line() to prevent panic on corrupt cache - **修复方式**: 将 `self.sampled_offsets[block]` 直接索引替换为 `self.sampled_offsets.get(block)?`,越界时返回 `None` 而非 panic - **测试**: 全部 38 个 line_index 测试通过,无需新增测试(现有测试已覆盖越界和缓存损坏场景)
dailz added the bugseverity/higharea/io labels 2026-06-03 13:52:51 +08:00
dailz closed this issue 2026-06-03 15:39:12 +08:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dailz/logViewer#3