Skip to content

Commit 2b10153

Browse files
authored
wc: stop processing --files0-from input after stdout write failure (#11023)
* fix(wc): stop processing after stdout write error When wc encounters a write error to stdout (e.g., when stdout is redirected to /dev/full), it should stop processing immediately rather than continuing to process remaining files. This matches GNU wc behavior and prevents unnecessary processing after the output is already broken. * fix(wc): improve error message matching in test_files0_stops_after_stdout_write_error
1 parent 84165be commit 2b10153

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/uu/wc/src/wc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ fn wc(inputs: &Inputs, settings: &Settings) -> UResult<()> {
988988
if let Err(err) = print_stats(settings, &word_count, maybe_title_str, number_width) {
989989
let title = maybe_title_str.unwrap_or(OsStr::new("<stdin>"));
990990
show!(err.map_err_context(|| translate!("wc-error-failed-to-print-result", "title" => title.to_string_lossy())));
991+
return Ok(());
991992
}
992993
}
993994
// Print deferred error after stats to match GNU wc output order

tests/by-util/test_wc.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,30 @@ fn test_files0_progressive_stream() {
763763
.stdout_only("36 370 2189 total\n");
764764
}
765765

766+
#[cfg(target_os = "linux")]
767+
#[test]
768+
fn test_files0_stops_after_stdout_write_error() {
769+
use std::fs::OpenOptions;
770+
771+
let dev_full = OpenOptions::new().write(true).open("/dev/full").unwrap();
772+
773+
let stderr = new_ucmd!()
774+
.args(&["--files0-from=-", "--total=never"])
775+
.set_stdout(dev_full)
776+
.pipe_in(b"/dev/null\0/dev/null\0/dev/null\0")
777+
.fails()
778+
.stderr_str()
779+
.to_string();
780+
781+
assert_eq!(
782+
stderr
783+
.matches("failed to print result for /dev/null")
784+
.count(),
785+
1,
786+
"wc should stop after the first stdout write error: {stderr:?}"
787+
);
788+
}
789+
766790
#[test]
767791
fn files0_from_dir() {
768792
// On Unix, `read(open("."))` fails. On Windows, `open(".")` fails. Thus, the errors happen in

0 commit comments

Comments
 (0)