Skip to content

Commit

Permalink
Merge pull request #684 from dongbeiouba/fix/coverity_20241225
Browse files Browse the repository at this point in the history
Fix coverity issues
  • Loading branch information
InfoHunter authored Dec 27, 2024
2 parents bf98e82 + f50c28d commit 9ab553c
Show file tree
Hide file tree
Showing 29 changed files with 113 additions and 90 deletions.
2 changes: 1 addition & 1 deletion apps/crl2pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int crl2pkcs7_main(int argc, char **argv)
prog = opt_init(argc, argv, crl2pkcs7_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
default:
case OPT_ERR:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
Expand Down
3 changes: 2 additions & 1 deletion apps/delecred.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ int delecred_main(int argc, char **argv)
ee_key_file = opt_arg();
break;
case OPT_SEC:
opt_int(opt_arg(), &valid_time);
if (!opt_int(opt_arg(), &valid_time))
goto opthelp;
break;
case OPT_EXPECT_VERIFY_MD:
expect_verify_hash = opt_arg();
Expand Down
12 changes: 6 additions & 6 deletions apps/ec_elgamal.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,17 @@ int ec_elgamal_main(int argc, char **argv)
}

action_sum = encrypt + decrypt + add + sub + mul;
if (action_sum == 0) {
BIO_printf(bio_err, "No action parameter specified.\n");
goto opthelp1;
} else if (action_sum != 1) {
BIO_printf(bio_err, "Only one action parameter must be specified.\n");
if (action_sum != 1) {
if (action_sum == 0)
BIO_printf(bio_err, "No action parameter specified.\n");
else
BIO_printf(bio_err, "Only one action parameter must be specified.\n");

goto opthelp1;
}

while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
case OPT_ERR:
opthelp2:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
Expand Down
2 changes: 1 addition & 1 deletion apps/errstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int errstr_main(int argc, char **argv)
prog = opt_init(argc, argv, errstr_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
default:
case OPT_ERR:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
goto end;
Expand Down
2 changes: 1 addition & 1 deletion apps/gendsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int gendsa_main(int argc, char **argv)
prog = opt_init(argc, argv, gendsa_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
default:
case OPT_ERR:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
Expand Down
26 changes: 16 additions & 10 deletions apps/lib/s_cb.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,28 @@ int verify_callback(int ok, X509_STORE_CTX *ctx)
}
switch (err) {
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
BIO_puts(bio_err, "issuer= ");
X509_NAME_print_ex(bio_err, X509_get_issuer_name(err_cert),
0, get_nameopt());
BIO_puts(bio_err, "\n");
if (err_cert != NULL) {
BIO_puts(bio_err, "issuer= ");
X509_NAME_print_ex(bio_err, X509_get_issuer_name(err_cert),
0, get_nameopt());
BIO_puts(bio_err, "\n");
}
break;
case X509_V_ERR_CERT_NOT_YET_VALID:
case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
BIO_printf(bio_err, "notBefore=");
ASN1_TIME_print(bio_err, X509_get0_notBefore(err_cert));
BIO_printf(bio_err, "\n");
if (err_cert != NULL) {
BIO_printf(bio_err, "notBefore=");
ASN1_TIME_print(bio_err, X509_get0_notBefore(err_cert));
BIO_printf(bio_err, "\n");
}
break;
case X509_V_ERR_CERT_HAS_EXPIRED:
case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
BIO_printf(bio_err, "notAfter=");
ASN1_TIME_print(bio_err, X509_get0_notAfter(err_cert));
BIO_printf(bio_err, "\n");
if (err_cert != NULL) {
BIO_printf(bio_err, "notAfter=");
ASN1_TIME_print(bio_err, X509_get0_notAfter(err_cert));
BIO_printf(bio_err, "\n");
}
break;
case X509_V_ERR_NO_EXPLICIT_POLICY:
if (!verify_args.quiet)
Expand Down
2 changes: 1 addition & 1 deletion apps/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ int list_main(int argc, char **argv)
prog = opt_init(argc, argv, list_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF: /* Never hit, but suppresses warning */
default:
case OPT_ERR:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
Expand Down
2 changes: 1 addition & 1 deletion apps/nseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int nseq_main(int argc, char **argv)
prog = opt_init(argc, argv, nseq_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
default:
case OPT_ERR:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
Expand Down
12 changes: 6 additions & 6 deletions apps/paillier.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ int paillier_main(int argc, char **argv)
prog = opt_init(argc, argv, paillier_options);
if ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
case OPT_ERR:
opthelp1:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
Expand Down Expand Up @@ -492,11 +491,12 @@ int paillier_main(int argc, char **argv)
}

action_sum = keygen + pubgen + key + pub + encrypt + decrypt + add + add_plain + sub + mul;
if (action_sum == 0) {
BIO_printf(bio_err, "No action parameter specified.\n");
goto opthelp1;
} else if (action_sum != 1) {
BIO_printf(bio_err, "Only one action parameter must be specified.\n");
if (action_sum != 1) {
if (action_sum == 0)
BIO_printf(bio_err, "No action parameter specified.\n");
else
BIO_printf(bio_err, "Only one action parameter must be specified.\n");

goto opthelp1;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/pkeyparam.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int pkeyparam_main(int argc, char **argv)
prog = opt_init(argc, argv, pkeyparam_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
default:
case OPT_ERR:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
Expand Down
2 changes: 1 addition & 1 deletion apps/prime.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int prime_main(int argc, char **argv)
prog = opt_init(argc, argv, prime_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
default:
case OPT_ERR:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
Expand Down
2 changes: 1 addition & 1 deletion apps/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int rand_main(int argc, char **argv)
prog = opt_init(argc, argv, rand_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
default:
case OPT_ERR:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
Expand Down
5 changes: 3 additions & 2 deletions apps/rehash.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ static int add_entry(enum Type type, unsigned int hash, const char *filename,
if (need_symlink && !ep->need_symlink) {
ep->need_symlink = 1;
bp->num_needed++;
memcpy(ep->digest, digest, evpmdsize);
if (digest)
memcpy(ep->digest, digest, evpmdsize);
}
return 0;
}
Expand Down Expand Up @@ -488,7 +489,7 @@ int rehash_main(int argc, char **argv)
prog = opt_init(argc, argv, rehash_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
default:
case OPT_ERR:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
goto end;
Expand Down
20 changes: 12 additions & 8 deletions apps/speed.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ static OPT_PAIR bulletproofs_choices[] = {
# endif
};

static int bulletproofs_bits[] = {16, 32, 64};
static int bulletproofs_bits[] = {16, 32, 63};
static int bulletproofs_agg_max[] = {1, 16, 32};

# define BULLETPROOFS_NUM OSSL_NELEM(bulletproofs_choices)
Expand Down Expand Up @@ -1008,22 +1008,26 @@ static int EVP_Update_loop(void *args)
rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
if (rc != 1) {
/* reset iv in case of counter overflow */
EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
rc = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
}
}
} else {
for (count = 0; COND(c[D_EVP][testnum]); count++) {
rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
if (rc != 1) {
/* reset iv in case of counter overflow */
EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
rc = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
}
}
}
if (decrypt)
EVP_DecryptFinal_ex(ctx, buf, &outl);
rc = EVP_DecryptFinal_ex(ctx, buf, &outl);
else
EVP_EncryptFinal_ex(ctx, buf, &outl);
rc = EVP_EncryptFinal_ex(ctx, buf, &outl);

if (rc == 0)
BIO_printf(bio_err, "Error finalizing cipher loop\n");

return count;
}

Expand Down Expand Up @@ -4769,7 +4773,7 @@ int speed_main(int argc, char **argv)

#ifndef OPENSSL_NO_BULLETPROOFS
for (i = 1; i < sizeof(bp_secrets)/sizeof(bp_secrets[0]); i++) {
bp_secrets[i] = (1U << i) - 1;
bp_secrets[i] = (1ULL << i) - 1;
}

if (!(v = BN_new()))
Expand All @@ -4783,7 +4787,7 @@ int speed_main(int argc, char **argv)
continue; /* Ignore Curve */

for (m = 0; m < BULLETPROOFS_BITS_NUM; m++) {
bp_secrets[0] = (1U << bulletproofs_bits[m]) - 1;
bp_secrets[0] = (1ULL << bulletproofs_bits[m]) - 1;

for (n = 0; n < BULLETPROOFS_AGG_MAX_NUM; n++) {
bp_pp[testnum][m][n] = BP_PUB_PARAM_new_by_curve_id(test_bulletproofs_curves[testnum].nid,
Expand Down Expand Up @@ -4823,7 +4827,7 @@ int speed_main(int argc, char **argv)
}

bp_ctx[testnum][m][n][j] = BP_RANGE_CTX_new(bp_pp[testnum][m][n], bp_witness[testnum][m][n][j], bp_transcript[testnum][m][n]);
if (bp_ctx[testnum][m][n] == NULL)
if (bp_ctx[testnum][m][n][j] == NULL)
goto end;

if (!BP_RANGE_PROOF_prove(bp_ctx[testnum][m][n][j], bp_proof[testnum][m][n])) {
Expand Down
3 changes: 2 additions & 1 deletion crypto/ec/ec_elgamal_crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ EC_ELGAMAL_CTX *EC_ELGAMAL_CTX_new(EC_KEY *key, const EC_POINT *h, int32_t flag)
}
#endif

EC_KEY_up_ref(key);
if (!EC_KEY_up_ref(key))
goto err;
ctx->key = key;
ctx->flag = flag;

Expand Down
2 changes: 1 addition & 1 deletion crypto/encode_decode/encoder_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ static int encoder_process(struct encoder_process_data_st *data)
/* Preparations */

switch (ok) {
case 0:
default:
break;
case -1:
/*
Expand Down
12 changes: 9 additions & 3 deletions crypto/mem_sec.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,17 @@ int CRYPTO_secure_allocated(const void *ptr)

size_t CRYPTO_secure_used(void)
{
size_t ret = 0;

#ifndef OPENSSL_NO_SECURE_MEMORY
return secure_mem_used;
#else
return 0;
if (!CRYPTO_THREAD_read_lock(sec_malloc_lock))
return 0;

ret = secure_mem_used;

CRYPTO_THREAD_unlock(sec_malloc_lock);
#endif /* OPENSSL_NO_SECURE_MEMORY */
return ret;
}

size_t CRYPTO_secure_actual_size(void *ptr)
Expand Down
4 changes: 1 addition & 3 deletions crypto/provider_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,
if (!ossl_provider_up_ref(actualtmp)) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
actualtmp = NULL;
goto err;
return 0;
}
*actualprov = actualtmp;
}
Expand All @@ -663,8 +663,6 @@ int ossl_provider_add_to_store(OSSL_PROVIDER *prov, OSSL_PROVIDER **actualprov,

err:
CRYPTO_THREAD_unlock(store->lock);
if (actualprov != NULL)
ossl_provider_free(*actualprov);
return 0;
}

Expand Down
8 changes: 5 additions & 3 deletions crypto/x509/x509_lu.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,13 @@ int X509_STORE_copy(X509_STORE *dest, const X509_STORE *src)
for (i = 0; i < num; i++) {
obj = sk_X509_OBJECT_value(src->objs, i);
if (obj->type == X509_LU_X509) {
X509_STORE_add_cert(dest, obj->data.x509);
if (!X509_STORE_add_cert(dest, obj->data.x509))
return 0;
} else if (obj->type == X509_LU_CRL) {
X509_STORE_add_crl(dest, obj->data.crl);
if (!X509_STORE_add_crl(dest, obj->data.crl))
return 0;
} else {
/* abort(); */
return 0;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/zkp/bulletproofs/bulletproofs_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ BP_RANGE_PROOF *BP_RANGE_PROOF_decode(const unsigned char *in, size_t size)
proof->T2 = sk_EC_POINT_value(sk_point, 3);

sk_bn = zkp_stack_of_bignum_decode(p, &len, bn_len);
if (sk_point == NULL)
if (sk_bn == NULL)
goto err;
p += len;

Expand Down Expand Up @@ -1114,7 +1114,7 @@ BP_R1CS_PROOF *BP_R1CS_PROOF_decode(const unsigned char *in, size_t size)
#endif

sk_bn = zkp_stack_of_bignum_decode(p, &len, bn_len);
if (sk_point == NULL)
if (sk_bn == NULL)
goto err;
p += len;

Expand Down
3 changes: 2 additions & 1 deletion crypto/zkp/bulletproofs/inner_product.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ bp_inner_product_proof_t *bp_inner_product_proof_new(bp_inner_product_ctx_t *ctx

if (ctx == NULL || ctx->pp == NULL) {
ERR_raise(ERR_LIB_ZKP_BP, ERR_R_PASSED_NULL_PARAMETER);
return NULL;
}

n = sk_EC_POINT_num(ctx->pp->sk_G);
Expand Down Expand Up @@ -531,7 +532,7 @@ int bp_inner_product_proof_verify(bp_inner_product_ctx_t *ctx,
proof_num = sk_EC_POINT_num(proof->sk_L);
n = 2 * proof_num + 2 * pp_num + 1;

if (!(vec_x = OPENSSL_zalloc(proof_num * sizeof(*vec_x)))
if (proof_num < 0 || !(vec_x = OPENSSL_zalloc(proof_num * sizeof(*vec_x)))
|| !(vec_x_inv = OPENSSL_zalloc(proof_num * sizeof(*vec_x_inv)))) {
ERR_raise(ERR_LIB_ZKP_BP, ERR_R_MALLOC_FAILURE);
goto end;
Expand Down
3 changes: 2 additions & 1 deletion crypto/zkp/bulletproofs/r1cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,8 @@ int BP_R1CS_PROOF_verify(BP_R1CS_CTX *ctx, BP_R1CS_PROOF *proof)

v_n = sk_BP_VARIABLE_num(witness->sk_V);
lg_n = sk_EC_POINT_num(ip_proof->sk_L);
if (padded_n != 1 << lg_n) {
if (lg_n < 0 || (lg_n >= (int)sizeof(int) * 8)
|| ((unsigned int)padded_n != 1U << lg_n)) {
ERR_raise(ERR_LIB_ZKP_BP, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
Expand Down
Loading

0 comments on commit 9ab553c

Please sign in to comment.