sed - with `-s` or `-i`, the file read by `R` is not restarted for each input file
维护者通常 1 天内回复
评估
调研方向
从 src/sed/named_reader.rs 以及 src/sed/processor.rs 中 process_all_files 附近的重置逻辑入手;检查独立输入文件开始时如何处理 context.named_readers。添加或使用 issue 中类似的回归测试,然后运行它,以确认使用 -s 和 -i 时,R 命令会读取每个文件的头部首行。
由索引模型根据 Issue 内容生成。
描述
With -s (separate) or -i (which implies separate files), GNU sed restarts every file read by
the R command at the beginning of each input file. uutils sed keeps one cursor for the whole
run, so sed -i '1R header' f1 f2 inserts the first line of header into f1 and the second
line into f2; once header is used up, later files get nothing. Without -s/-i both
implementations agree.
Reproduction
$ printf 'h1\nh2\n' > hdr; printf 'a\n' > fa; printf 'b\n' > fb
Rust (incorrect)
$ ./target/release/sed -s '1R hdr' fa fb
a
h1
b
h2
# Exit code: 0
GNU (correct)
$ /usr/bin/sed -s '1R hdr' fa fb
a
h1
b
h1
# Exit code: 0
Root cause
NamedReader (src/sed/named_reader.rs) only moves forward: Unopened -> Open ->
Exhausted, with no way to go back. process_all_files resets per-file state when a new
separate file starts (src/sed/processor.rs:995-1002), but not the named readers:
if context.separate || index == 0 {
context.line_number = 0;
reset_latched_address_ranges(&mut context.range_commands);
// Reset hold space for separate file processing
context.hold.content.clear();
context.hold.has_newline = true;
}
The readers live in context.named_readers (src/sed/command.rs:75).
Test
#[test]
fn test_r_command_restarts_for_each_separate_file() {
let temp_dir = assert_fs::TempDir::new().unwrap();
let header = temp_dir.child("header");
header.write_str("first\nsecond\n").unwrap();
let one = temp_dir.child("one");
one.write_str("x\n").unwrap();
let two = temp_dir.child("two");
two.write_str("y\n").unwrap();
new_ucmd!()
.args(&[
"-s",
&format!("1R {}", header.path().display()),
one.path().to_str().unwrap(),
two.path().to_str().unwrap(),
])
.succeeds()
.stdout_is("x\nfirst\ny\nfirst\n");
}
Reported by Anthropic's OSS Scanner (ANT-2026-D4GHQSKO).
- 主要语言
- Rust
- 星标
- 123
- 派生
- 53
- 平均合并
- 2 天 8 小时
- 30 天内合并 PR
- 40
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
uutils/sed 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
sed - with `--posix`, `-e 'i\'` takes the next `-e` as its text可能已有人在做 @yunusarda-sudo 于 14 天前认领。 未关闭bug gnu-compat good first issue
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
sed - script parser skips CR, VT, FF (and 0x85, 0xA0) as blanks; GNU skips only space and tab可能已有人在做 @Himanshu-singh04 于 22 天前认领。 未关闭bug gnu-compat good first issue oss-scanner
难度 2/5 1-3 小时 新手友好度 89/100
维护者通常 1 天内回复
-
sed - output already produced is lost when a runtime error stops processing可能已有人在做 @Himanshu-singh04 于 21 天前认领。 未关闭bug gnu-compat good first issue oss-scanner
难度 2/5 1-3 小时 新手友好度 86/100
维护者通常 1 天内回复
-
sed - unknown POSIX class names like `[[:foo:]]` or `[[:word:]]` are accepted instead of rejected可能已有人在做 @draarivpatel-ui 于 7 天前认领。 未关闭bug gnu-compat good first issue oss-scanner
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
相似的 Issue
-
autofix:working
难度 2/5 1-3 小时 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 75/100
libracoder/kiri#1138 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 83/100
roc-lang/basic-webserver#277 ·
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 82/100
samkerrcode/opengent#113 ·
维护者通常 1 天内回复
-
app bug session
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复