Skip to content

Commit d0cfa2d

Browse files
committed
fix: handle write errors gracefully instead of panicking
Fixes #9769 Changed error.print().unwrap() to let _ = error.print() to prevent panic when writing to /dev/full. Added regression test in test_cat.rs.
1 parent 666c6df commit d0cfa2d

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

.vscode/cspell.dictionaries/workspace.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ LINESIZE
182182
NAMESIZE
183183
RTLD_NEXT
184184
RTLD
185+
SIGABRT
185186
SIGINT
186187
SIGKILL
187188
SIGSTOP

src/uucore/src/lib/mods/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ impl Error for ClapErrorWrapper {}
748748
// This is abuse of the Display trait
749749
impl Display for ClapErrorWrapper {
750750
fn fmt(&self, _f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
751-
self.error.print().unwrap();
751+
let _ = self.error.print();
752752
Ok(())
753753
}
754754
}

tests/by-util/test_cat.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,37 @@ fn test_child_when_pipe_in() {
833833
ts.ucmd().pipe_in("content").run().stdout_is("content");
834834
}
835835

836+
/// Regression test for GitHub issue #9769
837+
/// https://github.com/uutils/coreutils/issues/9769
838+
///
839+
/// Bug: Utilities panic when output is redirected to /dev/full
840+
/// Location: src/uucore/src/lib/mods/error.rs:751 - `.unwrap()` causes panic
841+
///
842+
/// This test verifies that cat handles write errors to /dev/full gracefully
843+
/// instead of panicking with exit code 134 (SIGABRT).
844+
///
845+
/// Expected behavior with current BUGGY code:
846+
/// - Test WILL FAIL (cat panics with exit code 134)
847+
///
848+
/// Expected behavior after fix:
849+
/// - Test SHOULD PASS (cat exits gracefully with error code 1)
850+
// Regression test for issue #9769: graceful error handling when writing to /dev/full
851+
#[test]
852+
#[cfg(target_os = "linux")]
853+
fn test_write_error_handling() {
854+
use std::fs::File;
855+
856+
let dev_full =
857+
File::create("/dev/full").expect("Failed to open /dev/full - test must run on Linux");
858+
859+
new_ucmd!()
860+
.pipe_in("test content that should cause write error to /dev/full")
861+
.set_stdout(dev_full)
862+
.fails()
863+
.code_is(1)
864+
.stderr_contains("No space left on device");
865+
}
866+
836867
#[test]
837868
fn test_cat_eintr_handling() {
838869
// Test that cat properly handles EINTR (ErrorKind::Interrupted) during I/O operations

0 commit comments

Comments
 (0)