Allow NULL for some _free routines.
authorRich Salz <rsalz@openssl.org>
Tue, 27 Mar 2018 20:25:08 +0000 (16:25 -0400)
committerRich Salz <rsalz@openssl.org>
Tue, 27 Mar 2018 20:25:08 +0000 (16:25 -0400)
Based on the description in https://github.com/openssl/openssl/pull/5757,
this re-implements the "allow NULL to be passed" behavior of a number of
xxx_free routines.  I also fixed up some egregious formatting errors
that were nearby.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/5761)

15 files changed:
crypto/bio/bss_acpt.c
crypto/bio/bss_conn.c
crypto/bn/bn_blind.c
crypto/bn/bn_ctx.c
crypto/bn/bn_mont.c
crypto/bn/bn_recp.c
crypto/buffer/buffer.c
crypto/comp/comp_lib.c
crypto/err/err.c
crypto/txt_db/txt_db.c
crypto/x509/x509_lu.c
ssl/s3_lib.c
ssl/ssl_cert.c
ssl/ssl_lib.c
ssl/ssl_sess.c

index 0171c496076e99d94bb7bb59de7cea03fcb1e6d6..64cc452891a57ff698f1ed1108d011847013f7bf 100644 (file)
@@ -101,6 +101,8 @@ static BIO_ACCEPT *BIO_ACCEPT_new(void)
 
 static void BIO_ACCEPT_free(BIO_ACCEPT *a)
 {
+    if (a == NULL)
+        return;
     OPENSSL_free(a->param_addr);
     OPENSSL_free(a->param_serv);
     BIO_ADDRINFO_free(a->addr_first);
index 0fad02fdd5987cf388f33a6ade68e07b4e585a34..cc245ab07c50675fde358226d91f0ad3d48c2226 100644 (file)
@@ -232,6 +232,8 @@ BIO_CONNECT *BIO_CONNECT_new(void)
 
 void BIO_CONNECT_free(BIO_CONNECT *a)
 {
+    if (a == NULL)
+        return;
     OPENSSL_free(a->param_hostname);
     OPENSSL_free(a->param_service);
     BIO_ADDRINFO_free(a->addr_first);
index 8bd61567e7daed7980c38db0253c6c4d77025abb..985d3ef32bb66a190647f6ba456b8168310fbcdd 100644 (file)
@@ -80,6 +80,8 @@ BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
 
 void BN_BLINDING_free(BN_BLINDING *r)
 {
+    if (r == NULL)
+        return;
     BN_free(r->A);
     BN_free(r->Ai);
     BN_free(r->e);
index 7202aef326d336c346f35d38559a40ed9ffb5b31..68c04687437f1d9fdf7971adb60fa68399d57d38 100644 (file)
@@ -156,6 +156,8 @@ BN_CTX *BN_CTX_secure_new(void)
 
 void BN_CTX_free(BN_CTX *ctx)
 {
+    if (ctx == NULL)
+        return;
 #ifdef BN_CTX_DEBUG
     {
         BN_POOL_ITEM *pool = ctx->pool.head;
index bae7d235bd125818384a486149fa00327ea30abd..c882891d5e2ef9136d8ed3d690fd0fccbfcb30e6 100644 (file)
@@ -208,18 +208,20 @@ BN_MONT_CTX *BN_MONT_CTX_new(void)
 void BN_MONT_CTX_init(BN_MONT_CTX *ctx)
 {
     ctx->ri = 0;
-    bn_init(&(ctx->RR));
-    bn_init(&(ctx->N));
-    bn_init(&(ctx->Ni));
+    bn_init(&ctx->RR);
+    bn_init(&ctx->N);
+    bn_init(&ctx->Ni);
     ctx->n0[0] = ctx->n0[1] = 0;
     ctx->flags = 0;
 }
 
 void BN_MONT_CTX_free(BN_MONT_CTX *mont)
 {
-    BN_clear_free(&(mont->RR));
-    BN_clear_free(&(mont->N));
-    BN_clear_free(&(mont->Ni));
+    if (mont == NULL)
+        return;
+    BN_clear_free(&mont->RR);
+    BN_clear_free(&mont->N);
+    BN_clear_free(&mont->Ni);
     if (mont->flags & BN_FLG_MALLOCED)
         OPENSSL_free(mont);
 }
index 923a9b33d8f54be0cdc29194d74a44c0663cdccb..8eb500b61a800d992271f40588c5bd0db0cf01da 100644 (file)
@@ -32,8 +32,10 @@ BN_RECP_CTX *BN_RECP_CTX_new(void)
 
 void BN_RECP_CTX_free(BN_RECP_CTX *recp)
 {
-    BN_free(&(recp->N));
-    BN_free(&(recp->Nr));
+    if (recp == NULL)
+        return;
+    BN_free(&recp->N);
+    BN_free(&recp->Nr);
     if (recp->flags & BN_FLG_MALLOCED)
         OPENSSL_free(recp);
 }
index dfa5c23d1d24aaf20fc6ac9b9285a68c22f5d9fd..48618a4435a948407d3abae88c8a3f98fe947f3a 100644 (file)
@@ -42,6 +42,8 @@ BUF_MEM *BUF_MEM_new(void)
 
 void BUF_MEM_free(BUF_MEM *a)
 {
+    if (a == NULL)
+        return;
     if (a->data != NULL) {
         if (a->flags & BUF_MEM_FLAG_SECURE)
             OPENSSL_secure_clear_free(a->data, a->max);
index 5bed1876a80e55098cc865a90d7b055434910cd0..c199bb352f139f9a5768402cf9a07cdfd245b709 100644 (file)
@@ -45,6 +45,8 @@ const char *COMP_get_name(const COMP_METHOD *meth)
 
 void COMP_CTX_free(COMP_CTX *ctx)
 {
+    if (ctx == NULL)
+        return;
     if (ctx->meth->finish != NULL)
         ctx->meth->finish(ctx);
 
index 4ae61786864e9b2233d4b97b2011dfd756c58e4d..4c5f354e5cf651f7bc717fa533ad3f2b81ea504a 100644 (file)
@@ -233,29 +233,30 @@ static void build_SYS_str_reasons(void)
 }
 #endif
 
-#define err_clear_data(p,i) \
+#define err_clear_data(p, i) \
         do { \
-        if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) \
-                {  \
+            if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) {\
                 OPENSSL_free((p)->err_data[i]); \
-                (p)->err_data[i]=NULL; \
-                } \
-        (p)->err_data_flags[i]=0; \
-        } while(0)
+                (p)->err_data[i] = NULL; \
+            } \
+            (p)->err_data_flags[i] = 0; \
+        } while (0)
 
-#define err_clear(p,i) \
+#define err_clear(p, i) \
         do { \
-        (p)->err_flags[i]=0; \
-        (p)->err_buffer[i]=0; \
-        err_clear_data(p,i); \
-        (p)->err_file[i]=NULL; \
-        (p)->err_line[i]= -1; \
-        } while(0)
+            err_clear_data(p, i); \
+            (p)->err_flags[i] = 0; \
+            (p)->err_buffer[i] = 0; \
+            (p)->err_file[i] = NULL; \
+            (p)->err_line[i] = -1; \
+        } while (0)
 
 static void ERR_STATE_free(ERR_STATE *s)
 {
     int i;
 
+    if (s == NULL)
+        return;
     for (i = 0; i < ERR_NUM_ERRORS; i++) {
         err_clear_data(s, i);
     }
index a08f346af6771f4bb9cefdc07344b2c7a6a77546..a00560dcbd994bf988d4e32f53a64932b1cdeb5c 100644 (file)
@@ -284,6 +284,8 @@ void TXT_DB_free(TXT_DB *db)
     int i, n;
     char **p, *max;
 
+    if (db == NULL)
+        return;
     if (db->index != NULL) {
         for (i = db->num_fields - 1; i >= 0; i--)
             lh_OPENSSL_STRING_free(db->index[i]);
index d69cedb72f8ec7cb5ea107b1d167cc1e1bde34d3..7b33ebad73b3fa01f587c1b568a52766773f3311 100644 (file)
@@ -178,6 +178,8 @@ void X509_STORE_free(X509_STORE *vfy)
     STACK_OF(X509_LOOKUP) *sk;
     X509_LOOKUP *lu;
 
+    if (vfy == NULL)
+        return;
     CRYPTO_DOWN_REF(&vfy->references, &i, vfy->lock);
     REF_PRINT_COUNT("X509_STORE", vfy);
     if (i > 0)
index 78a5a3a2e233e2b0956e8dc638a13ed2ff22e781..619326949c47bc2a8c5d4c8ec680cc748a9d8106 100644 (file)
@@ -3312,7 +3312,7 @@ int ssl3_new(SSL *s)
 
 void ssl3_free(SSL *s)
 {
-    if (s->s3 == NULL)
+    if (s == NULL || s->s3 == NULL)
         return;
 
     ssl3_cleanup_key_block(s);
index 5a465e39425088c0638d9d862886869df8046a99..b2b342767cf585caba2e5f65d2fac04ac71deb84 100644 (file)
@@ -225,6 +225,8 @@ void ssl_cert_free(CERT *c)
 {
     int i;
 
+    if (c == NULL)
+        return;
     CRYPTO_DOWN_REF(&c->references, &i, c->lock);
     REF_PRINT_COUNT("CERT", c);
     if (i > 0)
index b678fcfbbb3d6b17d8f1088a0e97435f93781c9a..9d4c4d48998066e41cd866d610a70071ba30af32 100644 (file)
@@ -1125,6 +1125,8 @@ void SSL_free(SSL *s)
 {
     int i;
 
+    if (s == NULL)
+        return;
     CRYPTO_DOWN_REF(&s->references, &i, s->lock);
     REF_PRINT_COUNT("SSL", s);
     if (i > 0)
index 1672cd2a9545a83371b4f7e13c0a8266d7c96388..f936cb687f3249b9a956f7a01339104ed660d6a1 100644 (file)
@@ -785,6 +785,8 @@ void SSL_SESSION_free(SSL_SESSION *ss)
 {
     int i;
 
+    if (ss == NULL)
+        return;
     CRYPTO_DOWN_REF(&ss->references, &i, ss->lock);
     REF_PRINT_COUNT("SSL_SESSION", ss);
     if (i > 0)