Skip to content

diff: exit 141 on a closed output pipe instead of panicking - #282

Open
MsfPablo wants to merge 1 commit into
uutils:mainfrom
MsfPablo:diff-broken-pipe-244
Open

diff: exit 141 on a closed output pipe instead of panicking#282
MsfPablo wants to merge 1 commit into
uutils:mainfrom
MsfPablo:diff-broken-pipe-244

Conversation

@MsfPablo

Copy link
Copy Markdown

Summary

diff's output write is a bare .unwrap() (normal/context/unified/ed path in src/diff.rs:94, side-by-side path in src/side_diff.rs), so when the reader closes the pipe early (diff big1 big2 | head) the BrokenPipe error is unwrapped and the process panics/aborts (exit 134) instead of dying quietly to SIGPIPE like GNU (exit 141).

Fix

Added exit_on_broken_pipe_or_panic in src/utils.rs: on ErrorKind::BrokenPipe it exits with 141 (matching GNU); any other write error still panics, since that would be a genuine bug. Wired it into both write sites.

Test plan

  • cargo build — clean
  • cargo test — 21 passed
  • cargo fmt -- --check / cargo clippy --all-targets — clean (pre-existing warnings in unrelated test code untouched)
  • Manually reproduced with diff big1 big2 | head -1 — before: panic, exit 134; after: exit 141
  • Same for diff -y big1 big2 | head -1

Fixes #244

🤖 Generated with Claude Code

diff writes the full result buffer (and side-by-side writes each line)
with a bare .unwrap(), so a reader closing early (diff big1 big2 | head)
turns a BrokenPipe error into a panic and a core dump (exit 134). GNU
diff dies quietly to SIGPIPE (exit 141) in the same situation. Handle
BrokenPipe explicitly at both write sites and exit(141); any other
write error still panics since it would be a genuine bug.

Fixes uutils#244
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

diff panics (broken-pipe unwrap) when its stdout is closed early

1 participant