Skip to content

sed - output already produced is lost when a runtime error stops processing #649

Open

Description

@sylvestre

When sed stops with a runtime error partway through its input, GNU sed has already written the
output for the earlier lines. uutils sed loses it when it reads a file operand and stdout is not
a terminal (a file or a pipe): everything still in the output buffer is dropped.

(The scanner report also mentions w files losing data when the script has a backward branch
loop; that part did not reproduce on current main, so it is left out.)

Reproduction

$ printf 'a\nb\nc\n' > in3

Rust (incorrect)

$ ./target/release/sed '3s//X/' in3 > out.txt; echo "rc=$?"; cat out.txt
sed: <script argument 1>:1:1: 'in3':3 error: no previous regular expression
rc=2
# out.txt is empty

GNU (correct)

$ /usr/bin/sed '3s//X/' in3 > out.txt; echo "rc=$?"; cat out.txt
/usr/bin/sed: -e expression #1, char 0: no previous regular expression
rc=1
a
b

(The exit status and message format also differ; those are separate issues. This one is only
about the lost a and b.)

Root cause

process_all_files in src/sed/processor.rs returns as soon as process_file fails
(src/sed/processor.rs:1005), so the output buffer for the current file is never flushed (nor
is named_writer::flush_all() at src/sed/processor.rs:1025-1026 reached):

context.input_name = path;
process_file(commands.clone(), &mut reader, output, context)?;
...
// Flush all output files
named_writer::flush_all()?;

Test

#[test]
fn test_output_kept_on_runtime_error() {
    let temp_dir = assert_fs::TempDir::new().unwrap();
    let input = temp_dir.child("in");
    input.write_str("one\ntwo\nthree\n").unwrap();

    new_ucmd!()
        .args(&["3s//X/", input.path().to_str().unwrap()])
        .fails()
        .stdout_is("one\ntwo\n");
}

The output is lost with a pipe as well as with a file (sed '3s//X/' in3 | cat prints nothing),
so the default uutests capture should show the bug.

Reported by Anthropic's OSS Scanner (ANT-2026-D6QYNXKH).

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