🔴 [H1] 文件缩小后 mmap 未处理 → SIGBUS 崩溃 #1

Closed
opened 2026-06-03 13:52:50 +08:00 by dailz · 1 comment
Owner

文件: core/io/file_reader.rs:116
分类: 内存安全

问题: update_for_append() 检测到 new_size <= old_size 时直接返回 Ok(0),不重新 mmap。如果文件被截断/轮转,后续访问旧 mmap 范围会触发 SIGBUS 进程崩溃

if new_size <= old_size {
    return Ok(0);  // ← 文件缩小被忽略!
}

影响: log viewer 场景下文件经常被外部轮转/截断,但代码假设文件在 mmap 期间不变。

建议修复: 检测 new_size < old_size 时强制 reload() 或返回错误要求重新映射/索引。

**文件**: `core/io/file_reader.rs:116` **分类**: 内存安全 **问题**: `update_for_append()` 检测到 `new_size <= old_size` 时直接返回 `Ok(0)`,不重新 mmap。如果文件被截断/轮转,后续访问旧 mmap 范围会触发 **SIGBUS 进程崩溃**。 ```rust if new_size <= old_size { return Ok(0); // ← 文件缩小被忽略! } ``` **影响**: log viewer 场景下文件经常被外部轮转/截断,但代码假设文件在 mmap 期间不变。 **建议修复**: 检测 `new_size < old_size` 时强制 `reload()` 或返回错误要求重新映射/索引。
dailz added the bugseverity/higharea/io labels 2026-06-03 13:52:50 +08:00
dailz closed this issue 2026-06-03 14:50:35 +08:00
Author
Owner

Fixed in b6e655b.

Root cause: update_for_append() returned Ok(0) when new_size <= old_size, leaving a stale mmap that would SIGBUS on access.

Fix: Introduced AppendStatus enum (Unchanged/Appended/Reloaded). When file shrinks, reload() rebuilds the mmap and line index. TUI layer clamps cursor and invalidates viewport on Reloaded, matching existing handle_file_truncated() behavior.

Fixed in b6e655b. Root cause: `update_for_append()` returned `Ok(0)` when `new_size <= old_size`, leaving a stale mmap that would SIGBUS on access. Fix: Introduced `AppendStatus` enum (`Unchanged`/`Appended`/`Reloaded`). When file shrinks, `reload()` rebuilds the mmap and line index. TUI layer clamps cursor and invalidates viewport on `Reloaded`, matching existing `handle_file_truncated()` behavior.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dailz/logViewer#1