Skip to content

fix: plug sign() memory leak and normalize check_key() return#128

Merged
timlegge merged 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-sign-leak-and-check-key
Mar 20, 2026
Merged

fix: plug sign() memory leak and normalize check_key() return#128
timlegge merged 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-sign-leak-and-check-key

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

What

Fix two error-path bugs in RSA.xs: a memory leak in sign() and a return value normalization issue in check_key().

Why

  1. sign() leak: On OpenSSL 3.x, if EVP_PKEY_sign() fails after Newx() allocates the signature buffer, the err: label frees md and ctx but not signature. The buffer leaks on croak().

  2. check_key() truthy -1: EVP_PKEY_private_check() (3.x) and RSA_check_key() (pre-3.x) return -1/-2 on error, which is truthy in Perl. Code like if ($rsa->check_key()) would treat an error as "valid". The internal _new_key_from_parameters() already normalizes with == 1; the public check_key() did not.

How

  • Initialize signature = NULL in PREINIT and add Safefree(signature) to the err: cleanup (Safefree(NULL) is a no-op, safe for all error paths).
  • Wrap both check functions with (result == 1) to return exactly 0 or 1.

Testing

  • All 499 tests pass.
  • Added 2 tests to check_param.t verifying check_key() returns exactly 1.

🤖 Generated with Claude Code

Two error-path fixes in RSA.xs:

1. sign() on OpenSSL 3.x: if the second EVP_PKEY_sign() call failed
   after Newx() allocated the signature buffer, the err: label freed
   md and ctx but not signature.  Initialize signature=NULL and add
   Safefree(signature) to the error cleanup.

2. check_key(): EVP_PKEY_private_check() (3.x) and RSA_check_key()
   (pre-3.x) can return -1 or -2 on error, which is truthy in Perl.
   The docs promise "true if valid, false otherwise", so normalize
   the return to (result == 1), matching what _new_key_from_parameters()
   already does internally.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@timlegge timlegge marked this pull request as ready for review March 20, 2026 02:15
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.

lgtm

@timlegge timlegge merged commit baa0515 into cpan-authors:main Mar 20, 2026
54 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