Skip to content

Commit a00d347

Browse files
authored
Merge pull request #9032 from RenjiSann/cksum-fix-len-zero
Fix "cksum: --length 0 shouldn't fail for algorithms that don't support --length"
2 parents 28bc57a + 4e9d07e commit a00d347

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/uu/cksum/src/cksum.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
383383
let input_length = matches.get_one::<usize>(options::LENGTH);
384384

385385
let length = match input_length {
386-
Some(length) => {
387-
if algo_name == ALGORITHM_OPTIONS_BLAKE2B {
388-
calculate_blake2b_length(*length)?
389-
} else {
390-
return Err(ChecksumError::LengthOnlyForBlake2b.into());
391-
}
386+
None | Some(0) => None,
387+
Some(length) if algo_name == ALGORITHM_OPTIONS_BLAKE2B => {
388+
calculate_blake2b_length(*length)?
389+
}
390+
_ => {
391+
return Err(ChecksumError::LengthOnlyForBlake2b.into());
392392
}
393-
None => None,
394393
};
395394

396395
if LEGACY_ALGORITHMS.contains(&algo_name) && check {

tests/by-util/test_cksum.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,20 @@ fn test_length_with_wrong_algorithm() {
348348
.stderr_contains("cksum: --length is only supported with --algorithm=blake2b");
349349
}
350350

351+
/// Giving --length to a wrong algorithm doesn't fail if the length is zero
352+
#[test]
353+
fn test_length_is_zero_with_wrong_algorithm() {
354+
for algo in ["md5", "crc", "sha1", "sha224", "sha256", "sha384", "sha512"] {
355+
new_ucmd!()
356+
.arg("--length=0")
357+
.args(&["-a", algo])
358+
.arg("lorem_ipsum.txt")
359+
.succeeds()
360+
.no_stderr()
361+
.stdout_is_fixture(format!("{algo}_single_file.expected"));
362+
}
363+
}
364+
351365
#[test]
352366
fn test_length_not_supported() {
353367
new_ucmd!()

0 commit comments

Comments
 (0)