sed - with `-s` or `-i`, the file read by `R` is not restarted for each input file

未关闭 适合新手
#653 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Himanshu-singh04 已经在做这个了。

开始于 2026年11月10日。

  • #666 来自 @Himanshu-singh04 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
84/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
cli

调研方向

从 src/sed/named_reader.rs 以及 src/sed/processor.rs 中 process_all_files 附近的重置逻辑入手;检查独立输入文件开始时如何处理 context.named_readers。添加或使用 issue 中类似的回归测试,然后运行它,以确认使用 -s 和 -i 时,R 命令会读取每个文件的头部首行。

由索引模型根据 Issue 内容生成。

描述

bug gnu-compat good first issue oss-scanner

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 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

uutils/sed 的其他 Issue

查看 uutils/sed 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。