Skip to content

Commit 12193c5

Browse files
authored
cksum: Don't panic when checked file returned EIO (uutils#10534)
1 parent e6e2b28 commit 12193c5

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/uucore/src/lib/features/checksum/validate.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,26 @@ fn compute_and_check_digest_from_file(
676676

677677
// TODO: improve function signature to use ReadingMode instead of binary bool
678678
// Set binary to false because --binary is not supported with --check
679-
let (calculated_checksum, _) =
680-
digest_reader(&mut digest, &mut file_reader, /* binary */ false).unwrap();
679+
680+
let (calculated_checksum, _) = match digest_reader(&mut digest, &mut file_reader, false) {
681+
Ok(result) => result,
682+
Err(err) => {
683+
show!(err.map_err_context(|| {
684+
locale_aware_escape_name(&real_filename_to_check, QuotingStyle::SHELL_ESCAPE)
685+
.to_string_lossy()
686+
.to_string()
687+
}));
688+
689+
write_file_report(
690+
std::io::stdout(),
691+
filename,
692+
FileChecksumResult::CantOpen,
693+
prefix,
694+
opts.verbose,
695+
);
696+
return Err(LineCheckError::CantOpenFile);
697+
}
698+
};
681699

682700
// Do the checksum validation
683701
let checksum_correct = match calculated_checksum {

tests/by-util/test_cksum.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3046,3 +3046,17 @@ mod debug_flag {
30463046
.stderr_contains("pclmul");
30473047
}
30483048
}
3049+
3050+
#[test]
3051+
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
3052+
fn test_check_file_with_io_error() {
3053+
// /proc/self/mem causes EIO when read without proper seeking
3054+
new_ucmd!()
3055+
.arg("-a")
3056+
.arg("md5")
3057+
.arg("--check")
3058+
.pipe_in("d8e8fca2dc0f896fd7cb4cb0031ba249 /proc/self/mem\n")
3059+
.fails()
3060+
.stderr_contains("Input/output error")
3061+
.stdout_contains("FAILED open or read");
3062+
}

0 commit comments

Comments
 (0)