Skip to content

Commit

Permalink
Programmatically remove NULL test with { XFREE() }
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Fekete committed Aug 6, 2024
1 parent 208f2d6 commit d6a7187
Show file tree
Hide file tree
Showing 37 changed files with 175 additions and 529 deletions.
28 changes: 7 additions & 21 deletions IDE/Renesas/e2studio/RA6M4/test/src/wolfssl_sce_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,18 +691,10 @@ static int sce_rsa_test(int prnt, int keySize)
wc_FreeRsaKey(key);
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (in != NULL) {
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (in2 != NULL) {
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (out != NULL) {
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (out2 != NULL) {
XFREE(out2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(out2, NULL, DYNAMIC_TYPE_TMP_BUFFER);

return ret;
}
Expand Down Expand Up @@ -778,15 +770,9 @@ static int sce_rsa_SignVerify_test(int prnt, int keySize)
wc_FreeRsaKey(key);
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (in != NULL) {
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (in2 != NULL) {
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (out != NULL) {
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);

return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,15 +781,9 @@ static int tsip_rsa_SignVerify_test(int prnt, int keySize)
wc_FreeRsaKey(key);
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (in != NULL) {
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (in2 != NULL) {
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (out != NULL) {
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);

return ret;
}
Expand Down
28 changes: 7 additions & 21 deletions IDE/Renesas/e2studio/RZN2L/test/src/test/wolfssl_rsip_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -718,18 +718,10 @@ static int rsip_rsa_test(int prnt, int keySize)
wc_FreeRsaKey(key);
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (in != NULL) {
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (in2 != NULL) {
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (out != NULL) {
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (out2 != NULL) {
XFREE(out2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(out2, NULL, DYNAMIC_TYPE_TMP_BUFFER);

(void) prnt;
return ret;
Expand Down Expand Up @@ -808,15 +800,9 @@ static int rsip_rsa_SignVerify_test(int prnt, int keySize)
wc_FreeRsaKey(key);
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (in != NULL) {
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (in2 != NULL) {
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (out != NULL) {
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(in2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);

return ret;
}
Expand Down
8 changes: 2 additions & 6 deletions IDE/STM32Cube/wolfssl_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -1700,9 +1700,7 @@ static int tls13_uart_server(void)
}

#ifdef WOLFSSL_SMALL_STACK
if (tbuf != NULL) {
XFREE(tbuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(tbuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif

return ret;
Expand Down Expand Up @@ -1795,9 +1793,7 @@ static int tls13_uart_client(void)
wolfSSL_CTX_free(ctx);
}
#ifdef WOLFSSL_SMALL_STACK
if (tbuf != NULL) {
XFREE(tbuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(tbuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif

return ret;
Expand Down
8 changes: 2 additions & 6 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -13952,9 +13952,7 @@ int LoadCertByIssuer(WOLFSSL_X509_STORE* store, X509_NAME* issuer, int type)
/* / <hashvalue>.(r)N\0 */
/*|1| 8 |1|1|1|1| => 13 */
len = (int)XSTRLEN(entry->dir_name) + 13;
if (filename != NULL) {
XFREE(filename, NULL, DYNAMIC_TYPE_OPENSSL);
}
XFREE(filename, NULL, DYNAMIC_TYPE_OPENSSL);

filename = (char*)XMALLOC(len, NULL, DYNAMIC_TYPE_OPENSSL);
if (filename == NULL) {
Expand Down Expand Up @@ -41182,9 +41180,7 @@ void wolfSSL_BY_DIR_entry_free(WOLFSSL_BY_DIR_entry* entry)
wolfSSL_sk_BY_DIR_HASH_free(entry->hashes);
}

if (entry->dir_name != NULL) {
XFREE(entry->dir_name, NULL, DYNAMIC_TYPE_OPENSSL);
}
XFREE(entry->dir_name, NULL, DYNAMIC_TYPE_OPENSSL);

XFREE(entry, NULL, DYNAMIC_TYPE_OPENSSL);
}
Expand Down
4 changes: 1 addition & 3 deletions src/pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -7799,9 +7799,7 @@ static int wolfssl_dhparams_to_der(WOLFSSL_DH* dh, unsigned char** out,
*out = der;
der = NULL;
}
if (der != NULL) {
XFREE(der, heap, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(der, heap, DYNAMIC_TYPE_TMP_BUFFER);

return ret;
}
Expand Down
4 changes: 1 addition & 3 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,7 @@ WOLFSSL_CTX* wolfSSL_CTX_new_ex(WOLFSSL_METHOD* method, void* heap)
if (ret != WOLFSSL_SUCCESS) {
WOLFSSL_MSG("wolfSSL_Init failed");
WOLFSSL_LEAVE("wolfSSL_CTX_new_ex", 0);
if (method != NULL) {
XFREE(method, heap, DYNAMIC_TYPE_METHOD);
}
XFREE(method, heap, DYNAMIC_TYPE_METHOD);
return NULL;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/ssl_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -3002,9 +3002,7 @@ int wolfSSL_ASN1_STRING_print_ex(WOLFSSL_BIO *bio, WOLFSSL_ASN1_STRING *str,
void wolfSSL_ASN1_GENERALIZEDTIME_free(WOLFSSL_ASN1_TIME* asn1Time)
{
WOLFSSL_ENTER("wolfSSL_ASN1_GENERALIZEDTIME_free");
if (asn1Time != NULL) {
XFREE(asn1Time, NULL, DYNAMIC_TYPE_OPENSSL);
}
XFREE(asn1Time, NULL, DYNAMIC_TYPE_OPENSSL);
}

#ifndef NO_BIO
Expand Down
52 changes: 13 additions & 39 deletions src/ssl_p7p12.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,7 @@ static int wolfSSL_BIO_to_MIME_crlf(WOLFSSL_BIO* in, WOLFSSL_BIO* out)
}
}

if (canonLine != NULL) {
XFREE(canonLine, NULL, DYNAMIC_TYPE_PKCS7);
}
XFREE(canonLine, NULL, DYNAMIC_TYPE_PKCS7);
#ifdef WOLFSSL_SMALL_STACK
XFREE(line, in->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
Expand Down Expand Up @@ -744,9 +742,7 @@ int wolfSSL_PKCS7_final(PKCS7* pkcs7, WOLFSSL_BIO* in, int flags)
}

if (ret == 1) {
if (p7->data != NULL) {
XFREE(p7->data, NULL, DYNAMIC_TYPE_PKCS7);
}
XFREE(p7->data, NULL, DYNAMIC_TYPE_PKCS7);
p7->data = (byte*)XMALLOC(memSz, NULL, DYNAMIC_TYPE_PKCS7);
if (p7->data == NULL) {
ret = 0;
Expand Down Expand Up @@ -1603,12 +1599,8 @@ int wolfSSL_SMIME_write_PKCS7(WOLFSSL_BIO* out, PKCS7* pkcs7, WOLFSSL_BIO* in,
}
}

if (p7out != NULL) {
XFREE(p7out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
if (sigBase64 != NULL) {
XFREE(sigBase64, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(p7out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(sigBase64, NULL, DYNAMIC_TYPE_TMP_BUFFER);

if (ret > 0) {
return WOLFSSL_SUCCESS;
Expand Down Expand Up @@ -1736,9 +1728,7 @@ int wolfSSL_i2d_PKCS12_bio(WOLFSSL_BIO *bio, WC_PKCS12 *pkcs12)
}
}

if (certDer != NULL) {
XFREE(certDer, NULL, DYNAMIC_TYPE_PKCS);
}
XFREE(certDer, NULL, DYNAMIC_TYPE_PKCS);
}

return ret;
Expand Down Expand Up @@ -1900,12 +1890,8 @@ int wolfSSL_PKCS12_parse(WC_PKCS12* pkcs12, const char* psw,
*ca = (WOLF_STACK_OF(WOLFSSL_X509)*)XMALLOC(
sizeof(WOLF_STACK_OF(WOLFSSL_X509)), heap, DYNAMIC_TYPE_X509);
if (*ca == NULL) {
if (pk != NULL) {
XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
}
if (certData != NULL) {
XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
}
XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
/* Free up WC_DerCertList and move on */
while (current != NULL) {
WC_DerCertList* next = current->next;
Expand Down Expand Up @@ -1939,12 +1925,8 @@ int wolfSSL_PKCS12_parse(WC_PKCS12* pkcs12, const char* psw,
FreeDecodedCert(DeCert);
wolfSSL_X509_free(x509);
wolfSSL_sk_X509_pop_free(*ca, NULL); *ca = NULL;
if (pk != NULL) {
XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
}
if (certData != NULL) {
XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
}
XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
/* Free up WC_DerCertList */
while (current != NULL) {
WC_DerCertList* next = current->next;
Expand All @@ -1962,12 +1944,8 @@ int wolfSSL_PKCS12_parse(WC_PKCS12* pkcs12, const char* psw,
WOLFSSL_MSG("Failed to push x509 onto stack");
wolfSSL_X509_free(x509);
wolfSSL_sk_X509_pop_free(*ca, NULL); *ca = NULL;
if (pk != NULL) {
XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
}
if (certData != NULL) {
XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
}
XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
XFREE(certData, heap, DYNAMIC_TYPE_PKCS);

/* Free up WC_DerCertList */
while (current != NULL) {
Expand All @@ -1993,9 +1971,7 @@ int wolfSSL_PKCS12_parse(WC_PKCS12* pkcs12, const char* psw,
*cert = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), heap,
DYNAMIC_TYPE_X509);
if (*cert == NULL) {
if (pk != NULL) {
XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
}
XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
if (ca != NULL) {
wolfSSL_sk_X509_pop_free(*ca, NULL); *ca = NULL;
}
Expand All @@ -2011,9 +1987,7 @@ int wolfSSL_PKCS12_parse(WC_PKCS12* pkcs12, const char* psw,
if (CopyDecodedToX509(*cert, DeCert) != 0) {
WOLFSSL_MSG("Failed to copy decoded cert");
FreeDecodedCert(DeCert);
if (pk != NULL) {
XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
}
XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
if (ca != NULL) {
wolfSSL_sk_X509_pop_free(*ca, NULL); *ca = NULL;
}
Expand Down
4 changes: 1 addition & 3 deletions src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -5974,9 +5974,7 @@ static TlsxSrtp* TLSX_UseSRTP_New(word16 ids, void* heap)

static void TLSX_UseSRTP_Free(TlsxSrtp *srtp, void* heap)
{
if (srtp != NULL) {
XFREE(srtp, heap, DYNAMIC_TYPE_TLSX);
}
XFREE(srtp, heap, DYNAMIC_TYPE_TLSX);
(void)heap;
}

Expand Down
8 changes: 2 additions & 6 deletions src/x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -6573,9 +6573,7 @@ static int X509PrintSignature(WOLFSSL_BIO* bio, WOLFSSL_X509* x509,
return WOLFSSL_FAILURE;
}

if (sig != NULL) {
XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);

}

Expand Down Expand Up @@ -8331,9 +8329,7 @@ static int X509CRLPrintSignature(WOLFSSL_BIO* bio, WOLFSSL_X509_CRL* crl,
return WOLFSSL_FAILURE;
}

if (sig != NULL) {
XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);

}

Expand Down
4 changes: 1 addition & 3 deletions wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -13694,9 +13694,7 @@ static WARN_UNUSED_RESULT int S2V(
}
}
#ifdef WOLFSSL_SMALL_STACK
if (cmac != NULL) {
XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
}
XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
wc_MemZero_Check(cmac, sizeof(Cmac));
#endif
Expand Down
16 changes: 4 additions & 12 deletions wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -4025,9 +4025,7 @@ int wc_BerToDer(const byte* ber, word32 berSz, byte* der, word32* derSz)
}
end:
#ifdef WOLFSSL_SMALL_STACK
if (indefItems != NULL) {
XFREE(indefItems, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(indefItems, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret;
}
Expand Down Expand Up @@ -8616,9 +8614,7 @@ int wc_EncryptPKCS8Key(byte* key, word32 keySz, byte* out, word32* outSz,
}

#ifdef WOLFSSL_SMALL_STACK
if (saltTmp != NULL) {
XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif

WOLFSSL_LEAVE("wc_EncryptPKCS8Key", ret);
Expand Down Expand Up @@ -24321,13 +24317,9 @@ void FreeTrustedPeer(TrustedPeerCert* tp, void* heap)
return;
}

if (tp->name) {
XFREE(tp->name, heap, DYNAMIC_TYPE_SUBJECT_CN);
}
XFREE(tp->name, heap, DYNAMIC_TYPE_SUBJECT_CN);

if (tp->sig) {
XFREE(tp->sig, heap, DYNAMIC_TYPE_SIGNATURE);
}
XFREE(tp->sig, heap, DYNAMIC_TYPE_SIGNATURE);
#ifndef IGNORE_NAME_CONSTRAINTS
if (tp->permittedNames)
FreeNameSubtrees(tp->permittedNames, heap);
Expand Down
8 changes: 2 additions & 6 deletions wolfcrypt/src/cmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,7 @@ int wc_AesCmacGenerate(byte* out, word32* outSz,


#ifdef WOLFSSL_SMALL_STACK
if (cmac) {
XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
}
XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
wc_MemZero_Check(cmac, sizeof(Cmac));
#endif
Expand Down Expand Up @@ -495,9 +493,7 @@ int wc_AesCmacVerify(const byte* check, word32 checkSz,
INVALID_DEVID);

#ifdef WOLFSSL_SMALL_STACK
if (cmac) {
XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
}
XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
wc_MemZero_Check(cmac, sizeof(Cmac));
#endif
Expand Down
Loading

0 comments on commit d6a7187

Please sign in to comment.