1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

cksum: properly handle cases where --lenghth is 0 or >512

This commit is contained in:
Vardhan Patil 2023-12-30 20:28:03 +05:30
parent e1863ac64e
commit e7cb4e9ff0

View file

@ -244,7 +244,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let input_length = matches.get_one::<usize>(options::LENGTH); let input_length = matches.get_one::<usize>(options::LENGTH);
let length = if let Some(length) = input_length { let length = if let Some(length) = input_length {
if length % 8 != 0 { match length.to_owned() {
n if n == 0 => None,
n if n % 8 != 0 => {
// GNU's implementation seem to use these quotation marks // GNU's implementation seem to use these quotation marks
// in their error messages, so we do the same. // in their error messages, so we do the same.
uucore::show_error!("invalid length: \u{2018}{length}\u{2019}"); uucore::show_error!("invalid length: \u{2018}{length}\u{2019}");
@ -254,7 +256,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
) )
.into()); .into());
} }
n if n > 512 => {
uucore::show_error!("invalid length: \u{2018}{length}\u{2019}");
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"maximum digest length for \u{2018}BLAKE2b\u{2019} is 512 bits",
)
.into());
}
n => {
if algo_name != ALGORITHM_OPTIONS_BLAKE2B { if algo_name != ALGORITHM_OPTIONS_BLAKE2B {
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::InvalidInput, io::ErrorKind::InvalidInput,
@ -265,7 +276,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// Divide by 8, as our blake2b implementation expects bytes // Divide by 8, as our blake2b implementation expects bytes
// instead of bits. // instead of bits.
Some(length / 8) Some(n / 8)
}
}
} else { } else {
None None
}; };