🟡 [M20] append_lines 吞掉行计数 I/O 错误并从 0 继续编号 #45

Closed
opened 2026-06-05 11:54:15 +08:00 by dailz · 1 comment
Owner

问题

append_linescount_existing_lines(path).unwrap_or(0) 会在行计数失败时静默把现有行数当作 0。

影响

如果文件在打开后被删除、权限变化或读取失败,追加行编号会从 0 重新开始,调用方无法感知底层 I/O 错误。

位置

  • crates/bench/src/data_gen.rs:90

建议

count_existing_lines(path)? 向上传播错误,保持 append_linesstd::io::Result<()> 语义。

## 问题 `append_lines` 中 `count_existing_lines(path).unwrap_or(0)` 会在行计数失败时静默把现有行数当作 0。 ## 影响 如果文件在打开后被删除、权限变化或读取失败,追加行编号会从 0 重新开始,调用方无法感知底层 I/O 错误。 ## 位置 - `crates/bench/src/data_gen.rs:90` ## 建议 让 `count_existing_lines(path)?` 向上传播错误,保持 `append_lines` 的 `std::io::Result<()>` 语义。
Author
Owner

修复方案

提交: f6081b9

改动内容

1. append_lines — 先计数再打开 + 错误传播

- let existing_lines = count_existing_lines(path).unwrap_or(0);
+ let existing_lines = count_existing_lines(path)?;

将计数移到文件打开之前,unwrap_or(0) 改为 ? 向上传播错误,保持 std::io::Result<()> 语义。

2. count_existing_lines — 逐行传播读取错误

// 之前: reader.lines().count() 会静默丢弃每行的 I/O 错误
// 之后: 显式循环逐行 line? 传播错误
let mut count = 0u64;
for line in reader.lines() {
    line?;
    count += 1;
}

验证

  • 63 个测试全部通过,含 test_append_lines_increases_line_count
  • clippy 无新增警告
## 修复方案 **提交**: f6081b9 ### 改动内容 **1. `append_lines` — 先计数再打开 + 错误传播** ```diff - let existing_lines = count_existing_lines(path).unwrap_or(0); + let existing_lines = count_existing_lines(path)?; ``` 将计数移到文件打开之前,`unwrap_or(0)` 改为 `?` 向上传播错误,保持 `std::io::Result<()>` 语义。 **2. `count_existing_lines` — 逐行传播读取错误** ```rust // 之前: reader.lines().count() 会静默丢弃每行的 I/O 错误 // 之后: 显式循环逐行 line? 传播错误 let mut count = 0u64; for line in reader.lines() { line?; count += 1; } ``` ### 验证 - 63 个测试全部通过,含 `test_append_lines_increases_line_count` - clippy 无新增警告
dailz closed this issue 2026-06-07 09:14:16 +08:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dailz/logViewer#45