diff --git a/src/uucore/src/lib/features/checksum/mod.rs b/src/uucore/src/lib/features/checksum/mod.rs index 40c5bb421f..36df7c5bc8 100644 --- a/src/uucore/src/lib/features/checksum/mod.rs +++ b/src/uucore/src/lib/features/checksum/mod.rs @@ -260,12 +260,6 @@ pub struct HashLength { } impl HashLength { - #[must_use] - #[inline] - pub(crate) fn from_bytes(n: usize) -> Self { - Self { bit_len: n * 8 } - } - #[must_use] #[inline] pub fn from_bits(n: usize) -> Self { diff --git a/src/uucore/src/lib/features/checksum/validate.rs b/src/uucore/src/lib/features/checksum/validate.rs index 50465fbb41..f53c756d75 100644 --- a/src/uucore/src/lib/features/checksum/validate.rs +++ b/src/uucore/src/lib/features/checksum/validate.rs @@ -791,7 +791,12 @@ fn process_non_algo_based_line( // bits except when dealing with blake2b, sha2 and sha3, where we will // detect the length. let algo_len = match cli_algo_kind { - ak::Blake2b | ak::Blake3 => Some(HashLength::from_bytes(expected_checksum.len())), + // An over-length digest makes this a malformed line for GNU, not a + // fatal error. + algo @ (ak::Blake2b | ak::Blake3) => Some( + parse_blake_length(algo, BlakeLength::Int(expected_checksum.len() * 8)) + .map_err(|_| LineCheckError::ImproperlyFormatted)?, + ), ak::Sha2 | ak::Sha3 => { // multiplication by 8 to get the number of bits Some( diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 241777f289..ec77c77e82 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -1676,6 +1676,20 @@ fn test_md5_bits() { .stderr_contains("f: no properly formatted checksum lines found"); } +#[test] +fn test_blake2b_check_digest_too_long() { + let (at, mut ucmd) = at_and_ucmd!(); + // The referenced file must exist: the digest length is only used once the + // line is accepted and the file is about to be hashed. + at.write("f1", "content\n"); + // 65 bytes, above the 64 bytes BLAKE2b maximum. + at.write("sums", &format!("{} f1\n", "a".repeat(130))); + + ucmd.args(&["-a", "blake2b", "-c", "sums"]) + .fails_with_code(1) + .stderr_contains("sums: no properly formatted checksum lines found"); +} + #[test] fn test_blake2b_bits() { let (at, mut ucmd) = at_and_ucmd!();