1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-15 11:36:16 +00:00

Merge pull request #2417 from miDeb/sort/no-sigpipe-panic

sort: avoid sigpipe errors
This commit is contained in:
Terts Diepraam 2021-06-17 10:34:13 +02:00 committed by GitHub
commit de069190b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 8 deletions

View file

@ -255,7 +255,7 @@ fn write<I: WriteableTmpFile>(
fn write_lines<'a, T: Write>(lines: &[Line<'a>], writer: &mut T, separator: u8) { fn write_lines<'a, T: Write>(lines: &[Line<'a>], writer: &mut T, separator: u8) {
for s in lines { for s in lines {
crash_if_err!(1, writer.write_all(s.line.as_bytes())); writer.write_all(s.line.as_bytes()).unwrap();
crash_if_err!(1, writer.write_all(&[separator])); writer.write_all(&[separator]).unwrap();
} }
} }

View file

@ -384,13 +384,13 @@ impl<'a> Line<'a> {
fn print(&self, writer: &mut impl Write, settings: &GlobalSettings) { fn print(&self, writer: &mut impl Write, settings: &GlobalSettings) {
if settings.zero_terminated && !settings.debug { if settings.zero_terminated && !settings.debug {
crash_if_err!(1, writer.write_all(self.line.as_bytes())); writer.write_all(self.line.as_bytes()).unwrap();
crash_if_err!(1, writer.write_all(b"\0")); writer.write_all(b"\0").unwrap();
} else if !settings.debug { } else if !settings.debug {
crash_if_err!(1, writer.write_all(self.line.as_bytes())); writer.write_all(self.line.as_bytes()).unwrap();
crash_if_err!(1, writer.write_all(b"\n")); writer.write_all(b"\n").unwrap();
} else { } else {
crash_if_err!(1, self.print_debug(settings, writer)); self.print_debug(settings, writer).unwrap();
} }
} }

View file

@ -8,7 +8,7 @@ pub fn mute_sigpipe_panic() {
let hook = panic::take_hook(); let hook = panic::take_hook();
panic::set_hook(Box::new(move |info| { panic::set_hook(Box::new(move |info| {
if let Some(res) = info.payload().downcast_ref::<String>() { if let Some(res) = info.payload().downcast_ref::<String>() {
if res.contains("Broken pipe") { if res.contains("BrokenPipe") {
return; return;
} }
} }

View file

@ -913,3 +913,16 @@ fn test_merge_batch_size() {
.succeeds() .succeeds()
.stdout_only_fixture("merge_ints_interleaved.expected"); .stdout_only_fixture("merge_ints_interleaved.expected");
} }
#[test]
fn test_sigpipe_panic() {
let mut cmd = new_ucmd!();
let mut child = cmd.args(&["ext_sort.txt"]).run_no_wait();
// Dropping the stdout should not lead to an error.
// The "Broken pipe" error should be silently ignored.
drop(child.stdout.take());
assert_eq!(
String::from_utf8(child.wait_with_output().unwrap().stderr),
Ok(String::new())
);
}