Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coverity issues, resource leaks #622

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/speed.c
Original file line number Diff line number Diff line change
Expand Up @@ -5357,6 +5357,7 @@ static int do_multi(int multi, int size_num)
close(1);
if (dup(fd[1]) == -1) {
BIO_printf(bio_err, "dup failed\n");
close(fd[1]);
exit(1);
}
close(fd[1]);
Expand Down
2 changes: 1 addition & 1 deletion crypto/ec/ec_elgamal_crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ EC_ELGAMAL_MR_CTX *EC_ELGAMAL_MR_CTX_new(STACK_OF(EC_KEY) *keys, const EC_POINT

if (h != NULL) {
if (!(ctx->h = EC_POINT_dup(h, ctx->group)))
return 0;
goto err;
} else {
ctx->h = EC_POINT_new(group);
if (ctx->h == NULL) {
Expand Down
18 changes: 8 additions & 10 deletions crypto/ec/ec_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1544,13 +1544,13 @@ int EC_POINTs_from_strings(const EC_GROUP *group, EC_POINTS **r,
{
int ret = 0;
BN_CTX *new_ctx = NULL;
EC_POINTS *result = NULL;
EC_POINTS *result = NULL, *new_r = NULL;

if (r == NULL || strings == NULL || num <= 0)
return 0;

if (*r == NULL) {
result = EC_POINTS_new(group, (uint32_t)num);
new_r = result = EC_POINTS_new(group, (uint32_t)num);
if (result == NULL)
return 0;
} else {
Expand All @@ -1574,14 +1574,13 @@ int EC_POINTs_from_strings(const EC_GROUP *group, EC_POINTS **r,
}
#endif

result = NULL;

/*
* TODO
*/
err:
BN_CTX_free(new_ctx);
EC_POINTS_free(result);
if (*r != new_r)
EC_POINTS_free(new_r);
return ret;
}

Expand All @@ -1600,13 +1599,13 @@ int EC_POINTs_from_strings_scalar_mul(const EC_GROUP *group, EC_POINTS **r,
{
int ret = 0;
BN_CTX *new_ctx = NULL;
EC_POINTS *result = NULL;
EC_POINTS *result, *new_r = NULL;

if (r == NULL || strings == NULL || num <= 0)
return 0;

if (*r == NULL) {
result = EC_POINTS_new(group, (uint32_t)num);
new_r = result = EC_POINTS_new(group, (uint32_t)num);
if (result == NULL)
return 0;
} else {
Expand All @@ -1632,14 +1631,13 @@ int EC_POINTs_from_strings_scalar_mul(const EC_GROUP *group, EC_POINTS **r,
}
#endif

result = NULL;

/*
* TODO
*/
err:
BN_CTX_free(new_ctx);
EC_POINTS_free(result);
if (*r != new_r)
EC_POINTS_free(new_r);
return ret;
}

Expand Down
4 changes: 3 additions & 1 deletion crypto/zkp/bulletproofs/bulletproofs_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ static int bp_inner_product_proof_encode(bp_inner_product_proof_t *ip_proof,

len += sk_len;

if (out == NULL)
if (out == NULL) {
sk_BIGNUM_free(sk_bn);
return len;
}

sk_len = zkp_stack_of_bignum_encode(sk_bn, p, bn_len);
if (sk_len == 0)
Expand Down
1 change: 1 addition & 0 deletions crypto/zkp/gadget/zkp_range_proof.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ void ZKP_RANGE_PROOF_free(ZKP_RANGE_PROOF *proof)

NIZK_PLAINTEXT_KNOWLEDGE_PROOF_free(proof->ptke_proof);
BP_RANGE_PROOF_free(proof->bp_proof);
OPENSSL_free(proof);
}

ZKP_RANGE_PROOF *ZKP_RANGE_PROOF_prove(ZKP_RANGE_CTX *ctx, int left_bound_bits,
Expand Down
2 changes: 1 addition & 1 deletion crypto/zkp/nizk/nizk_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ NIZK_WITNESS *NIZK_WITNESS_decode(const unsigned char *in, size_t size, int flag
if (flag == 1) {
if (size < (sizeof(int) + bn_len * 3)) {
ERR_raise(ERR_LIB_ZKP_NIZK, ERR_R_PASSED_INVALID_ARGUMENT);
return NULL;
goto err;
}

witness->v = zkp_bignum_decode(p, NULL, bn_len);
Expand Down
1 change: 1 addition & 0 deletions test/bntest.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static BIGNUM *getBN(STANZA *s, const char *attribute)

if (parseBN(&ret, hex) != (int)strlen(hex)) {
TEST_error("Could not decode '%s'", hex);
BN_free(ret);
return NULL;
}
return ret;
Expand Down
2 changes: 2 additions & 0 deletions test/bulletproofs_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1134,9 +1134,11 @@ static BP_R1CS_PROOF *r1cs_range_prove(BP_R1CS_CTX *ctx, BP_WITNESS *witness,
if (!(proof = BP_R1CS_PROOF_prove(ctx)))
goto err;

BN_free(v);
return proof;

err:
BN_free(v);
BP_R1CS_LINEAR_COMBINATION_free(lc);
BP_R1CS_PROOF_free(proof);
return NULL;
Expand Down
4 changes: 4 additions & 0 deletions test/nizk_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ static int nizk_plaintext_knowledge_test(int plaintext)
ret = 1;

err:
EC_ELGAMAL_CIPHERTEXT_free(enc_ct);
EC_ELGAMAL_CTX_free(enc_ctx);
NIZK_PLAINTEXT_KNOWLEDGE_PROOF_free(proof);
NIZK_PLAINTEXT_KNOWLEDGE_CTX_free(ctx);
NIZK_WITNESS_free(witness);
Expand Down Expand Up @@ -176,6 +178,8 @@ static int nizk_plaintext_equality_test(int plaintext)
ret = 1;

err:
EC_ELGAMAL_MR_CTX_free(enc_ctx);
EC_ELGAMAL_MR_CIPHERTEXT_free(enc_ct);
NIZK_PLAINTEXT_EQUALITY_PROOF_free(proof);
NIZK_PLAINTEXT_EQUALITY_CTX_free(ctx);
NIZK_WITNESS_free(witness);
Expand Down
2 changes: 2 additions & 0 deletions test/paillier_internal_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ static size_t paillier_add_plain(PAILLIER_CTX *ctx, unsigned char **out,
ret = size;

err:
OPENSSL_free(buf);
PAILLIER_CIPHERTEXT_free(c);
PAILLIER_CIPHERTEXT_free(r);
return ret;
Expand Down Expand Up @@ -270,6 +271,7 @@ static size_t paillier_mul(PAILLIER_CTX *ctx, unsigned char **out,
ret = size;

err:
OPENSSL_free(buf);
PAILLIER_CIPHERTEXT_free(c);
PAILLIER_CIPHERTEXT_free(r);
return ret;
Expand Down
1 change: 1 addition & 0 deletions test/pkcs12_format_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ static int test_set0_attrs(void)
return end_pkcs12_builder(pb);

err:
(void)end_pkcs12_builder(pb);
return 0;
}

Expand Down
3 changes: 0 additions & 3 deletions test/zkp_gadget_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ static int zkp_poly3_eval_test(void)
BN_CTX *bn_ctx = NULL;
STACK_OF(BIGNUM) *sk_eval = NULL;

if (!(sk_eval = sk_BIGNUM_new_reserve(NULL, n)))
goto err;

bn_ctx = BN_CTX_new();
if (bn_ctx == NULL)
goto err;
Expand Down
Loading