Skip to content

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

Open

Description

@sylvestre

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).

Activity

  1. added
    bug
    good first issue
    oss-scanner
    gnu-compat
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-compatgood first issueoss-scanner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions