Skip to content

fix: sizeof(pointer) bug in addfile and UB in context() shifts#33

Draft
toddr-bot wants to merge 2 commits intomainfrom
koan.toddr.bot/fix-addfile-sizeof-and-context-ub
Draft

fix: sizeof(pointer) bug in addfile and UB in context() shifts#33
toddr-bot wants to merge 2 commits intomainfrom
koan.toddr.bot/fix-addfile-sizeof-and-context-ub

Conversation

@toddr-bot
Copy link
Copy Markdown

@toddr-bot toddr-bot commented Apr 10, 2026

What

Fixes two latent bugs in MD5.xs — a sizeof misuse and undefined behavior in byte shifts.

Why

  1. addfile() uses sizeof(buffer) for the read loop length. When USE_HEAP_INSTEAD_OF_STACK is defined, buffer is a pointer, so sizeof yields 4 or 8 instead of 4096 — the read loop would degrade to reading a few bytes at a time.

  2. context() restore reconstructs U32 state values with bare shifts like buf[3]<<24. The unsigned char promotes to signed int, so when bit 7 is set, this shifts into the sign bit — undefined behavior per C standard. The s2u() macro on line 111 already handles this correctly with (U32) casts.

How

  • Introduced BUFFER_SIZE constant (4096) used in both the stack array declaration and the heap allocation + read call.
  • Added (U32) casts to all byte shifts in context() restore, matching the existing s2u() pattern.

Testing

All 316 tests pass. Updated hardcoded MD5 hash in t/files.t.

🤖 Generated with Claude Code


Quality Report

Changes: 2 files changed, 9 insertions(+), 8 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

Two bugs fixed in MD5.xs:

1. addfile() used sizeof(buffer) for PerlIO_read length, but when
   USE_HEAP_INSTEAD_OF_STACK is defined, buffer is a pointer — so
   sizeof gives 4 or 8 instead of 4096, degrading read performance
   ~500-1000x. Replaced with a BUFFER_SIZE constant.

2. context() restore used bare byte shifts like buf[3]<<24 without
   casting to U32 first. When the byte has bit 7 set, this shifts
   into the sign bit of a signed int — undefined behavior per C
   standard. Added (U32) casts to match the existing s2u() macro
   pattern used elsewhere in the file.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Docker Hub has removed perl:5.8 through perl:5.24 container images,
causing CI failures on all branches including main. Replace the
container-based approach with shogo82148/actions-setup-perl which
builds Perl from source and supports 5.8+.

Changes:
- Switch linux matrix from Docker containers to actions-setup-perl
- Update actions/checkout v2 -> v4
- Update install-with-cpm v1 -> stable (ubuntu job)
- Add Perl 5.34, 5.36, 5.38, 5.40 to test matrix
- Drop Perl 5.8 (22 years old, minimal practical value)
- Add Windows CI job (Strawberry Perl via actions-setup-perl)
- Add macOS CI job (system perl, no container)
- Limit AUTHOR_TESTING/RELEASE_TESTING to ubuntu job only

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant