Repository navigation
cmp, diffutils: handle stdout write errors without panicking (#165) - #442
yunusarda-sudo wants to merge 2 commits into
Conversation
| let written = std::io::Write::write_all(&mut out, s.as_bytes()) | ||
| .and_then(|()| std::io::Write::flush(&mut out)); | ||
| if let Err(e) = written { | ||
| if e.kind() != std::io::ErrorKind::BrokenPipe { |
There was a problem hiding this comment.
the write error is printed but we still exit 0 via second_arg_error, no?
should be 2 like cmp/diff
| if e.kind() != std::io::ErrorKind::BrokenPipe { | ||
| let _ = std::io::Write::write_fmt( | ||
| &mut std::io::stderr().lock(), | ||
| format_args!("{name}: standard output: {e}\n"), |
There was a problem hiding this comment.
this keeps the "(os error 28)" suffix, cmp and diff strip it
could you please reuse format_failure_to_read_input_file?
| ); | ||
| 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)); |
There was a problem hiding this comment.
please use std::io::Write; instead of the fully qualified calls
| .stdout(File::create("/dev/full").unwrap()) | ||
| .output() | ||
| .unwrap(); | ||
| assert_ne!(output.status.code(), Some(134)); |
There was a problem hiding this comment.
too weak, please check the exit code and the stderr message
| writeln!(out) | ||
| } | ||
|
|
||
| fn stdout_error(executable: &std::ffi::OsStr, err: &io::Error) -> String { |
There was a problem hiding this comment.
returning an empty string for broken pipe and testing is_empty() in two places is fragile
could be a dedicated error variant, no?
| String::new() | ||
| } else { | ||
| format_failure_to_read_input_file( | ||
| &OsString::from(executable), |
There was a problem hiding this comment.
params.executable is already an OsString, why take &OsStr and convert back?
When writing to a full or unbuffered stdout device (such as
/dev/full),cmpand the multicall binarydiffutilspanicked inside Rust'sprintln!macro withfailed printing to stdout: No space left on device (os error 28)(exit status 134).Changes
src/cmp.rs: Write differences and verbose output directly to the locked stdout writer, reporting write errors ascmp: standard output: <error>with exit status 2 (matching GNU cmp anddiff).cmp --helpwithout panicking.src/main.rs: Safely write usage text without panicking when stdout is full.tests/by-util/test_cmp.rsfor default cmp, verbose cmp (-l), help output, and the multicall binary.Fixes #165