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

Aperta Adatta ai principianti
#649 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Himanshu-singh04 ci sta già lavorando.

Dal 10/11/2026.

  • #668 di @Himanshu-singh04 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
86/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
cli

Direzione di ricerca

Leggi process_all_files e process_file in src/sed/processor.rs, concentrandoti su come vengono restituiti gli errori prima che l'output venga svuotato. Riproduci il problema con il caso fornito test_output_kept_on_runtime_error e conferma che il test acquisisce one\ntwo\n dopo l'errore di runtime. Il lavoro è terminato quando l'output bufferizzato delle righe precedenti viene conservato e il comando continua a segnalare un errore.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug gnu-compat good first issue oss-scanner

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

Lingua principale
Rust
Stelle
123
Fork
53
Merge medio
2g 8h
PR unite (30g)
40

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di uutils/sed

Tutte le issue di uutils/sed

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.