Skip to content

cksum: preserve the full input length on 32-bit targets - #16995

Open
limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:fix/cksum-32bit-length-16994
Open

limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:fix/cksum-32bit-length-16994

Conversation

@limadog9

Copy link
Copy Markdown
Contributor

Keep the full input length when computing the POSIX CRC and printing the byte count. Add regression tests for the 4 GiB boundary.

Verified the 32-bit build against GNU cksum using the files from the issue.

Closes #16994

Comment thread src/uucore/src/lib/features/sum.rs Outdated
fn hash_update(&mut self, input: &[u8]) {
self.digest.update(input);
self.size += input.len();
self.size += input.len() as u64;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self.size += input.len() as u64;
self.size += input.len().into();

Please use .into() instead of an as cast where possible, as it provides safer type conversions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Actually, .into() doesn't work here. You'd need to use u64::try_from(input.len()).unwrap(), which is a bit cumbersome given that the conversion is infallible on all supported platforms.

@limadog9
limadog9 force-pushed the fix/cksum-32bit-length-16994 branch from 5ae2602 to 1a4d5f0 Compare November 29, 2026 23:45
Comment thread src/uucore/src/lib/features/sum.rs Outdated
fn hash_update(&mut self, input: &[u8]) {
self.digest.update(input);
self.size += input.len();
self.size += u64::try_from(input.len()).expect("slice length fits in u64");

@xtqqczze xtqqczze Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self.size += u64::try_from(input.len()).expect("slice length fits in u64");
self.size += u64::try_from(input.len()).unwrap_or(u64::MAX);

I guess this is slightly less cumbersome than using expect.

@limadog9
limadog9 force-pushed the fix/cksum-32bit-length-16994 branch from 1a4d5f0 to 99df3bc Compare November 30, 2026 00:19

// Print the sum
let block_size =
u64::try_from(options.algo_kind.bitlen()).expect("legacy checksum sizes fit in u64");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
u64::try_from(options.algo_kind.bitlen()).expect("legacy checksum sizes fit in u64");
u64::try_from(options.algo_kind.bitlen()).unwrap_or(u64::MAX);

Use u64 for the length folded into the POSIX CRC and keep the byte
count returned by io::copy when formatting legacy checksums. A 32-bit
build previously reported the same CRC and length for 17 zero bytes
and for 4 GiB plus 17 zero bytes.

Use checked conversions for slice lengths and legacy block sizes.
Rust provides TryFrom<usize> for u64.

Exercise the CRC crossing the 4 GiB boundary and the printed size
without hashing a multi-gigabyte fixture in the test suite.

Closes uutils#16994
@limadog9
limadog9 force-pushed the fix/cksum-32bit-length-16994 branch from 99df3bc to d9ac1a9 Compare November 30, 2026 02:42

This branch has not been deployed

No deployments
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.

cksum: 32-bit builds report the wrong checksum and byte count at 4 GiB

2 participants