From cef9aacdb421ff675b16488a22955a13f4dbdf8f Mon Sep 17 00:00:00 2001 From: Floze <88098863+floze-the-genius@users.noreply.github.com> Date: Mon, 10 Aug 2026 23:42:45 +0400 Subject: [PATCH] printf: omit errno from write errors --- src/uucore/src/lib/features/format/mod.rs | 7 +++++-- tests/by-util/test_printf.rs | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/uucore/src/lib/features/format/mod.rs b/src/uucore/src/lib/features/format/mod.rs index 1f1c79de556..73db2b4ba28 100644 --- a/src/uucore/src/lib/features/format/mod.rs +++ b/src/uucore/src/lib/features/format/mod.rs @@ -42,7 +42,10 @@ use crate::extendedbigdecimal::ExtendedBigDecimal; pub use argument::{FormatArgument, FormatArguments}; use self::{escape::parse_escape_code, num_format::Formatter}; -use crate::{NonUtf8OsStrError, error::UError}; +use crate::{ + NonUtf8OsStrError, + error::{UError, strip_errno}, +}; pub use spec::Spec; use std::{ error::Error, @@ -113,7 +116,7 @@ impl Display for FormatError { Self::InvalidPrecision(precision) => write!(f, "invalid precision: '{precision}'"), // TODO: Error message below needs some work Self::WrongSpecType => write!(f, "wrong % directive type was given"), - Self::IoError(e) => write!(f, "write error: {e}"), + Self::IoError(e) => write!(f, "write error: {}", strip_errno(e)), Self::NoMoreArguments => write!(f, "no more arguments"), Self::InvalidArgument(_) => write!(f, "invalid argument"), Self::MissingHex => write!(f, "missing hexadecimal number in escape"), diff --git a/tests/by-util/test_printf.rs b/tests/by-util/test_printf.rs index fda7f7d158e..665eac5acd1 100644 --- a/tests/by-util/test_printf.rs +++ b/tests/by-util/test_printf.rs @@ -1507,6 +1507,18 @@ fn test_emoji_formatting() { .stdout_only("Status: Success 🚀 🎯 Count: 42\n"); } +#[cfg(target_os = "linux")] +#[test] +fn test_write_error_omits_errno() { + let dev_full = std::fs::File::create("/dev/full").expect("failed to open /dev/full"); + + new_ucmd!() + .arg("\n") + .set_stdout(dev_full) + .fails_with_code(1) + .stderr_only("printf: write error: No space left on device\n"); +} + #[test] fn test_large_width_format() { // Test that extremely large width specifications fail gracefully with an error