Skip to content

Commit b00dd7b

Browse files
author
Gunter Schmidt
committed
fix: panic on write to /dev/full
* fixes issue #165 * simpler format of io errors (remove (os error 2))
1 parent 5582641 commit b00dd7b

4 files changed

Lines changed: 31 additions & 13 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmp.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE-*
44
// files that was distributed with this source code.
55

6-
use crate::utils::format_failure_to_read_input_file;
6+
use crate::utils::{format_failure_to_read_input_file, format_io_error};
77
use std::env::{self, ArgsOs};
88
use std::ffi::OsString;
99
use std::io::{BufRead, BufReader, BufWriter, Read, Write};
@@ -441,7 +441,7 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {
441441
})?;
442442
output.clear();
443443
} else {
444-
report_difference(from_byte, to_byte, at_byte, at_line, params);
444+
report_difference(from_byte, to_byte, at_byte, at_line, params)?;
445445
return Ok(Cmp::Different);
446446
}
447447
}
@@ -707,9 +707,15 @@ fn is_posix_locale() -> bool {
707707
}
708708

709709
#[inline]
710-
fn report_difference(from_byte: u8, to_byte: u8, at_byte: usize, at_line: usize, params: &Params) {
710+
fn report_difference(
711+
from_byte: u8,
712+
to_byte: u8,
713+
at_byte: usize,
714+
at_line: usize,
715+
params: &Params,
716+
) -> Result<(), String> {
711717
if params.quiet {
712-
return;
718+
return Ok(());
713719
}
714720

715721
let term = if is_posix_locale() && !params.print_bytes {
@@ -734,7 +740,16 @@ fn report_difference(from_byte: u8, to_byte: u8, at_byte: usize, at_line: usize,
734740
format_visible_byte(to_byte)
735741
);
736742
}
737-
println!();
743+
// Instead of println!(), which panics in case of error (> /dev/full).
744+
let mut stdout = io::stdout();
745+
if let Err(e) = writeln!(stdout) {
746+
return Err(format_io_error(&e));
747+
};
748+
if let Err(e) = stdout.flush() {
749+
return Err(format_io_error(&e));
750+
};
751+
752+
Ok(())
738753
}
739754

740755
#[cfg(test)]

src/diff.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// files that was distributed with this source code.
55

66
use crate::params::{parse_params, Format};
7-
use crate::utils::report_failure_to_read_input_file;
7+
use crate::utils::{format_io_error, report_failure_to_read_input_file};
88
use crate::{context_diff, ed_diff, normal_diff, side_diff, unified_diff};
99
use std::env::ArgsOs;
1010
use std::ffi::OsString;
@@ -107,8 +107,8 @@ pub fn main(opts: Peekable<ArgsOs>) -> ExitCode {
107107
uucore::error::set_exit_code(1);
108108
}
109109
}
110-
Err(err) => {
111-
eprintln!("{err}");
110+
Err(error) => {
111+
eprintln!("{}", format_io_error(&error));
112112
return ExitCode::FAILURE;
113113
}
114114
}

src/utils.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// For the full copyright and license information, please view the LICENSE-*
44
// files that was distributed with this source code.
55

6-
use regex::Regex;
76
use std::{ffi::OsString, io::Write};
87
use unicode_width::UnicodeWidthStr;
98

@@ -78,15 +77,19 @@ pub fn format_failure_to_read_input_file(
7877
) -> String {
7978
// std::io::Error's display trait outputs "{detail} (os error {code})"
8079
// but we want only the {detail} (error string) part
81-
let error_code_re = Regex::new(r"\ \(os\ error\ \d+\)$").unwrap();
8280
format!(
8381
"{}: {}: {}",
8482
executable.to_string_lossy(),
8583
filepath.to_string_lossy(),
86-
error_code_re.replace(error.to_string().as_str(), ""),
84+
format_io_error(&error),
8785
)
8886
}
8987

88+
pub fn format_io_error(error: &dyn std::error::Error) -> String {
89+
let s = error.to_string();
90+
s.split(" (os error").next().unwrap_or(&s).to_string()
91+
}
92+
9093
pub fn report_failure_to_read_input_file(
9194
executable: &OsString,
9295
filepath: &OsString,

0 commit comments

Comments
 (0)