Skip to content

head: fix panic on write error when printing a long -v filename header#13268

Open
koopatroopa787 wants to merge 1 commit into
uutils:mainfrom
koopatroopa787:fix-head-verbatim-write-panic
Open

head: fix panic on write error when printing a long -v filename header#13268
koopatroopa787 wants to merge 1 commit into
uutils:mainfrom
koopatroopa787:fix-head-verbatim-write-panic

Conversation

@koopatroopa787

Copy link
Copy Markdown
Contributor

Summary

head -v prints a ==> <filename> <== header for each file. The
filename was written with print_verbatim(file).unwrap(). If stdout
is a full device (ENOSPC) and the filename is longer than the
LineWriter internal buffer (~1024 bytes), write_all inside
print_verbatim flushes mid-write, the write error is returned to
.unwrap(), and the process panics (exit 134) instead of exiting
cleanly.

A short filename stays entirely within the buffer, so its error only
surfaces at the next ?-checked writeln! — those fail gracefully.
This asymmetry means only long filenames trigger the panic.

$ LONG="/dev/$(python3 -c "print('./'*512)")null"   # >1024-byte path
$ head -v "$LONG" > /dev/full
thread 'main' panicked at src/uu/head/src/head.rs:463:42:
called `Result::unwrap()` on an `Err` value: Os { code: 28, kind: StorageFull, ... }
Aborted (core dumped)

GNU head reports the error and exits with code 1.

Fix

Replace .unwrap() with ? so the error propagates through the
enclosing UResult<()> closure, exactly like the surrounding
write!/writeln! calls.

Changes

  • src/uu/head/src/head.rs line 463: .unwrap()?
  • tests/by-util/test_head.rs: regression test using /dev/full and a

    1024-byte path built from repeated ./ segments.

Fixes #13264

When head prints the ==> <filename> <== header for -v/multi-file mode,
the filename was written with print_verbatim(file).unwrap().  If stdout
is a full device (ENOSPC) and the filename is longer than the internal
LineWriter buffer (~1024 bytes), the write_all inside print_verbatim
flushes mid-write, the write error is returned to .unwrap(), and the
process panics (exit 134).

A short filename stays entirely within the buffer, so its error only
surfaces at the next ?-checked write — those fail gracefully.  The
asymmetry meant only long filenames triggered the panic.

Fix: replace .unwrap() with ? so the error is propagated to the
enclosing closure, which already returns UResult<()>, and is handled the
same way as the surrounding write!/writeln! calls.

Adds a regression test using /dev/full and a >1024-byte path built from
repeated "./" segments that the OS resolves to /dev/null.

Fixes uutils#13264
Copilot AI review requested due to automatic review settings July 4, 2026 08:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/tail/tail-n0f (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/symlink (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/printf/printf-surprise is now being skipped but was previously passing.

@oech3

oech3 commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

duplicated PR

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.

head panics (aborts) on a write error while printing a long -v filename header

3 participants