Skip to content

Commit d5a7b5f

Browse files
committed
Fix memory leaks when sk_X509_new_null() fails
In a lot of places the return value is not checked, and when the function fails the code continues execution. However, this means that operations on the stack fail and will cause memory leaks on the objects that weren't pushed. We also notice an inconsistency in how these failures are handled. For example, in one place we explicitly have a fatal error `php_error_docref(NULL, E_ERROR, "Memory allocation failure");` but this is the only place to do so.
1 parent 19b3003 commit d5a7b5f

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

ext/openssl/openssl.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,9 @@ static STACK_OF(X509) *php_array_to_X509_sk(zval * zcerts, uint32_t arg_num, con
25522552
bool free_cert;
25532553

25542554
sk = sk_X509_new_null();
2555+
if (sk == NULL) {
2556+
goto clean_exit;
2557+
}
25552558

25562559
/* get certs */
25572560
if (Z_TYPE_P(zcerts) == IS_ARRAY) {
@@ -5797,6 +5800,9 @@ PHP_FUNCTION(openssl_pkcs7_encrypt)
57975800
}
57985801

57995802
recipcerts = sk_X509_new_null();
5803+
if (recipcerts == NULL) {
5804+
goto clean_exit;
5805+
}
58005806

58015807
/* get certs */
58025808
if (Z_TYPE_P(zrecipcerts) == IS_ARRAY) {
@@ -6404,6 +6410,9 @@ PHP_FUNCTION(openssl_cms_encrypt)
64046410
}
64056411

64066412
recipcerts = sk_X509_new_null();
6413+
if (recipcerts == NULL) {
6414+
goto clean_exit;
6415+
}
64076416

64086417
/* get certs */
64096418
if (Z_TYPE_P(zrecipcerts) == IS_ARRAY) {

0 commit comments

Comments
 (0)