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

オープン 初心者向け
#649 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@Himanshu-singh04 がすでに取り組んでいます。

2026年11月10日 から。

  • #668 @Himanshu-singh04 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
86/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust
領域
cli

調査の方向性

src/sed/processor.rs の process_all_files と process_file を読み、出力がフラッシュされる前にエラーがどのように返されるかに注目してください。提供された test_output_kept_on_runtime_error のケースで再現し、実行時エラーの後にテストが one\ntwo\n を取得することを確認してください。以前の行のバッファー済み出力が保持され、コマンドが引き続き失敗を報告すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
Rust
スター
123
フォーク
53
平均マージ
2日 8時間
マージ済み PR(30日)
40

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

uutils/sed のほかの issue

uutils/sed の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。