Remove unnecessary NULL checks, part 1.
First batch of the alphabet.
Change-Id: If4e60f4fbb69e04eb4b70aa1b2240e329251bfa5
Reviewed-on: https://boringssl-review.googlesource.com/4514
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/bio/buffer.c b/crypto/bio/buffer.c
index 5fbce7e..3fc0685 100644
--- a/crypto/bio/buffer.c
+++ b/crypto/bio/buffer.c
@@ -122,17 +122,13 @@
static int buffer_free(BIO *bio) {
BIO_F_BUFFER_CTX *ctx;
- if (bio == NULL) {
+ if (bio == NULL || bio->ptr == NULL) {
return 0;
}
ctx = (BIO_F_BUFFER_CTX *)bio->ptr;
- if (ctx->ibuf != NULL) {
- OPENSSL_free(ctx->ibuf);
- }
- if (ctx->obuf != NULL) {
- OPENSSL_free(ctx->obuf);
- }
+ OPENSSL_free(ctx->ibuf);
+ OPENSSL_free(ctx->obuf);
OPENSSL_free(bio->ptr);
bio->ptr = NULL;
diff --git a/crypto/bio/connect.c b/crypto/bio/connect.c
index 1ef08ed..32361bf 100644
--- a/crypto/bio/connect.c
+++ b/crypto/bio/connect.c
@@ -161,9 +161,7 @@
break;
}
}
- if (c->param_port != NULL) {
- OPENSSL_free(c->param_port);
- }
+ OPENSSL_free(c->param_port);
c->param_port = BUF_strdup(p);
}
}
@@ -286,12 +284,8 @@
return;
}
- if (c->param_hostname != NULL) {
- OPENSSL_free(c->param_hostname);
- }
- if (c->param_port != NULL) {
- OPENSSL_free(c->param_port);
- }
+ OPENSSL_free(c->param_hostname);
+ OPENSSL_free(c->param_port);
OPENSSL_free(c);
}
@@ -426,17 +420,13 @@
if (ptr != NULL) {
bio->init = 1;
if (num == 0) {
- if (data->param_hostname != NULL) {
- OPENSSL_free(data->param_hostname);
- }
+ OPENSSL_free(data->param_hostname);
data->param_hostname = BUF_strdup(ptr);
if (data->param_hostname == NULL) {
ret = 0;
}
} else if (num == 1) {
- if (data->param_port != NULL) {
- OPENSSL_free(data->param_port);
- }
+ OPENSSL_free(data->param_port);
data->param_port = BUF_strdup(ptr);
if (data->param_port == NULL) {
ret = 0;
diff --git a/crypto/bio/pair.c b/crypto/bio/pair.c
index de2b4cb..cc55950 100644
--- a/crypto/bio/pair.c
+++ b/crypto/bio/pair.c
@@ -145,7 +145,7 @@
bio_destroy_pair(bio);
}
- if (b->buf != NULL && !b->buf_externally_allocated) {
+ if (!b->buf_externally_allocated) {
OPENSSL_free(b->buf);
}
@@ -793,14 +793,10 @@
err:
if (ret == 0) {
- if (bio1) {
- BIO_free(bio1);
- bio1 = NULL;
- }
- if (bio2) {
- BIO_free(bio2);
- bio2 = NULL;
- }
+ BIO_free(bio1);
+ bio1 = NULL;
+ BIO_free(bio2);
+ bio2 = NULL;
}
*bio1_p = bio1;
diff --git a/crypto/bn/bn.c b/crypto/bn/bn.c
index f89b6f1..f32d6b0 100644
--- a/crypto/bn/bn.c
+++ b/crypto/bn/bn.c
@@ -88,7 +88,7 @@
return;
}
- if (bn->d != NULL && (bn->flags & BN_FLG_STATIC_DATA) == 0) {
+ if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
OPENSSL_free(bn->d);
}
@@ -304,9 +304,7 @@
memcpy(a, bn->d, sizeof(BN_ULONG) * bn->top);
- if (bn->d) {
- OPENSSL_free(bn->d);
- }
+ OPENSSL_free(bn->d);
bn->d = a;
bn->dmax = words;
diff --git a/crypto/bn/convert.c b/crypto/bn/convert.c
index 9c7b9be..531b661 100644
--- a/crypto/bn/convert.c
+++ b/crypto/bn/convert.c
@@ -407,13 +407,9 @@
ok = 1;
err:
- if (bn_data != NULL) {
- OPENSSL_free(bn_data);
- }
- if (t != NULL) {
- BN_free(t);
- }
- if (!ok && buf) {
+ OPENSSL_free(bn_data);
+ BN_free(t);
+ if (!ok) {
OPENSSL_free(buf);
buf = NULL;
}
diff --git a/crypto/bn/ctx.c b/crypto/bn/ctx.c
index e3a5c92..0578376 100644
--- a/crypto/bn/ctx.c
+++ b/crypto/bn/ctx.c
@@ -205,9 +205,7 @@
}
static void BN_STACK_finish(BN_STACK *st) {
- if (st->size) {
- OPENSSL_free(st->indexes);
- }
+ OPENSSL_free(st->indexes);
}
static int BN_STACK_push(BN_STACK *st, unsigned int idx) {
@@ -222,9 +220,7 @@
if (st->depth) {
memcpy(newitems, st->indexes, st->depth * sizeof(unsigned int));
}
- if (st->size) {
- OPENSSL_free(st->indexes);
- }
+ OPENSSL_free(st->indexes);
st->indexes = newitems;
st->size = newsize;
}
diff --git a/crypto/bn/div.c b/crypto/bn/div.c
index 8b0152e..3588ea1 100644
--- a/crypto/bn/div.c
+++ b/crypto/bn/div.c
@@ -492,9 +492,7 @@
ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
- if (abs_m) {
- BN_free(abs_m);
- }
+ BN_free(abs_m);
return ret;
}
diff --git a/crypto/bn/exponentiation.c b/crypto/bn/exponentiation.c
index 57f319a..d3063c9 100644
--- a/crypto/bn/exponentiation.c
+++ b/crypto/bn/exponentiation.c
@@ -763,7 +763,7 @@
ret = 1;
err:
- if (in_mont == NULL && mont != NULL) {
+ if (in_mont == NULL) {
BN_MONT_CTX_free(mont);
}
BN_CTX_end(ctx);
@@ -878,8 +878,7 @@
BN_CTX_start(ctx);
/* Allocate a montgomery context if it was not supplied by the caller.
- * If this is not done, things will break in the montgomery part.
- */
+ * If this is not done, things will break in the montgomery part. */
if (in_mont != NULL) {
mont = in_mont;
} else {
@@ -1191,14 +1190,12 @@
}
ret = 1;
err:
- if ((in_mont == NULL) && (mont != NULL)) {
+ if (in_mont == NULL) {
BN_MONT_CTX_free(mont);
}
if (powerbuf != NULL) {
OPENSSL_cleanse(powerbuf, powerbufLen);
- if (powerbufFree) {
- OPENSSL_free(powerbufFree);
- }
+ OPENSSL_free(powerbufFree);
}
BN_CTX_end(ctx);
return (ret);
@@ -1353,7 +1350,7 @@
ret = 1;
err:
- if (in_mont == NULL && mont != NULL) {
+ if (in_mont == NULL) {
BN_MONT_CTX_free(mont);
}
BN_CTX_end(ctx);
@@ -1557,7 +1554,7 @@
ret = 1;
err:
- if (in_mont == NULL && mont != NULL) {
+ if (in_mont == NULL) {
BN_MONT_CTX_free(mont);
}
BN_CTX_end(ctx);
diff --git a/crypto/bn/random.c b/crypto/bn/random.c
index 285bf26..3be7510 100644
--- a/crypto/bn/random.c
+++ b/crypto/bn/random.c
@@ -321,8 +321,6 @@
ret = 1;
err:
- if (k_bytes) {
- OPENSSL_free(k_bytes);
- }
+ OPENSSL_free(k_bytes);
return ret;
}
diff --git a/crypto/bn/sqrt.c b/crypto/bn/sqrt.c
index 07041f9..e71a818 100644
--- a/crypto/bn/sqrt.c
+++ b/crypto/bn/sqrt.c
@@ -420,7 +420,7 @@
end:
if (err) {
- if (ret != NULL && ret != in) {
+ if (ret != in) {
BN_clear_free(ret);
}
ret = NULL;
diff --git a/crypto/bytestring/cbb.c b/crypto/bytestring/cbb.c
index e146727..f1e09a2 100644
--- a/crypto/bytestring/cbb.c
+++ b/crypto/bytestring/cbb.c
@@ -66,7 +66,7 @@
void CBB_cleanup(CBB *cbb) {
if (cbb->base) {
- if (cbb->base->buf && cbb->base->can_resize) {
+ if (cbb->base->can_resize) {
OPENSSL_free(cbb->base->buf);
}
OPENSSL_free(cbb->base);
diff --git a/crypto/bytestring/cbs.c b/crypto/bytestring/cbs.c
index bd94cce..10f1a99 100644
--- a/crypto/bytestring/cbs.c
+++ b/crypto/bytestring/cbs.c
@@ -52,10 +52,8 @@
}
int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
- if (*out_ptr != NULL) {
- OPENSSL_free(*out_ptr);
- *out_ptr = NULL;
- }
+ OPENSSL_free(*out_ptr);
+ *out_ptr = NULL;
*out_len = 0;
if (cbs->len == 0) {
diff --git a/crypto/dh/dh.c b/crypto/dh/dh.c
index 77ebb1d..ab7ed8b 100644
--- a/crypto/dh/dh.c
+++ b/crypto/dh/dh.c
@@ -179,9 +179,7 @@
}
}
- if (*dst) {
- BN_free(*dst);
- }
+ BN_free(*dst);
*dst = a;
return 1;
}
@@ -204,11 +202,10 @@
return 0;
}
- if (to->seed) {
- OPENSSL_free(to->seed);
- to->seed = NULL;
- to->seedlen = 0;
- }
+ OPENSSL_free(to->seed);
+ to->seed = NULL;
+ to->seedlen = 0;
+
if (from->seed) {
to->seed = BUF_memdup(from->seed, from->seedlen);
if (!to->seed) {
diff --git a/crypto/dh/dh_impl.c b/crypto/dh/dh_impl.c
index 81d777d..f269412 100644
--- a/crypto/dh/dh_impl.c
+++ b/crypto/dh/dh_impl.c
@@ -245,10 +245,10 @@
OPENSSL_PUT_ERROR(DH, generate_key, ERR_R_BN_LIB);
}
- if (pub_key != NULL && dh->pub_key == NULL) {
+ if (dh->pub_key == NULL) {
BN_free(pub_key);
}
- if (priv_key != NULL && dh->priv_key == NULL) {
+ if (dh->priv_key == NULL) {
BN_free(priv_key);
}
BN_CTX_free(ctx);
diff --git a/crypto/dh/dh_test.c b/crypto/dh/dh_test.c
index 836b3b2..a1fc83f 100644
--- a/crypto/dh/dh_test.c
+++ b/crypto/dh/dh_test.c
@@ -189,18 +189,10 @@
err:
ERR_print_errors_fp(stderr);
- if (abuf != NULL) {
- OPENSSL_free(abuf);
- }
- if (bbuf != NULL) {
- OPENSSL_free(bbuf);
- }
- if (b != NULL) {
- DH_free(b);
- }
- if (a != NULL) {
- DH_free(a);
- }
+ OPENSSL_free(abuf);
+ OPENSSL_free(bbuf);
+ DH_free(b);
+ DH_free(a);
return ret;
}
@@ -493,18 +485,10 @@
ERR_print_errors_fp(stderr);
err:
- if (Z1 != NULL) {
- OPENSSL_free(Z1);
- }
- if (Z2 != NULL) {
- OPENSSL_free(Z2);
- }
- if (dhA != NULL) {
- DH_free(dhA);
- }
- if (dhB != NULL) {
- DH_free(dhB);
- }
+ OPENSSL_free(Z1);
+ OPENSSL_free(Z2);
+ DH_free(dhA);
+ DH_free(dhB);
fprintf(stderr, "Test failed RFC5114 set %d\n", i + 1);
return 0;
diff --git a/crypto/dsa/dsa.c b/crypto/dsa/dsa.c
index 78e4154..e8e3d73 100644
--- a/crypto/dsa/dsa.c
+++ b/crypto/dsa/dsa.c
@@ -134,27 +134,13 @@
CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
- if (dsa->p != NULL) {
- BN_clear_free(dsa->p);
- }
- if (dsa->q != NULL) {
- BN_clear_free(dsa->q);
- }
- if (dsa->g != NULL) {
- BN_clear_free(dsa->g);
- }
- if (dsa->pub_key != NULL) {
- BN_clear_free(dsa->pub_key);
- }
- if (dsa->priv_key != NULL) {
- BN_clear_free(dsa->priv_key);
- }
- if (dsa->kinv != NULL) {
- BN_clear_free(dsa->kinv);
- }
- if (dsa->r != NULL) {
- BN_clear_free(dsa->r);
- }
+ BN_clear_free(dsa->p);
+ BN_clear_free(dsa->q);
+ BN_clear_free(dsa->g);
+ BN_clear_free(dsa->pub_key);
+ BN_clear_free(dsa->priv_key);
+ BN_clear_free(dsa->kinv);
+ BN_clear_free(dsa->r);
CRYPTO_MUTEX_cleanup(&dsa->method_mont_p_lock);
OPENSSL_free(dsa);
}
@@ -198,12 +184,8 @@
return;
}
- if (sig->r) {
- BN_free(sig->r);
- }
- if (sig->s) {
- BN_free(sig->s);
- }
+ BN_free(sig->r);
+ BN_free(sig->s);
OPENSSL_free(sig);
}
@@ -282,12 +264,8 @@
ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
err:
- if (der != NULL) {
- OPENSSL_free(der);
- }
- if (s) {
- DSA_SIG_free(s);
- }
+ OPENSSL_free(der);
+ DSA_SIG_free(s);
return ret;
}
@@ -365,8 +343,6 @@
return ret;
err:
- if (ret != NULL) {
- DH_free(ret);
- }
+ DH_free(ret);
return NULL;
}
diff --git a/crypto/dsa/dsa_impl.c b/crypto/dsa/dsa_impl.c
index c4df80b..b7e1fd8 100644
--- a/crypto/dsa/dsa_impl.c
+++ b/crypto/dsa/dsa_impl.c
@@ -162,14 +162,10 @@
goto err;
}
- if (*kinvp != NULL) {
- BN_clear_free(*kinvp);
- }
+ BN_clear_free(*kinvp);
*kinvp = kinv;
kinv = NULL;
- if (*rp != NULL) {
- BN_clear_free(*rp);
- }
+ BN_clear_free(*rp);
*rp = r;
ret = 1;
@@ -277,15 +273,10 @@
BN_free(r);
BN_free(s);
}
- if (ctx != NULL) {
- BN_CTX_free(ctx);
- }
+ BN_CTX_free(ctx);
BN_clear_free(&m);
BN_clear_free(&xr);
- if (kinv != NULL) {
- /* dsa->kinv is NULL now if we used it */
- BN_clear_free(kinv);
- }
+ BN_clear_free(kinv);
return ret;
}
@@ -392,9 +383,7 @@
if (ret != 1) {
OPENSSL_PUT_ERROR(DSA, verify, ERR_R_BN_LIB);
}
- if (ctx != NULL) {
- BN_CTX_free(ctx);
- }
+ BN_CTX_free(ctx);
BN_free(&u1);
BN_free(&u2);
BN_free(&t1);
@@ -447,15 +436,13 @@
ok = 1;
err:
- if (pub_key != NULL && dsa->pub_key == NULL) {
+ if (dsa->pub_key == NULL) {
BN_free(pub_key);
}
- if (priv_key != NULL && dsa->priv_key == NULL) {
+ if (dsa->priv_key == NULL) {
BN_free(priv_key);
}
- if (ctx != NULL) {
- BN_CTX_free(ctx);
- }
+ BN_CTX_free(ctx);
return ok;
}
@@ -706,15 +693,9 @@
err:
if (ok) {
- if (ret->p) {
- BN_free(ret->p);
- }
- if (ret->q) {
- BN_free(ret->q);
- }
- if (ret->g) {
- BN_free(ret->g);
- }
+ BN_free(ret->p);
+ BN_free(ret->q);
+ BN_free(ret->g);
ret->p = BN_dup(p);
ret->q = BN_dup(q);
ret->g = BN_dup(g);
@@ -735,9 +716,7 @@
BN_CTX_free(ctx);
}
- if (mont != NULL) {
- BN_MONT_CTX_free(mont);
- }
+ BN_MONT_CTX_free(mont);
return ok;
}
diff --git a/crypto/dsa/dsa_test.c b/crypto/dsa/dsa_test.c
index 83ecf25..9b70dbe 100644
--- a/crypto/dsa/dsa_test.c
+++ b/crypto/dsa/dsa_test.c
@@ -247,9 +247,7 @@
}
end:
- if (dsa != NULL) {
- DSA_free(dsa);
- }
+ DSA_free(dsa);
return ok;
}
@@ -271,9 +269,7 @@
ERR_clear_error();
end:
- if (dsa != NULL) {
- DSA_free(dsa);
- }
+ DSA_free(dsa);
return ok;
}