Skip to content

Commit 21f34ef

Browse files
Merge pull request #6905 from bandi13/moreCodeSonarFixes
Don't nag about leaked resources
2 parents 15fdf6e + bb9031d commit 21f34ef

9 files changed

Lines changed: 43 additions & 8 deletions

File tree

examples/asn1/asn1.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ int main(int argc, char* argv[])
470470
if (ret != 0) {
471471
fprintf(stderr, "%s\n", wc_GetErrorString(ret));
472472
}
473+
474+
if (fp != stdin) {
475+
fclose(fp);
476+
}
473477
return (ret == 0) ? 0 : 1;
474478
}
475479

src/tls13.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4723,10 +4723,14 @@ static int EchCheckAcceptance(WOLFSSL* ssl, const byte* input,
47234723
int digestSize;
47244724
HS_Hashes* tmpHashes;
47254725
HS_Hashes* acceptHashes;
4726-
byte zeros[WC_MAX_DIGEST_SIZE] = {0};
4726+
byte zeros[WC_MAX_DIGEST_SIZE];
47274727
byte transcriptEchConf[WC_MAX_DIGEST_SIZE];
47284728
byte expandLabelPrk[WC_MAX_DIGEST_SIZE];
47294729
byte acceptConfirmation[ECH_ACCEPT_CONFIRMATION_SZ];
4730+
XMEMSET(zeros, 0, sizeof(zeros));
4731+
XMEMSET(transcriptEchConf, 0, sizeof(transcriptEchConf));
4732+
XMEMSET(expandLabelPrk, 0, sizeof(expandLabelPrk));
4733+
XMEMSET(acceptConfirmation, 0, sizeof(acceptConfirmation));
47304734
/* copy ech hashes to accept */
47314735
ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashesEch, &acceptHashes);
47324736
/* swap hsHashes to acceptHashes */
@@ -4839,9 +4843,12 @@ static int EchWriteAcceptance(WOLFSSL* ssl, byte* output,
48394843
int digestSize;
48404844
HS_Hashes* tmpHashes;
48414845
HS_Hashes* acceptHashes;
4842-
byte zeros[WC_MAX_DIGEST_SIZE] = {0};
4846+
byte zeros[WC_MAX_DIGEST_SIZE];
48434847
byte transcriptEchConf[WC_MAX_DIGEST_SIZE];
48444848
byte expandLabelPrk[WC_MAX_DIGEST_SIZE];
4849+
XMEMSET(zeros, 0, sizeof(zeros));
4850+
XMEMSET(transcriptEchConf, 0, sizeof(transcriptEchConf));
4851+
XMEMSET(expandLabelPrk, 0, sizeof(expandLabelPrk));
48454852

48464853
/* copy ech hashes to accept */
48474854
ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, &acceptHashes);
@@ -5710,7 +5717,7 @@ static void RefineSuites(WOLFSSL* ssl, Suites* peerSuites)
57105717
if (AllocateSuites(ssl) != 0)
57115718
return;
57125719

5713-
XMEMSET(suites, 0, WOLFSSL_MAX_SUITE_SZ);
5720+
XMEMSET(suites, 0, sizeof(suites));
57145721

57155722
if (!ssl->options.useClientOrder) {
57165723
/* Server order refining. */

src/x509.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14110,7 +14110,8 @@ int wolfSSL_X509_REQ_add1_attr_by_NID(WOLFSSL_X509 *req,
1411014110
}
1411114111
}
1411214112
ret = wolfSSL_sk_push(req->reqAttributes, attr);
14113-
if (ret != WOLFSSL_SUCCESS) {
14113+
if ((ret != WOLFSSL_SUCCESS) || (req->reqAttributes->type == STACK_TYPE_CIPHER)) {
14114+
/* CIPHER type makes a copy */
1411414115
wolfSSL_X509_ATTRIBUTE_free(attr);
1411514116
}
1411614117
}

tests/api.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16878,6 +16878,7 @@ static int test_wc_Chacha_SetKey(void)
1687816878
word32 keySz = (word32)(sizeof(key)/sizeof(byte));
1687916879
byte cipher[128];
1688016880

16881+
XMEMSET(cipher, 0, sizeof(cipher));
1688116882
ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
1688216883
/* Test bad args. */
1688316884
ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
@@ -54076,6 +54077,8 @@ static int test_wolfssl_EVP_chacha20(void)
5407654077
EVP_CIPHER_CTX* ctx = NULL;
5407754078
int outSz;
5407854079

54080+
XMEMSET(key, 0, sizeof(key));
54081+
XMEMSET(iv, 0, sizeof(iv));
5407954082
/* Encrypt. */
5408054083
ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
5408154084
ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,

tests/srp.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ static void test_SrpSetPassword(void)
208208
byte v[64];
209209
word32 vSz = 0;
210210

211+
XMEMSET(v, 0, sizeof(v));
211212
AssertIntEQ(0, wc_SrpInit(&srp, SRP_TYPE_SHA, SRP_CLIENT_SIDE));
212213
AssertIntEQ(0, wc_SrpSetUsername(&srp, username, usernameSz));
213214

@@ -262,6 +263,7 @@ static void test_SrpGetPublic(void)
262263
byte pub[64];
263264
word32 pubSz = 0;
264265

266+
XMEMSET(pub, 0, sizeof(pub));
265267
AssertIntEQ(0, wc_SrpInit(&srp, SRP_TYPE_SHA, SRP_CLIENT_SIDE));
266268
AssertIntEQ(0, wc_SrpSetUsername(&srp, username, usernameSz));
267269
AssertIntEQ(0, wc_SrpSetParams(&srp, srp_N, sizeof(srp_N),
@@ -318,6 +320,8 @@ static void test_SrpComputeKey(void)
318320
word32 clientPubKeySz = 64;
319321
word32 serverPubKeySz = 64;
320322

323+
XMEMSET(clientPubKey, 0, sizeof(clientPubKey));
324+
XMEMSET(serverPubKey, 0, sizeof(serverPubKey));
321325
AssertIntEQ(0, wc_SrpInit(&cli, SRP_TYPE_SHA, SRP_CLIENT_SIDE));
322326
AssertIntEQ(0, wc_SrpInit(&srv, SRP_TYPE_SHA, SRP_SERVER_SIDE));
323327

@@ -388,6 +392,10 @@ static void test_SrpGetProofAndVerify(void)
388392
word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
389393
word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
390394

395+
XMEMSET(clientPubKey, 0, sizeof(clientPubKey));
396+
XMEMSET(serverPubKey, 0, sizeof(serverPubKey));
397+
XMEMSET(clientProof, 0, sizeof(clientProof));
398+
XMEMSET(serverProof, 0, sizeof(serverProof));
391399
AssertIntEQ(0, wc_SrpInit(&cli, SRP_TYPE_SHA, SRP_CLIENT_SIDE));
392400
AssertIntEQ(0, wc_SrpInit(&srv, SRP_TYPE_SHA, SRP_SERVER_SIDE));
393401

@@ -792,6 +800,10 @@ static void test_SrpKeyGenFunc_cb(void)
792800
};
793801
#endif
794802

803+
XMEMSET(clientPubKey, 0, sizeof(clientPubKey));
804+
XMEMSET(serverPubKey, 0, sizeof(serverPubKey));
805+
XMEMSET(clientProof, 0, sizeof(clientProof));
806+
XMEMSET(serverProof, 0, sizeof(serverProof));
795807
AssertIntEQ(0, wc_SrpInit(&cli, SRP_TYPE_SHA512, SRP_CLIENT_SIDE));
796808
AssertIntEQ(0, wc_SrpInit(&srv, SRP_TYPE_SHA512, SRP_SERVER_SIDE));
797809

wolfcrypt/benchmark/benchmark.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4732,6 +4732,7 @@ void bench_chacha(void)
47324732
double start;
47334733
int i, count;
47344734

4735+
XMEMSET(&enc, 0, sizeof(enc));
47354736
wc_Chacha_SetKey(&enc, bench_key, 16);
47364737

47374738
bench_stats_start(&count, &start);

wolfcrypt/src/asn.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37523,8 +37523,10 @@ int wc_MIME_parse_headers(char* in, int inLen, MimeHdr** headers)
3752337523
}
3752437524
else if (mimeStatus == MIME_BODYVAL && cur == ';' && pos >= 1) {
3752537525
end = pos-1;
37526-
if (bodyVal != NULL)
37526+
if (bodyVal != NULL) {
3752737527
XFREE(bodyVal, NULL, DYNAMIC_TYPE_PKCS7);
37528+
bodyVal = NULL;
37529+
}
3752837530
ret = wc_MIME_header_strip(curLine, &bodyVal, start, end);
3752937531
if (ret) {
3753037532
goto error;
@@ -37617,9 +37619,12 @@ int wc_MIME_parse_headers(char* in, int inLen, MimeHdr** headers)
3761737619
if (ret != 0)
3761837620
wc_MIME_free_hdrs(curHdr);
3761937621
wc_MIME_free_hdrs(nextHdr);
37620-
XFREE(nameAttr, NULL, DYNAMIC_TYPE_PKCS7);
37621-
XFREE(bodyVal, NULL, DYNAMIC_TYPE_PKCS7);
37622-
XFREE(nextParam, NULL, DYNAMIC_TYPE_PKCS7);
37622+
if (nameAttr != NULL)
37623+
XFREE(nameAttr, NULL, DYNAMIC_TYPE_PKCS7);
37624+
if (bodyVal != NULL)
37625+
XFREE(bodyVal, NULL, DYNAMIC_TYPE_PKCS7);
37626+
if (nextParam != NULL)
37627+
XFREE(nextParam, NULL, DYNAMIC_TYPE_PKCS7);
3762337628

3762437629
return ret;
3762537630
}

wolfcrypt/src/hpke.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@ static int wc_HpkeContextOpenBase(Hpke* hpke, HpkeBaseContext* context,
11271127
return BAD_FUNC_ARG;
11281128
}
11291129

1130+
XMEMSET(nonce, 0, sizeof(nonce));
11301131
#ifdef WOLFSSL_SMALL_STACK
11311132
aes_key = (Aes*)XMALLOC(sizeof(Aes), hpke->heap, DYNAMIC_TYPE_AES);
11321133
if (aes_key == NULL) {

wolfcrypt/src/pkcs7.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8697,6 +8697,7 @@ static int wc_PKCS7_DecryptKtri(PKCS7* pkcs7, byte* in, word32 inSz,
86978697
mp_int serialNum[1];
86988698
RsaKey privKey[1];
86998699
#endif
8700+
XMEMSET(issuerHash, 0, sizeof(issuerHash));
87008701

87018702
#if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
87028703
keyIdSize = wc_HashGetDigestSize(wc_HashTypeConvert(HashIdAlg(

0 commit comments

Comments
 (0)