Skip to content

Fix signed overflow in sha256_transform - #612

Draft
mjcarroll wants to merge 1 commit into
rollingfrom
mjcarroll/fix-sha256-shift-ub
Draft

mjcarroll wants to merge 1 commit into
rollingfrom
mjcarroll/fix-sha256-shift-ub

Conversation

@mjcarroll

Copy link
Copy Markdown
Member

Description

sha256_transform shifts uint8_t message bytes left by 24 after integer promotion to int, which overflows for any byte ≥ 0x80 (UB; flagged by UBSan in the existing sha256 tests). Cast to uint32_t before shifting.

Is this user-facing behavior change?

No.

Did you use Generative AI?

Claude Code (Claude Opus 5.5) found this under UBSan and wrote the change; I reviewed it.

Additional Information

The message bytes were promoted to int before being shifted, so a byte
>= 0x80 shifted left by 24 overflowed a signed int, which is undefined
behavior (found with UndefinedBehaviorSanitizer). Shift them as
uint32_t.

Signed-off-by: Michael Carroll <mjcarroll.oss@gmail.com>
@mjcarroll
mjcarroll marked this pull request as draft November 10, 2026 02:41
@mjcarroll mjcarroll added the sanitizers Found with or related to ASan/LSan/UBSan/TSan label Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sanitizers Found with or related to ASan/LSan/UBSan/TSan

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant