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>
This commit is contained in:
David Benjamin 2015-04-22 13:50:28 -04:00 committed by Adam Langley
parent de95d262ab
commit 22ccc2d8f1
18 changed files with 73 additions and 178 deletions

View File

@ -122,17 +122,13 @@ err1:
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;

View File

@ -161,9 +161,7 @@ static int conn_state(BIO *bio, BIO_CONNECT *c) {
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 @@ static void BIO_CONNECT_free(BIO_CONNECT *c) {
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 @@ static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) {
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;

View File

@ -145,7 +145,7 @@ static int bio_free(BIO *bio) {
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 @@ int BIO_new_bio_pair_external_buf(BIO** bio1_p, size_t writebuf1_len,
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;

View File

@ -88,7 +88,7 @@ void BN_free(BIGNUM *bn) {
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 @@ BIGNUM *bn_wexpand(BIGNUM *bn, unsigned words) {
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;

View File

@ -407,13 +407,9 @@ char *BN_bn2dec(const BIGNUM *a) {
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;
}

View File

@ -205,9 +205,7 @@ static void BN_STACK_init(BN_STACK *st) {
}
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 @@ static int BN_STACK_push(BN_STACK *st, unsigned int idx) {
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;
}

View File

@ -492,9 +492,7 @@ int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
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;
}

View File

@ -763,7 +763,7 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
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 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
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 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
}
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 @@ int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
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 @@ int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
ret = 1;
err:
if (in_mont == NULL && mont != NULL) {
if (in_mont == NULL) {
BN_MONT_CTX_free(mont);
}
BN_CTX_end(ctx);

View File

@ -321,8 +321,6 @@ int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range, const BIGNUM *priv,
ret = 1;
err:
if (k_bytes) {
OPENSSL_free(k_bytes);
}
OPENSSL_free(k_bytes);
return ret;
}

View File

@ -420,7 +420,7 @@ vrfy:
end:
if (err) {
if (ret != NULL && ret != in) {
if (ret != in) {
BN_clear_free(ret);
}
ret = NULL;

View File

@ -66,7 +66,7 @@ int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len) {
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);

View File

@ -52,10 +52,8 @@ size_t CBS_len(const CBS *cbs) {
}
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) {

View File

@ -179,9 +179,7 @@ static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src) {
}
}
if (*dst) {
BN_free(*dst);
}
BN_free(*dst);
*dst = a;
return 1;
}
@ -204,11 +202,10 @@ static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
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) {

View File

@ -245,10 +245,10 @@ err:
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);

View File

@ -189,18 +189,10 @@ int main(int argc, char *argv[]) {
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 @@ bad_err:
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;

View File

@ -134,27 +134,13 @@ void DSA_free(DSA *dsa) {
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 @@ void DSA_SIG_free(DSA_SIG *sig) {
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 @@ int DSA_check_signature(int *out_valid, const uint8_t *digest,
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 @@ DH *DSA_dup_DH(const DSA *r) {
return ret;
err:
if (ret != NULL) {
DH_free(ret);
}
DH_free(ret);
return NULL;
}

View File

@ -162,14 +162,10 @@ static int sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
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 @@ err:
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 @@ err:
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 @@ static int keygen(DSA *dsa) {
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 @@ end:
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 @@ err:
BN_CTX_free(ctx);
}
if (mont != NULL) {
BN_MONT_CTX_free(mont);
}
BN_MONT_CTX_free(mont);
return ok;
}

View File

@ -247,9 +247,7 @@ static int test_generate(FILE *out) {
}
end:
if (dsa != NULL) {
DSA_free(dsa);
}
DSA_free(dsa);
return ok;
}
@ -271,9 +269,7 @@ static int test_verify(const uint8_t *sig, size_t sig_len, int expect) {
ERR_clear_error();
end:
if (dsa != NULL) {
DSA_free(dsa);
}
DSA_free(dsa);
return ok;
}