cksum: rework blake length validation , add tests#11437
cksum: rework blake length validation , add tests#11437RenjiSann wants to merge 6 commits intouutils:mainfrom
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
6f514f8 to
b0547e7
Compare
|
I don't know if you have seen it: clippy complains about two minor issues. |
Blake* variants of SizedAlgoKind now hold a usize instead of an Option<usize>. Previously, the None value represented the default length. Now, the default length is just written as is.
…algo... ...on applicable tests
6f2a9eb to
a6f6cf7
Compare
Yes, I just fixed it ^^ |
|
GNU testsuite comparison: |
| /// Note: when the input is a string, validation may print error messages. | ||
| /// Note: when the algo is Blake2b, values that are above 512 | ||
| /// (Blake2b::DEFAULT_BIT_SIZE) are errors. | ||
| pub fn validate_calculate_blake_length( |
There was a problem hiding this comment.
I'm not a fan of this function name ;-) Maybe something like parse_blake_length would be better?
There was a problem hiding this comment.
I'm ok with that 👍
| // Blake2b's length is parsed in an u64. | ||
| match bit_length.parse::<usize>() { | ||
| Ok(0) => Ok(None), | ||
| match parsed { |
There was a problem hiding this comment.
In the following block you are mixing the handling of parsing errors and the validation logic. I would move the handling of the parsing errors up to where you parse the string and do an early return in the error cases.
There was a problem hiding this comment.
Since GNU treats parsing errors and validation errors very similarly, I can't think of a way to make an early return that's not adding a lot of verbosity.
The reason for the current ordering of the match arms is that when the parsing fails because of an overflow error, the returned error should be the same as when the value exceeds 512 for Blake2b. So I put both arms side by side, and then I put the general case for parsing errors after.
| } | ||
| }; | ||
|
|
||
| // Blake2b's length is parsed in an u64. |
There was a problem hiding this comment.
I guess this comment is outdated?
This PR fixes a tricky edge case that happened when validating a length for the Blake2b algorithm in check mode, which didn't check early for the maximum allowed value, and ended up showing an unexpected error message.
To do so, I centralized all the Blake length validation logic under
validate_calculate_blake_length. This function may be given a string or an int to work on, depending whether we're validating from CLI argument or check-file.Note that it is important to preserve the string format in CLI argument, because for GNU compat, in case of error, we print the given argument, not the number we parsed from it (so that includes leading zeroes).
This MR additionnally adds more tests for BLAKE3 and some edge case tests for BLAKE2b.