Skip to content

fix: remove static buffer in get_message_digest() for thread safety#136

Merged
timlegge merged 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-static-digest-buffer
Mar 22, 2026
Merged

fix: remove static buffer in get_message_digest() for thread safety#136
timlegge merged 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-static-digest-buffer

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

@toddr-bot toddr-bot commented Mar 22, 2026

Summary

  • Remove thread-unsafe static buffer from get_message_digest()
  • Caller now provides stack-allocated buffer (unsigned char[EVP_MAX_MD_SIZE])

What

get_message_digest() used a static unsigned char m[EVP_MAX_MD_SIZE] — a single shared buffer across all calls.

Why

Under Perl ithreads, concurrent sign() or verify() calls from different threads would write their digests to the same memory, corrupting results silently. This is a data race with no error message — the worst kind of bug.

How

Changed get_message_digest() to accept a caller-provided buffer instead of using a static one. Both callers (sign() and verify()) now declare unsigned char digest_buf[EVP_MAX_MD_SIZE] on their stack frame (64 bytes — trivial).

Testing

Full test suite passes (499 tests, 16 files). Thread-specific tests would require use threads infrastructure not currently in the test suite.

🤖 Generated with Claude Code


Quality Report

Changes: 1 file changed, 5 insertions(+), 6 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

get_message_digest() used a static unsigned char buffer shared across all
calls, making it unsafe under Perl ithreads — concurrent sign()/verify()
calls would corrupt each other's digest. Replace with caller-provided
stack buffer (EVP_MAX_MD_SIZE = 64 bytes).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@timlegge timlegge marked this pull request as ready for review March 22, 2026 16:09
Copy link
Copy Markdown
Member

@timlegge timlegge left a comment

Choose a reason for hiding this comment

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

Looks fine

@timlegge timlegge merged commit 5d7e2e6 into cpan-authors:main Mar 22, 2026
28 checks passed
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