Skip to content

Commit d70cf5a

Browse files
committed
Fix various memory leaks on error conditions in openssl_x509_parse()
1 parent 90aac52 commit d70cf5a

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

ext/openssl/openssl.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,15 +2091,15 @@ PHP_FUNCTION(openssl_x509_parse)
20912091
/* Can return NULL on error or memory allocation failure */
20922092
if (!bn_serial) {
20932093
php_openssl_store_errors();
2094-
RETURN_FALSE;
2094+
goto err;
20952095
}
20962096

20972097
hex_serial = BN_bn2hex(bn_serial);
20982098
BN_free(bn_serial);
20992099
/* Can return NULL on error or memory allocation failure */
21002100
if (!hex_serial) {
21012101
php_openssl_store_errors();
2102-
RETURN_FALSE;
2102+
goto err;
21032103
}
21042104

21052105
str_serial = i2s_ASN1_INTEGER(NULL, asn1_serial);
@@ -2171,19 +2171,15 @@ PHP_FUNCTION(openssl_x509_parse)
21712171
bio_out = BIO_new(BIO_s_mem());
21722172
if (bio_out == NULL) {
21732173
php_openssl_store_errors();
2174-
RETURN_FALSE;
2174+
goto err_subitem;
21752175
}
21762176
if (nid == NID_subject_alt_name) {
21772177
if (openssl_x509v3_subjectAltName(bio_out, extension) == 0) {
21782178
BIO_get_mem_ptr(bio_out, &bio_buf);
21792179
add_assoc_stringl(&subitem, extname, bio_buf->data, bio_buf->length);
21802180
} else {
2181-
zend_array_destroy(Z_ARR_P(return_value));
21822181
BIO_free(bio_out);
2183-
if (cert_str) {
2184-
X509_free(cert);
2185-
}
2186-
RETURN_FALSE;
2182+
goto err_subitem;
21872183
}
21882184
}
21892185
else if (X509V3_EXT_print(bio_out, extension, 0, 0)) {
@@ -2198,6 +2194,16 @@ PHP_FUNCTION(openssl_x509_parse)
21982194
if (cert_str) {
21992195
X509_free(cert);
22002196
}
2197+
return;
2198+
2199+
err_subitem:
2200+
zval_ptr_dtor(&subitem);
2201+
err:
2202+
zend_array_destroy(Z_ARR_P(return_value));
2203+
if (cert_str) {
2204+
X509_free(cert);
2205+
}
2206+
RETURN_FALSE;
22012207
}
22022208
/* }}} */
22032209

0 commit comments

Comments
 (0)