Skip to content

cksum: rework blake length validation , add tests#11437

Open
RenjiSann wants to merge 6 commits intouutils:mainfrom
RenjiSann:cksum-improve-blake-validation
Open

cksum: rework blake length validation , add tests#11437
RenjiSann wants to merge 6 commits intouutils:mainfrom
RenjiSann:cksum-improve-blake-validation

Conversation

@RenjiSann
Copy link
Collaborator

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.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 21, 2026

Merging this PR will not alter performance

✅ 300 untouched benchmarks
⏩ 46 skipped benchmarks1


Comparing RenjiSann:cksum-improve-blake-validation (a6f6cf7) with main (c6a57bb)

Open in CodSpeed

Footnotes

  1. 46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@RenjiSann RenjiSann force-pushed the cksum-improve-blake-validation branch 5 times, most recently from 6f514f8 to b0547e7 Compare March 21, 2026 12:39
@RenjiSann RenjiSann marked this pull request as ready for review March 21, 2026 12:43
@cakebaker
Copy link
Contributor

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.
@RenjiSann RenjiSann force-pushed the cksum-improve-blake-validation branch from 6f2a9eb to a6f6cf7 Compare March 23, 2026 10:20
@RenjiSann
Copy link
Collaborator Author

I don't know if you have seen it: clippy complains about two minor issues.

Yes, I just fixed it ^^

@github-actions
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/tail/pipe-f2 is no longer failing!
Congrats! The gnu test tests/tail/tail-n0f is now passing!

/// 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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this function name ;-) Maybe something like parse_blake_length would be better?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with that 👍

// Blake2b's length is parsed in an u64.
match bit_length.parse::<usize>() {
Ok(0) => Ok(None),
match parsed {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this comment is outdated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants