Repository navigation
cmp, diffutils: handle stdout write errors without panicking (#165) #442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -375,6 +375,10 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> { | |
| ¶ms.to.to_string_lossy() | ||
| }; | ||
|
|
||
| stdout | ||
| .flush() | ||
| .map_err(|e| stdout_error(¶ms.executable, &e))?; | ||
|
|
||
| report_eof(at_byte, at_line, start_of_line, eof_on, params); | ||
| return Ok(Cmp::Different); | ||
| } | ||
|
|
@@ -419,15 +423,14 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> { | |
| &mut output, | ||
| params, | ||
| )?; | ||
| stdout.write_all(output.as_slice()).map_err(|e| { | ||
| format!( | ||
| "{}: error printing output: {e}", | ||
| params.executable.to_string_lossy() | ||
| ) | ||
| })?; | ||
| stdout | ||
| .write_all(output.as_slice()) | ||
| .map_err(|e| stdout_error(¶ms.executable, &e))?; | ||
| output.clear(); | ||
| } else { | ||
| report_difference(from_byte, to_byte, at_byte, at_line, params); | ||
| write_difference(&mut stdout, from_byte, to_byte, at_byte, at_line, params) | ||
| .and_then(|()| stdout.flush()) | ||
| .map_err(|e| stdout_error(¶ms.executable, &e))?; | ||
| return Ok(Cmp::Different); | ||
| } | ||
| } | ||
|
|
@@ -451,6 +454,9 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> { | |
| to.consume(consumed); | ||
| } | ||
|
|
||
| stdout | ||
| .flush() | ||
| .map_err(|e| stdout_error(¶ms.executable, &e))?; | ||
| Ok(compare) | ||
| } | ||
|
|
||
|
|
@@ -477,7 +483,20 @@ pub fn main(opts: Peekable<ArgsOs>) -> ExitCode { | |
| }; | ||
|
|
||
| if params.help { | ||
| println!("{}", usage_string(¶ms.executable.to_string_lossy())); | ||
| let mut out = io::stdout().lock(); | ||
| let written = writeln!( | ||
| out, | ||
| "{}", | ||
| usage_string(¶ms.executable.to_string_lossy()) | ||
| ) | ||
| .and_then(|()| out.flush()); | ||
| if let Err(e) = written { | ||
| let msg = stdout_error(¶ms.executable, &e); | ||
| if !msg.is_empty() { | ||
| eprintln!("{msg}"); | ||
| return ExitCode::from(2); | ||
| } | ||
| } | ||
| return ExitCode::SUCCESS; | ||
| } | ||
|
|
||
|
|
@@ -491,7 +510,7 @@ pub fn main(opts: Peekable<ArgsOs>) -> ExitCode { | |
| Ok(Cmp::Equal) => ExitCode::SUCCESS, | ||
| Ok(Cmp::Different) => ExitCode::from(1), | ||
| Err(e) => { | ||
| if !params.quiet { | ||
| if !params.quiet && !e.is_empty() { | ||
| eprintln!("{e}"); | ||
| } | ||
| ExitCode::from(2) | ||
|
|
@@ -698,39 +717,54 @@ fn report_eof( | |
| } | ||
|
|
||
| #[inline] | ||
| fn report_difference( | ||
| fn write_difference<W: Write>( | ||
| out: &mut W, | ||
| from_byte: u8, | ||
| to_byte: u8, | ||
| at_byte: BytesLimitU64, | ||
| at_line: u64, | ||
| params: &Params, | ||
| ) { | ||
| ) -> io::Result<()> { | ||
| if params.quiet { | ||
| return; | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let term = if is_posix_locale("LC_MESSAGES") && !params.print_bytes { | ||
| "char" | ||
| } else { | ||
| "byte" | ||
| }; | ||
| print!( | ||
| write!( | ||
| out, | ||
| "{} {} differ: {term} {}, line {}", | ||
| params.from.to_string_lossy(), | ||
| params.to.to_string_lossy(), | ||
| at_byte, | ||
| at_line | ||
| ); | ||
| )?; | ||
| if params.print_bytes { | ||
| print!( | ||
| write!( | ||
| out, | ||
| " is {:>3o} {} {:>3o} {}", | ||
| from_byte, | ||
| format_visible_byte(from_byte), | ||
| to_byte, | ||
| format_visible_byte(to_byte) | ||
| ); | ||
| )?; | ||
| } | ||
| writeln!(out) | ||
| } | ||
|
|
||
| fn stdout_error(executable: &std::ffi::OsStr, err: &io::Error) -> String { | ||
| if err.kind() == io::ErrorKind::BrokenPipe { | ||
| String::new() | ||
| } else { | ||
| format_failure_to_read_input_file( | ||
| &OsString::from(executable), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. params.executable is already an OsString, why take &OsStr and convert back? |
||
| &OsString::from("standard output"), | ||
| err, | ||
| ) | ||
| } | ||
| println!(); | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,10 +43,20 @@ fn name(binary_path: &Path) -> &OsStr { | |
| const VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
|
||
| fn usage(name: &str) { | ||
| println!("{name} {VERSION} (multi-call binary)\n"); | ||
| println!("Usage: {name} [function [arguments...]]\n"); | ||
| println!("Currently defined functions:\n"); | ||
| println!(" cmp, diff\n"); | ||
| let s = format!( | ||
| "{name} {VERSION} (multi-call binary)\n\nUsage: {name} [function [arguments...]]\n\nCurrently defined functions:\n\n cmp, diff\n" | ||
| ); | ||
| let mut out = std::io::stdout().lock(); | ||
| let written = std::io::Write::write_all(&mut out, s.as_bytes()) | ||
| .and_then(|()| std::io::Write::flush(&mut out)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please |
||
| if let Err(e) = written { | ||
| if e.kind() != std::io::ErrorKind::BrokenPipe { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the write error is printed but we still exit 0 via second_arg_error, no? |
||
| let _ = std::io::Write::write_fmt( | ||
| &mut std::io::stderr().lock(), | ||
| format_args!("{name}: standard output: {e}\n"), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this keeps the "(os error 28)" suffix, cmp and diff strip it |
||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn second_arg_error(name: &OsStr) -> ! { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -421,3 +421,61 @@ fn cmp_difference_term_follows_messages_locale( | |
| .fails_with_code(1) | ||
| .stdout_is(format!("a b differ: {term} 1, line 1\n")); | ||
| } | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| #[test] | ||
| fn write_error() { | ||
| use std::fs::File; | ||
| let (at, ts) = at_and_ts!(); | ||
| at.write("a", "left\n"); | ||
| at.write("b", "right\n"); | ||
|
|
||
| ts.ucmd() | ||
| .args(&["a", "b"]) | ||
| .set_stdout(File::create("/dev/full").unwrap()) | ||
| .fails_with_code(2) | ||
| .stderr_is("cmp: standard output: No space left on device\n"); | ||
| } | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| #[test] | ||
| fn write_error_verbose() { | ||
| use std::fs::File; | ||
| let (at, ts) = at_and_ts!(); | ||
| at.write("a", "left\n"); | ||
| at.write("b", "right\n"); | ||
|
|
||
| ts.ucmd() | ||
| .args(&["-l", "a", "b"]) | ||
| .set_stdout(File::create("/dev/full").unwrap()) | ||
| .fails_with_code(2) | ||
| .stderr_is("cmp: standard output: No space left on device\n"); | ||
| } | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| #[test] | ||
| fn write_error_help() { | ||
| use std::fs::File; | ||
| let (_at, ts) = at_and_ts!(); | ||
|
|
||
| ts.ucmd() | ||
| .arg("--help") | ||
| .set_stdout(File::create("/dev/full").unwrap()) | ||
| .fails_with_code(2) | ||
| .stderr_is("cmp: standard output: No space left on device\n"); | ||
| } | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| #[test] | ||
| fn multicall_write_error() { | ||
| use std::fs::File; | ||
| use std::process::Command; | ||
|
|
||
| let output = Command::new(crate::TESTS_BINARY) | ||
| .stdout(File::create("/dev/full").unwrap()) | ||
| .output() | ||
| .unwrap(); | ||
| assert_ne!(output.status.code(), Some(134)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too weak, please check the exit code and the stderr message |
||
| let stderr = String::from_utf8_lossy(&output.stderr); | ||
| assert!(stderr.contains("Expected utility name as second argument, got nothing.")); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returning an empty string for broken pipe and testing
is_empty()in two places is fragilecould be a dedicated error variant, no?