Skip to content

fix: Block checksum 32-bit overflow in _calc_chksum#244

Open
metaneutrons wants to merge 1 commit into
cnvogelg:mainfrom
metaneutrons:fix/checksum-overflow
Open

fix: Block checksum 32-bit overflow in _calc_chksum#244
metaneutrons wants to merge 1 commit into
cnvogelg:mainfrom
metaneutrons:fix/checksum-overflow

Conversation

@metaneutrons
Copy link
Copy Markdown

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) & 0xFFFFFFFF then 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 0xFFFFFFFF before negation — for example, PFS3-formatted hard disk images.

Fix

Mask the accumulator with & 0xFFFFFFFF on each addition step, matching the 32-bit wrapping arithmetic used by AmigaOS.

Before

chksum += self._get_long(i)

After

chksum = (chksum + self._get_long(i)) & 0xFFFFFFFF

Testing

Verified against a PFS3 RDB disk image where RDisk.open() previously returned valid=False due to checksum mismatch. After the fix, the RDB, PART, FSHD, and LSEG blocks all validate correctly.

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.
Copilot AI review requested due to automatic review settings April 12, 2026 16:17
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

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