From 2a9b4eaf90930128cd4b3cdba0796b56512a4d7a Mon Sep 17 00:00:00 2001 From: weili <541602953@qq.com> Date: Fri, 3 Jul 2026 07:53:02 +0000 Subject: [PATCH] head: don't panic on a write error while printing a verbose header The verbose `==> name <==` header wrote the filename with `print_verbatim(file).unwrap()`, while the surrounding writes use `?`. When the filename is longer than the stdout buffer, `write_all` flushes mid-write, so a write error (e.g. stdout on a full device) is raised inside the `unwrap` and aborts the process instead of being reported. Propagate the error with `?` like the neighbouring header writes, so a write failure exits non-zero with a message instead of panicking, regardless of filename length. --- src/uu/head/src/head.rs | 2 +- tests/by-util/test_head.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/uu/head/src/head.rs b/src/uu/head/src/head.rs index 955b3334dfd..0fc28fb6911 100644 --- a/src/uu/head/src/head.rs +++ b/src/uu/head/src/head.rs @@ -460,7 +460,7 @@ fn uu_head(options: &HeadOptions) -> UResult<()> { writeln!(stdout)?; } write!(stdout, "==> ")?; - print_verbatim(file).unwrap(); + print_verbatim(file)?; writeln!(stdout, " <==")?; first = false; } diff --git a/tests/by-util/test_head.rs b/tests/by-util/test_head.rs index 0f4e9dae5dd..e2edbed6c09 100644 --- a/tests/by-util/test_head.rs +++ b/tests/by-util/test_head.rs @@ -919,6 +919,22 @@ fn test_write_to_dev_full() { } } +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))] +#[test] +fn test_verbose_header_long_name_write_error_no_panic() { + use std::fs::OpenOptions; + + let dev_full = OpenOptions::new().write(true).open("/dev/full").unwrap(); + + let long_name = format!("/dev/{}null", "./".repeat(512)); + + new_ucmd!() + .args(&["-v", &long_name]) + .set_stdout(dev_full) + .fails_with_code(1) + .stderr_contains("head: No space left on device"); +} + #[test] #[cfg(target_os = "linux")] #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]