free NULL cleanup
authorRich Salz <rsalz@akamai.com>
Tue, 24 Mar 2015 14:17:37 +0000 (10:17 -0400)
committerRich Salz <rsalz@akamai.com>
Wed, 25 Mar 2015 03:17:16 +0000 (23:17 -0400)
Start ensuring all OpenSSL "free" routines allow NULL, and remove
any if check before calling them.
This gets DH_free, DSA_free, RSA_free

Reviewed-by: Matt Caswell <matt@openssl.org>
34 files changed:
apps/dh.c
apps/dhparam.c
apps/dsa.c
apps/dsaparam.c
apps/gendh.c
apps/gendsa.c
apps/genrsa.c
apps/rsa.c
apps/s_server.c
apps/speed.c
crypto/dh/dh_ameth.c
crypto/dh/dh_asn1.c
crypto/dh/dh_lib.c
crypto/dh/dhtest.c
crypto/dsa/dsa_ameth.c
crypto/dsa/dsa_depr.c
crypto/dsa/dsa_lib.c
crypto/dsa/dsatest.c
crypto/evp/evp_extra_test.c
crypto/pem/pvkfmt.c
crypto/rsa/rsa_depr.c
demos/easy_tls/easy-tls.c
doc/crypto/DH_new.pod
doc/crypto/DSA_new.pod
doc/crypto/RSA_new.pod
engines/ccgost/gost_ameth.c
engines/e_capi.c
engines/e_chil.c
engines/e_sureware.c
ssl/s3_clnt.c
ssl/s3_lib.c
ssl/ssl_cert.c
ssl/ssl_conf.c
ssl/ssltest.c

index cdb5f4a1b08ad589071f30eadc457a34102fe85b..a9216336e59561f17cf3a821ecbca58bdfa6886b 100644 (file)
--- a/apps/dh.c
+++ b/apps/dh.c
@@ -314,8 +314,7 @@ int MAIN(int argc, char **argv)
         BIO_free(in);
     if (out != NULL)
         BIO_free_all(out);
-    if (dh != NULL)
-        DH_free(dh);
+    DH_free(dh);
     apps_shutdown();
     OPENSSL_EXIT(ret);
 }
index 0e6a3c3198c355a0d6e37707efcb97cdd5138440..12a2be64550be3d609eea7ebeca0610c1799ed25 100644 (file)
@@ -319,8 +319,7 @@ int MAIN(int argc, char **argv)
             if (!dsa
                 || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
                                                cb)) {
-                if (dsa)
-                    DSA_free(dsa);
+                DSA_free(dsa);
                 BN_GENCB_free(cb);
                 ERR_print_errors(bio_err);
                 goto end;
@@ -520,8 +519,7 @@ int MAIN(int argc, char **argv)
         BIO_free(in);
     if (out != NULL)
         BIO_free_all(out);
-    if (dh != NULL)
-        DH_free(dh);
+    DH_free(dh);
     apps_shutdown();
     OPENSSL_EXIT(ret);
 }
index 7ff6ee960fffc26a77df420fcc46b4aa50870e6f..8d085bcef2f691290cbc748fbd7a02f7d8f5eaa2 100644 (file)
@@ -360,8 +360,7 @@ int MAIN(int argc, char **argv)
         BIO_free(in);
     if (out != NULL)
         BIO_free_all(out);
-    if (dsa != NULL)
-        DSA_free(dsa);
+    DSA_free(dsa);
     if (passin)
         OPENSSL_free(passin);
     if (passout)
index 74eefdc871eb6e7517437077c1635985cb6eea2d..fc1c81760410fcd40acec3bd2c233441262c5314 100644 (file)
@@ -438,8 +438,7 @@ int MAIN(int argc, char **argv)
         BIO_free(in);
     if (out != NULL)
         BIO_free_all(out);
-    if (dsa != NULL)
-        DSA_free(dsa);
+    DSA_free(dsa);
     apps_shutdown();
     OPENSSL_EXIT(ret);
 }
index bbeba067148cbd3eb3902da3654533735645988f..6102b79511ad51b802dfcf540cd8db7ad603c262 100644 (file)
@@ -212,8 +212,7 @@ int MAIN(int argc, char **argv)
         ERR_print_errors(bio_err);
     if (out != NULL)
         BIO_free_all(out);
-    if (dh != NULL)
-        DH_free(dh);
+    DH_free(dh);
     if (cb != NULL)
         BN_GENCB_free(cb);
     apps_shutdown();
index fd1360acd57f559a01a974d4ec760d43472af64a..d330a0100280fcdf2908dd863aca2a2d6f36195c 100644 (file)
@@ -271,8 +271,7 @@ int MAIN(int argc, char **argv)
         BIO_free(in);
     if (out != NULL)
         BIO_free_all(out);
-    if (dsa != NULL)
-        DSA_free(dsa);
+    DSA_free(dsa);
     if (passout)
         OPENSSL_free(passout);
     apps_shutdown();
index 5770c8d1ec4c5f088d7ea886f9d9f165efdb5c80..1b06c82e40a0008a630887d02238e0ba0e18170d 100644 (file)
@@ -311,8 +311,7 @@ int MAIN(int argc, char **argv)
         BN_free(bn);
     if (cb)
         BN_GENCB_free(cb);
-    if (rsa)
-        RSA_free(rsa);
+    RSA_free(rsa);
     if (out)
         BIO_free_all(out);
     if (passout)
index 419e50455adb6c3d8217aee87a817d581e9b3b57..ac4a3c43efe9ce0760c14bb03a943c9d7349cf1c 100644 (file)
@@ -424,8 +424,7 @@ int MAIN(int argc, char **argv)
  end:
     if (out != NULL)
         BIO_free_all(out);
-    if (rsa != NULL)
-        RSA_free(rsa);
+    RSA_free(rsa);
     if (passin)
         OPENSSL_free(passin);
     if (passout)
index 298e665d97a037e94c2e5ee0cd92d3cf24bbbf38..97aa23da195d4a6746c747f2335499d965d52afe 100644 (file)
@@ -3180,8 +3180,7 @@ static RSA *tmp_rsa_cb(SSL *s, int is_export, int keylength)
         }
         if (!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
             !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) {
-            if (rsa_tmp)
-                RSA_free(rsa_tmp);
+            RSA_free(rsa_tmp);
             rsa_tmp = NULL;
         }
         if (!s_quiet) {
index 44c276ab2129ee0a37ad76028686c23e8cdbf5a6..b023f2877f71739db6bf3019489ce744a69e2358 100644 (file)
@@ -2448,13 +2448,11 @@ int MAIN(int argc, char **argv)
         OPENSSL_free(buf2_malloc);
 #ifndef OPENSSL_NO_RSA
     for (i = 0; i < RSA_NUM; i++)
-        if (rsa_key[i] != NULL)
-            RSA_free(rsa_key[i]);
+        RSA_free(rsa_key[i]);
 #endif
 #ifndef OPENSSL_NO_DSA
     for (i = 0; i < DSA_NUM; i++)
-        if (dsa_key[i] != NULL)
-            DSA_free(dsa_key[i]);
+        DSA_free(dsa_key[i]);
 #endif
 
 #ifndef OPENSSL_NO_EC
index 17027c544f965c91c4515142bd8d0df1fbc4ed05..c71538fa15fdf286c07b3ecc40b4461fd34b0ab9 100644 (file)
@@ -142,8 +142,7 @@ static int dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  err:
     if (public_key)
         ASN1_INTEGER_free(public_key);
-    if (dh)
-        DH_free(dh);
+    DH_free(dh);
     return 0;
 
 }
@@ -711,8 +710,7 @@ static int dh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
         ASN1_INTEGER_free(public_key);
     if (pkpeer)
         EVP_PKEY_free(pkpeer);
-    if (dhpeer)
-        DH_free(dhpeer);
+    DH_free(dhpeer);
     return rv;
 }
 
index f470214399b642e3ac7edc2d402050c6ba3fea30..7066cafce64d772e547973452d40300b1d54db12 100644 (file)
@@ -142,8 +142,7 @@ DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
     }
 
     if (a) {
-        if (*a)
-            DH_free(*a);
+        DH_free(*a);
         *a = dh;
     }
 
index 46d1a2b7b9a8d8b243264952ab0fe7f7b853aae2..4f07a277ea73c474d0cab6aa7500c62875dc3b28 100644 (file)
@@ -170,6 +170,7 @@ DH *DH_new_method(ENGINE *engine)
 void DH_free(DH *r)
 {
     int i;
+
     if (r == NULL)
         return;
     i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
index 9bb9a0021280e2bd8388bd14f523e7493b18707b..6c063daf3d66fd67405f89b316a248d87f7ea1d1 100644 (file)
@@ -199,10 +199,8 @@ int main(int argc, char *argv[])
         OPENSSL_free(abuf);
     if (bbuf != NULL)
         OPENSSL_free(bbuf);
-    if (b != NULL)
-        DH_free(b);
-    if (a != NULL)
-        DH_free(a);
+    DH_free(b);
+    DH_free(a);
     if (_cb)
         BN_GENCB_free(_cb);
     BIO_free(out);
index 61a9d0fc5e00138ecb6f9e8a9bae0e48f5c163a1..96d5c5ae79be399b99a8d80cff5632ee08138402 100644 (file)
@@ -120,8 +120,7 @@ static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  err:
     if (public_key)
         ASN1_INTEGER_free(public_key);
-    if (dsa)
-        DSA_free(dsa);
+    DSA_free(dsa);
     return 0;
 
 }
index be1df138cb09a0821f5ebfa0666f498237489ded..10f0314f1824f055606377d5dd24091def8344cc 100644 (file)
@@ -89,10 +89,8 @@ DSA *DSA_generate_parameters(int bits,
     if ((ret = DSA_new()) == NULL)
         return NULL;
     cb = BN_GENCB_new();
-    if (!cb) {
-        DSA_free(ret);
-        return NULL;
-    }
+    if (!cb)
+        goto err;
 
     BN_GENCB_set_old(cb, callback, cb_arg);
 
@@ -102,6 +100,7 @@ DSA *DSA_generate_parameters(int bits,
         return ret;
     }
     BN_GENCB_free(cb);
+err:
     DSA_free(ret);
     return NULL;
 }
index eb13fbbbe713047e20372bdb7b583f341a15359a..bfd91062f5645a9da57c2efdb88e87c4165e96ea 100644 (file)
@@ -315,8 +315,7 @@ DH *DSA_dup_DH(const DSA *r)
     return ret;
 
  err:
-    if (ret != NULL)
-        DH_free(ret);
+    DH_free(ret);
     return NULL;
 }
 #endif
index 9b1308926f92d5ad819548b7e0ef6a414cc8ba50..bf47816037f2bda22e35324be3fb9b07556f0cfc 100644 (file)
@@ -211,8 +211,7 @@ int main(int argc, char **argv)
  end:
     if (!ret)
         ERR_print_errors(bio_err);
-    if (dsa != NULL)
-        DSA_free(dsa);
+    DSA_free(dsa);
     if (cb != NULL)
         BN_GENCB_free(cb);
     CRYPTO_cleanup_all_ex_data();
index 0f7b011ce85e0627fa6e209203e26b7efcee4c6c..c474134a2dbea78e97afb46c83c6c513064b842e 100644 (file)
@@ -268,9 +268,7 @@ static EVP_PKEY *load_example_rsa_key(void)
     if (pkey) {
         EVP_PKEY_free(pkey);
     }
-    if (rsa) {
-        RSA_free(rsa);
-    }
+    RSA_free(rsa);
 
     return ret;
 }
index ee4b6a8241cc2c37e6ed7b5cf4a674937eff6817..0f2390d3b422ad585db08e75ce5cb9a2c1b2ab38 100644 (file)
@@ -335,8 +335,7 @@ static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
 
  memerr:
     PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
-    if (dsa)
-        DSA_free(dsa);
+    DSA_free(dsa);
     if (ret)
         EVP_PKEY_free(ret);
     if (ctx)
@@ -385,8 +384,7 @@ static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
     return ret;
  memerr:
     PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
-    if (rsa)
-        RSA_free(rsa);
+    RSA_free(rsa);
     if (ret)
         EVP_PKEY_free(ret);
     return NULL;
index a6ec38548b6512c8ae2e17c27ca703dfaf6f31be..8da6ec12380a921ef3f93431f01a16b676a6c085 100644 (file)
@@ -101,8 +101,7 @@ RSA *RSA_generate_key(int bits, unsigned long e_value,
  err:
     if (e)
         BN_free(e);
-    if (rsa)
-        RSA_free(rsa);
+    RSA_free(rsa);
     if (cb)
         BN_GENCB_free(cb);
     return 0;
index 610b4f9cf220d79652cf0cda6b6a0a5ea953fe30..33303cc1179e3868784cf7a5fa6ce021ddfd68b3 100644 (file)
@@ -637,8 +637,7 @@ void tls_set_dhe1024(int i, void *apparg)
         tls_openssl_errors("", "", NULL, apparg);
         return;
     }
-    if (tls_dhe1024 != NULL)
-        DH_free(tls_dhe1024);
+    DH_free(tls_dhe1024);
     tls_dhe1024 = dhparams;
 }
 
index 60c930093e02f095ab89379071defeb362ff0057..6245e4adaf7587a4a2f1137692e6d6872025dafa 100644 (file)
@@ -18,6 +18,7 @@ DH_new() allocates and initializes a B<DH> structure.
 
 DH_free() frees the B<DH> structure and its components. The values are
 erased before the memory is returned to the system.
+If B<dh> is NULL nothing is done.
 
 =head1 RETURN VALUES
 
index 48e9b82a09c8dbba2ab4225632c9311313ab2627..3a6d58246581a4905c063f71187eb32a860bb150 100644 (file)
@@ -19,6 +19,7 @@ calling DSA_new_method(NULL).
 
 DSA_free() frees the B<DSA> structure and its components. The values are
 erased before the memory is returned to the system.
+If B<dsa> is NULL nothing is done.
 
 =head1 RETURN VALUES
 
index 3d15b928243d55c2145404f0c6c31c0096926c6f..70901a556b322f11deb1b135bb20329e385a0ad5 100644 (file)
@@ -19,6 +19,7 @@ calling RSA_new_method(NULL).
 
 RSA_free() frees the B<RSA> structure and its components. The key is
 erased before the memory is returned to the system.
+If B<rsa> is NULL nothing is done.
 
 =head1 RETURN VALUES
 
index ad8480daadb8cfef0ed974fb1debbdc48ae8d5ac..a5d80a10a8eb43b18bea30d1a0f9da46dccfda96 100644 (file)
@@ -276,9 +276,7 @@ static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
 /* --------------------- free functions * ------------------------------*/
 static void pkey_free_gost94(EVP_PKEY *key)
 {
-    if (key->pkey.dsa) {
-        DSA_free(key->pkey.dsa);
-    }
+    DSA_free(key->pkey.dsa);
 }
 
 static void pkey_free_gost01(EVP_PKEY *key)
index f280397ad8167f8dd022c86d64ea714c7c059b8f..2373d6903337a3c48536ce4f0cccf9afd0c8ba81 100644 (file)
@@ -754,10 +754,8 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key)
     if (pubkey)
         OPENSSL_free(pubkey);
     if (!ret) {
-        if (rkey)
-            RSA_free(rkey);
-        if (dkey)
-            DSA_free(dkey);
+        RSA_free(rkey);
+        DSA_free(dkey);
     }
 
     return ret;
index 69d49d7d3a88bd8979f85aab7c4ea14182acd0e7..19d29d7e62f7fc909c318f8960843f95c0ac6244 100644 (file)
@@ -849,8 +849,7 @@ static EVP_PKEY *hwcrhk_load_privkey(ENGINE *eng, const char *key_id,
     return res;
  err:
 #  ifndef OPENSSL_NO_RSA
-    if (rtmp)
-        RSA_free(rtmp);
+    RSA_free(rtmp);
 #  endif
     return NULL;
 }
index 36f6f4310410d7e1c03699bed327b631cc1e7618..262766c6027be5fda8994471a089e9809bfda69b 100644 (file)
@@ -801,12 +801,10 @@ static EVP_PKEY *sureware_load_public(ENGINE *e, const char *key_id,
     return res;
  err:
 #  ifndef OPENSSL_NO_RSA
-    if (rsatmp)
-        RSA_free(rsatmp);
+    RSA_free(rsatmp);
 #  endif
 #  ifndef OPENSSL_NO_DSA
-    if (dsatmp)
-        DSA_free(dsatmp);
+    DSA_free(dsatmp);
 #  endif
     return NULL;
 }
index f4b60bed4914dc74f095c39a34bc2aee2a8f75c1..27f03d4937062bb5535ed965c7325bd4087d68fc 100644 (file)
@@ -1380,16 +1380,12 @@ int ssl3_get_key_exchange(SSL *s)
     param = p = (unsigned char *)s->init_msg;
     if (s->session->sess_cert != NULL) {
 #ifndef OPENSSL_NO_RSA
-        if (s->session->sess_cert->peer_rsa_tmp != NULL) {
-            RSA_free(s->session->sess_cert->peer_rsa_tmp);
-            s->session->sess_cert->peer_rsa_tmp = NULL;
-        }
+        RSA_free(s->session->sess_cert->peer_rsa_tmp);
+        s->session->sess_cert->peer_rsa_tmp = NULL;
 #endif
 #ifndef OPENSSL_NO_DH
-        if (s->session->sess_cert->peer_dh_tmp) {
-            DH_free(s->session->sess_cert->peer_dh_tmp);
-            s->session->sess_cert->peer_dh_tmp = NULL;
-        }
+        DH_free(s->session->sess_cert->peer_dh_tmp);
+        s->session->sess_cert->peer_dh_tmp = NULL;
 #endif
 #ifndef OPENSSL_NO_EC
         if (s->session->sess_cert->peer_ecdh_tmp) {
@@ -1955,12 +1951,10 @@ int ssl3_get_key_exchange(SSL *s)
  err:
     EVP_PKEY_free(pkey);
 #ifndef OPENSSL_NO_RSA
-    if (rsa != NULL)
-        RSA_free(rsa);
+    RSA_free(rsa);
 #endif
 #ifndef OPENSSL_NO_DH
-    if (dh != NULL)
-        DH_free(dh);
+    DH_free(dh);
 #endif
 #ifndef OPENSSL_NO_EC
     BN_CTX_free(bn_ctx);
index 6c59824901f73c52600b78b65bdef4996f1b0938..9893930eef478d48eeaf634aaced9a42cb086560 100644 (file)
@@ -3138,8 +3138,7 @@ void ssl3_free(SSL *s)
     if (s->s3->rrec.comp != NULL)
         OPENSSL_free(s->s3->rrec.comp);
 #ifndef OPENSSL_NO_DH
-    if (s->s3->tmp.dh != NULL)
-        DH_free(s->s3->tmp.dh);
+    DH_free(s->s3->tmp.dh);
 #endif
 #ifndef OPENSSL_NO_EC
     if (s->s3->tmp.ecdh != NULL)
@@ -3181,10 +3180,8 @@ void ssl3_clear(SSL *s)
         s->s3->rrec.comp = NULL;
     }
 #ifndef OPENSSL_NO_DH
-    if (s->s3->tmp.dh != NULL) {
-        DH_free(s->s3->tmp.dh);
-        s->s3->tmp.dh = NULL;
-    }
+    DH_free(s->s3->tmp.dh);
+    s->s3->tmp.dh = NULL;
 #endif
 #ifndef OPENSSL_NO_EC
     if (s->s3->tmp.ecdh != NULL) {
@@ -3293,8 +3290,7 @@ long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg)
                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
                 return (ret);
             }
-            if (s->cert->rsa_tmp != NULL)
-                RSA_free(s->cert->rsa_tmp);
+            RSA_free(s->cert->rsa_tmp);
             s->cert->rsa_tmp = rsa;
             ret = 1;
         }
@@ -3329,8 +3325,7 @@ long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg)
                     return (ret);
                 }
             }
-            if (s->cert->dh_tmp != NULL)
-                DH_free(s->cert->dh_tmp);
+            DH_free(s->cert->dh_tmp);
             s->cert->dh_tmp = dh;
             ret = 1;
         }
@@ -3766,8 +3761,7 @@ long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
                 SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_RSA_LIB);
                 return (0);
             } else {
-                if (cert->rsa_tmp != NULL)
-                    RSA_free(cert->rsa_tmp);
+                RSA_free(cert->rsa_tmp);
                 cert->rsa_tmp = rsa;
                 return (1);
             }
@@ -3801,8 +3795,7 @@ long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
                     return 0;
                 }
             }
-            if (cert->dh_tmp != NULL)
-                DH_free(cert->dh_tmp);
+            DH_free(cert->dh_tmp);
             cert->dh_tmp = new;
             return 1;
         }
index a88d211bc52224576737bc0b877f0bf2f8d2c3e9..cbfe7bb416c0996e4410cf667ef37b511b8baedb 100644 (file)
@@ -443,12 +443,10 @@ void ssl_cert_free(CERT *c)
 #endif
 
 #ifndef OPENSSL_NO_RSA
-    if (c->rsa_tmp)
-        RSA_free(c->rsa_tmp);
+    RSA_free(c->rsa_tmp);
 #endif
 #ifndef OPENSSL_NO_DH
-    if (c->dh_tmp)
-        DH_free(c->dh_tmp);
+    DH_free(c->dh_tmp);
 #endif
 #ifndef OPENSSL_NO_EC
     if (c->ecdh_tmp)
@@ -651,12 +649,10 @@ void ssl_sess_cert_free(SESS_CERT *sc)
     }
 
 #ifndef OPENSSL_NO_RSA
-    if (sc->peer_rsa_tmp != NULL)
-        RSA_free(sc->peer_rsa_tmp);
+    RSA_free(sc->peer_rsa_tmp);
 #endif
 #ifndef OPENSSL_NO_DH
-    if (sc->peer_dh_tmp != NULL)
-        DH_free(sc->peer_dh_tmp);
+    DH_free(sc->peer_dh_tmp);
 #endif
 #ifndef OPENSSL_NO_EC
     if (sc->peer_ecdh_tmp != NULL)
index cfed40ddc2bd4fa4d754c579a3c6fc32f4b92777..25af065233b9f2345dc895b548f26af7a20e3c91 100644 (file)
@@ -421,8 +421,7 @@ static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
     if (cctx->ssl)
         rv = SSL_set_tmp_dh(cctx->ssl, dh);
  end:
-    if (dh)
-        DH_free(dh);
+    DH_free(dh);
     if (in)
         BIO_free(in);
     return rv > 0;
index 457ba86a7bf6f4945abdf1aa28aa012c75b7d3c0..d244ba3cd93c7815fd95cb063df9643d15d3d221 100644 (file)
@@ -2968,10 +2968,8 @@ static RSA *tmp_rsa_cb(SSL *s, int is_export, int keylength)
 
 static void free_tmp_rsa(void)
 {
-    if (rsa_tmp != NULL) {
-        RSA_free(rsa_tmp);
-        rsa_tmp = NULL;
-    }
+    RSA_free(rsa_tmp);
+    rsa_tmp = NULL;
 }
 #endif