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

Abierto Apto para principiantes
#649 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@Himanshu-singh04 ya está trabajando en esto.

Desde el 10/11/2026.

  • #668 de @Himanshu-singh04 — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
86/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
rust
Área
cli

Línea de trabajo

Lee process_all_files y process_file en src/sed/processor.rs y céntrate en cómo se devuelven los errores antes de vaciar la salida. Reprodúcelo con el caso proporcionado test_output_kept_on_runtime_error y confirma que la prueba captura one\ntwo\n después del error en tiempo de ejecución. Se considera terminado cuando se conserva la salida almacenada en búfer de las líneas anteriores y el comando sigue indicando un fallo.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
Rust
Estrellas
123
Forks
53
Merge medio
2 d 8 h
PR fusionados (30 d)
40

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de uutils/sed

Todos los issues de uutils/sed

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.