fix: Block checksum 32-bit overflow in _calc_chksum#244
Open
metaneutrons wants to merge 1 commit into
Open
Conversation
The checksum accumulator was not masked to 32 bits during addition, only at the end. With Python's arbitrary-precision integers, this caused the sum to exceed 32 bits, producing wrong checksums for blocks with large unsigned values (common in RDB headers). Fix: mask with & 0xFFFFFFFF on each addition step. This fixes RDB validation failing on valid Amiga disk images.
There was a problem hiding this comment.
Pull request overview
Fixes block checksum calculation to use 32-bit wrapping arithmetic during accumulation, aligning Python behavior with the AmigaOS/RDB checksum specification so valid disk images don’t fail validation.
Changes:
- Mask the running checksum accumulator to 32 bits on each addition in
Block._calc_chksum(). - Ensure the computed two’s complement checksum matches expected 32-bit overflow behavior for large-sum blocks.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Block._calc_chksum()accumulates unsigned 32-bit values read via_get_long()without masking the running sum to 32 bits. Since Python integers have arbitrary precision, the accumulator grows beyond 32 bits. The final(-chksum) & 0xFFFFFFFFthen produces the wrong two's complement, causing valid blocks to fail checksum validation.This is observable with RDB disk images where the sum of all longs in a block exceeds
0xFFFFFFFFbefore negation — for example, PFS3-formatted hard disk images.Fix
Mask the accumulator with
& 0xFFFFFFFFon each addition step, matching the 32-bit wrapping arithmetic used by AmigaOS.Before
After
Testing
Verified against a PFS3 RDB disk image where
RDisk.open()previously returnedvalid=Falsedue to checksum mismatch. After the fix, the RDB, PART, FSHD, and LSEG blocks all validate correctly.