fix: plug sign() memory leak and normalize check_key() return#128
Merged
timlegge merged 1 commit intoMar 20, 2026
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fix two error-path bugs in RSA.xs: a memory leak in
sign()and a return value normalization issue incheck_key().Why
sign() leak: On OpenSSL 3.x, if
EVP_PKEY_sign()fails afterNewx()allocates the signature buffer, theerr:label freesmdandctxbut notsignature. The buffer leaks oncroak().check_key() truthy -1:
EVP_PKEY_private_check()(3.x) andRSA_check_key()(pre-3.x) return -1/-2 on error, which is truthy in Perl. Code likeif ($rsa->check_key())would treat an error as "valid". The internal_new_key_from_parameters()already normalizes with== 1; the publiccheck_key()did not.How
signature = NULLin PREINIT and addSafefree(signature)to theerr:cleanup (Safefree(NULL) is a no-op, safe for all error paths).(result == 1)to return exactly 0 or 1.Testing
check_param.tverifyingcheck_key()returns exactly 1.🤖 Generated with Claude Code