Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/uu/tr/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,16 @@
let filtered = buf.iter().filter_map(|&c| translator.translate(c));
output_buf.extend(filtered);

output
.write_all(&output_buf)
.map_err_context(|| get_message("tr-error-write-error"))?;
if let Err(err) = output.write_all(&output_buf) {
// Treat broken pipe as a successful termination, which is the
// expected behavior when stdout is a pipeline and the downstream
// process terminates.
if err.kind() == std::io::ErrorKind::BrokenPipe {
break;

Check warning on line 716 in src/uu/tr/src/operation.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/tr/src/operation.rs#L715-L716

Added lines #L715 - L716 were not covered by tests
} else {
return Err(err.map_err_context(|| get_message("tr-error-write-error")));

Check warning on line 718 in src/uu/tr/src/operation.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/tr/src/operation.rs#L718

Added line #L718 was not covered by tests
}
}

buf.clear();
output_buf.clear();
Expand Down
11 changes: 7 additions & 4 deletions src/uu/tr/src/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use operation::{
};
use std::collections::HashMap;
use std::ffi::OsString;
use std::io::{BufWriter, Write, stdin, stdout};
use std::io::{BufWriter, ErrorKind, Write, stdin, stdout};
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::fs::is_stdin_directory;
Expand Down Expand Up @@ -157,9 +157,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
translate_input(&mut locked_stdin, &mut buffered_stdout, op)?;
}

buffered_stdout
.flush()
.map_err_context(|| get_message("tr-error-write-error"))?;
// Handle broken pipe errors gracefully during flush.
match buffered_stdout.flush() {
Ok(()) => {}
Err(err) if err.kind() == ErrorKind::BrokenPipe => {}
Err(err) => return Err(err.map_err_context(|| get_message("tr-error-write-error"))),
}

Ok(())
}
Expand Down
10 changes: 10 additions & 0 deletions tests/by-util/test_tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,3 +1554,13 @@ fn test_failed_write_is_reported() {
.fails()
.stderr_is("tr: write error: No space left on device\n");
}

#[test]
fn test_broken_pipe_no_error() {
new_ucmd!()
.args(&["e", "a"])
.pipe_in("hello".repeat(100))
.run_stdout_starts_with(b"")
.success()
.stderr_is("");
}
Loading