fix: eliminate TOCTOU race in IndexCache::save (closes #5)

spawn_indexer builds LineIndex from mmap snapshot but IndexCache::save()
re-opened the file to compute the hash. If the file changed between those
two steps, the cached index would be stored under the wrong hash.

- Add IndexCache::save_with_hash() that computes hash from in-memory data
- Add compute_data_hash() public function (same algorithm as compute_file_hash)
- Update spawn_indexer, FileReader::save_cache, and test callers
This commit is contained in:
dailz
2026-06-04 13:30:16 +08:00
parent bef0b44e91
commit 1bb6b2e9f3
5 changed files with 66 additions and 5 deletions

View File

@@ -1039,7 +1039,7 @@ mod tests {
fn load_file_ready(app: &mut App, path: &std::path::Path) {
let data = std::fs::read(path).unwrap();
let index = log_viewer_core::io::line_index::LineIndex::from_bytes(&data);
let _ = log_viewer_core::io::index_cache::IndexCache::save(path, &index);
let _ = log_viewer_core::io::index_cache::IndexCache::save_with_hash(path, &index, &data);
app.load_file(path.to_str().unwrap()).unwrap();
}