From 7fc1b16115c84599b68f9c7b8468857cf157be1f Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Sun, 18 Oct 2020 10:47:27 -0700 Subject: [PATCH] sort: fix panic on write to closed pipe If the output of sort is piped to another program that closes the file descriptor, sort currently panics. The GNU coreutils is able to handle this case. Replacing panic with crash_if_err reports the closed pipe and exits with a return code, which seems like the correct behavior. Tested on my Mac and the panic disappears. Add a test which pipes data to sort - it won't protect against this specific regression, but it increases the test coverage, at least. Fixes #1608. --- src/uu/sort/src/sort.rs | 5 +---- tests/by-util/test_sort.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index dc4dc1d90..11d243bba 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -550,10 +550,7 @@ where for line in iter { let str = format!("{}\n", line); - if let Err(e) = file.write_all(str.as_bytes()) { - show_error!("sort: {0}", e.to_string()); - panic!("Write failed"); - } + crash_if_err!(1, file.write_all(str.as_bytes())) } } diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index d3a4e6397..9ff1b3522 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -118,6 +118,19 @@ fn test_merge_reversed() { .stdout_only_fixture("merge_ints_reversed.expected"); } +#[test] +fn test_pipe() { + // TODO: issue 1608 reports a panic when we attempt to read from stdin, + // which was closed by the other side of the pipe. This test does not + // protect against regressions in that case; we should add one at some + // point. + new_ucmd!() + .pipe_in("one\ntwo\nfour") + .succeeds() + .stdout_is("four\none\ntwo\n") + .stderr_is(""); +} + #[test] fn test_check() { new_ucmd!()