Skip to content

Commit

Permalink
Remove some unnnecessary null checks in openssl
Browse files Browse the repository at this point in the history
Remove null checks before EVP_PKEY_free and BIO_free. NULL is a
no-op for both of these. Probably applies to most other freeing
function as well...
  • Loading branch information
nikic committed Aug 10, 2020
1 parent 80d3ce3 commit 90a2c79
Showing 1 changed file with 22 additions and 69 deletions.
91 changes: 22 additions & 69 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1612,9 +1612,7 @@ PHP_FUNCTION(openssl_spki_new)
goto cleanup;

cleanup:
if (pkey != NULL) {
EVP_PKEY_free(pkey);
}
EVP_PKEY_free(pkey);
if (spki != NULL) {
NETSCAPE_SPKI_free(spki);
}
Expand Down Expand Up @@ -1669,9 +1667,7 @@ PHP_FUNCTION(openssl_spki_verify)
if (spki != NULL) {
NETSCAPE_SPKI_free(spki);
}
if (pkey != NULL) {
EVP_PKEY_free(pkey);
}
EVP_PKEY_free(pkey);
if (spkstr_cleaned != NULL) {
efree(spkstr_cleaned);
}
Expand Down Expand Up @@ -1738,12 +1734,8 @@ PHP_FUNCTION(openssl_spki_export)
if (spki != NULL) {
NETSCAPE_SPKI_free(spki);
}
if (out != NULL) {
BIO_free_all(out);
}
if (pkey != NULL) {
EVP_PKEY_free(pkey);
}
BIO_free_all(out);
EVP_PKEY_free(pkey);
if (spkstr_cleaned != NULL) {
efree(spkstr_cleaned);
}
Expand Down Expand Up @@ -2627,10 +2619,7 @@ PHP_FUNCTION(openssl_pkcs12_export_to_file)
php_sk_X509_free(ca);

cleanup:

if (priv_key) {
EVP_PKEY_free(priv_key);
}
EVP_PKEY_free(priv_key);

if (cert_str) {
X509_free(cert);
Expand Down Expand Up @@ -2719,10 +2708,7 @@ PHP_FUNCTION(openssl_pkcs12_export)
php_sk_X509_free(ca);

cleanup:

if (priv_key) {
EVP_PKEY_free(priv_key);
}
EVP_PKEY_free(priv_key);
if (cert_str) {
X509_free(cert);
}
Expand Down Expand Up @@ -2822,13 +2808,9 @@ PHP_FUNCTION(openssl_pkcs12_read)
php_openssl_store_errors();
}

cleanup:
if (bio_in) {
BIO_free(bio_in);
}
if (pkey) {
EVP_PKEY_free(pkey);
}
cleanup:
BIO_free(bio_in);
EVP_PKEY_free(pkey);
if (cert) {
X509_free(cert);
}
Expand Down Expand Up @@ -3300,13 +3282,8 @@ PHP_FUNCTION(openssl_csr_sign)
}

PHP_SSL_REQ_DISPOSE(&req);

if (priv_key) {
EVP_PKEY_free(priv_key);
}
if (key) {
EVP_PKEY_free(key);
}
EVP_PKEY_free(priv_key);
EVP_PKEY_free(key);
if (csr_str) {
X509_REQ_free(csr);
}
Expand Down Expand Up @@ -4265,9 +4242,7 @@ PHP_FUNCTION(openssl_pkey_new)
if (eckey != NULL) {
EC_KEY_free(eckey);
}
if (pkey != NULL) {
EVP_PKEY_free(pkey);
}
EVP_PKEY_free(pkey);
RETURN_FALSE;
#endif
}
Expand Down Expand Up @@ -4367,13 +4342,8 @@ PHP_FUNCTION(openssl_pkey_export_to_file)

clean_exit:
PHP_SSL_REQ_DISPOSE(&req);

if (key) {
EVP_PKEY_free(key);
}
if (bio_out) {
BIO_free(bio_out);
}
EVP_PKEY_free(key);
BIO_free(bio_out);
}
/* }}} */

Expand Down Expand Up @@ -4448,13 +4418,8 @@ PHP_FUNCTION(openssl_pkey_export)
}
}
PHP_SSL_REQ_DISPOSE(&req);

if (key) {
EVP_PKEY_free(key);
}
if (bio_out) {
BIO_free(bio_out);
}
EVP_PKEY_free(key);
BIO_free(bio_out);
}
/* }}} */

Expand Down Expand Up @@ -4789,12 +4754,8 @@ PHP_FUNCTION(openssl_pkey_derive)
}

cleanup:
if (pkey) {
EVP_PKEY_free(pkey);
}
if (peer_key) {
EVP_PKEY_free(peer_key);
}
EVP_PKEY_free(pkey);
EVP_PKEY_free(peer_key);
if (ctx) {
EVP_PKEY_CTX_free(ctx);
}
Expand Down Expand Up @@ -5231,9 +5192,7 @@ PHP_FUNCTION(openssl_pkcs7_read)
RETVAL_TRUE;

clean_exit:
if (bio_in != NULL) {
BIO_free(bio_in);
}
BIO_free(bio_in);

if (p7 != NULL) {
PKCS7_free(p7);
Expand Down Expand Up @@ -5359,9 +5318,7 @@ PHP_FUNCTION(openssl_pkcs7_sign)
if (others) {
sk_X509_pop_free(others, X509_free);
}
if (privkey) {
EVP_PKEY_free(privkey);
}
EVP_PKEY_free(privkey);
if (cert && cert_str) {
X509_free(cert);
}
Expand Down Expand Up @@ -5441,9 +5398,7 @@ PHP_FUNCTION(openssl_pkcs7_decrypt)
if (cert && free_recipcert) {
X509_free(cert);
}
if (key) {
EVP_PKEY_free(key);
}
EVP_PKEY_free(key);
}
/* }}} */

Expand Down Expand Up @@ -6191,9 +6146,7 @@ PHP_FUNCTION(openssl_cms_decrypt)
if (cert && free_recipcert) {
X509_free(cert);
}
if (key) {
EVP_PKEY_free(key);
}
EVP_PKEY_free(key);
}
/* }}} */

Expand Down

0 comments on commit 90a2c79

Please sign in to comment.