Skip to content

Commit 99e8630

Browse files
committed
pkey: fix potential memory leak in PKey#sign
Fix potential leak of EVP_MD_CTX object in an error path. This path is normally unreachable, since the size of a signature generated by any supported algorithms would not be larger than LONG_MAX.
1 parent 11801ad commit 99e8630

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

ext/openssl/ossl_pkey.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,10 @@ ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
815815
EVP_MD_CTX_free(ctx);
816816
ossl_raise(ePKeyError, "EVP_DigestSign");
817817
}
818-
if (siglen > LONG_MAX)
818+
if (siglen > LONG_MAX) {
819+
EVP_MD_CTX_free(ctx);
819820
rb_raise(ePKeyError, "signature would be too large");
821+
}
820822
sig = ossl_str_new(NULL, (long)siglen, &state);
821823
if (state) {
822824
EVP_MD_CTX_free(ctx);
@@ -837,8 +839,10 @@ ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
837839
EVP_MD_CTX_free(ctx);
838840
ossl_raise(ePKeyError, "EVP_DigestSignFinal");
839841
}
840-
if (siglen > LONG_MAX)
842+
if (siglen > LONG_MAX) {
843+
EVP_MD_CTX_free(ctx);
841844
rb_raise(ePKeyError, "signature would be too large");
845+
}
842846
sig = ossl_str_new(NULL, (long)siglen, &state);
843847
if (state) {
844848
EVP_MD_CTX_free(ctx);

0 commit comments

Comments
 (0)