Skip to content

Commit

Permalink
remove NULL test with XFREE arguments with dereference
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Fekete committed Aug 6, 2024
1 parent d6a7187 commit d350ba6
Show file tree
Hide file tree
Showing 28 changed files with 122 additions and 292 deletions.
8 changes: 2 additions & 6 deletions src/bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,7 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len)
(const char*)data, len, 0, ret);
}

if (frmt != NULL) {
XFREE(frmt, front->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(frmt, front->heap, DYNAMIC_TYPE_TMP_BUFFER);

#ifdef WOLFSSL_BASE64_ENCODE
if (retB64 > 0 && ret > 0)
Expand Down Expand Up @@ -2969,9 +2967,7 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio)
bio->pair->pair = NULL;
}

if (bio->ip != NULL) {
XFREE(bio->ip, bio->heap, DYNAMIC_TYPE_OPENSSL);
}
XFREE(bio->ip, bio->heap, DYNAMIC_TYPE_OPENSSL);

if (bio->shutdown) {
if (bio->type == WOLFSSL_BIO_SSL && bio->ptr.ssl)
Expand Down
4 changes: 1 addition & 3 deletions src/dtls13.c
Original file line number Diff line number Diff line change
Expand Up @@ -811,9 +811,7 @@ static void Dtls13MaybeSaveClientHello(WOLFSSL* ssl)
while (r != NULL) {
if (r->handshakeType == client_hello) {
Dtls13RtxRecordUnlink(ssl, prev_next, r);
if (ssl->dtls13ClientHello != NULL)
XFREE(ssl->dtls13ClientHello, ssl->heap,
DYNAMIC_TYPE_DTLS_MSG);
XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG);
ssl->dtls13ClientHello = r->data;
ssl->dtls13ClientHelloSz = r->length;
r->data = NULL;
Expand Down
36 changes: 11 additions & 25 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4564,9 +4564,7 @@ void FreeX509(WOLFSSL_X509* x509)
XFREE(x509->sig.buffer, x509->heap, DYNAMIC_TYPE_SIGNATURE);
x509->sig.buffer = NULL;
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
if (x509->authKeyIdSrc != NULL) {
XFREE(x509->authKeyIdSrc, x509->heap, DYNAMIC_TYPE_X509_EXT);
}
XFREE(x509->authKeyIdSrc, x509->heap, DYNAMIC_TYPE_X509_EXT);
else {
XFREE(x509->authKeyId, x509->heap, DYNAMIC_TYPE_X509_EXT);
}
Expand All @@ -4588,9 +4586,7 @@ void FreeX509(WOLFSSL_X509* x509)
}
#if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
defined(WOLFSSL_QT)
if (x509->authInfoCaIssuer != NULL) {
XFREE(x509->authInfoCaIssuer, x509->heap, DYNAMIC_TYPE_X509_EXT);
}
XFREE(x509->authInfoCaIssuer, x509->heap, DYNAMIC_TYPE_X509_EXT);
if (x509->ext_sk != NULL) {
wolfSSL_sk_X509_EXTENSION_pop_free(x509->ext_sk, NULL);
}
Expand Down Expand Up @@ -8375,9 +8371,7 @@ void SSL_ResourceFree(WOLFSSL* ssl)
}
#endif
#ifdef OPENSSL_EXTRA
if (ssl->param) {
XFREE(ssl->param, ssl->heap, DYNAMIC_TYPE_OPENSSL);
}
XFREE(ssl->param, ssl->heap, DYNAMIC_TYPE_OPENSSL);
#endif
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
while (ssl->certReqCtx != NULL) {
Expand Down Expand Up @@ -13261,12 +13255,9 @@ static int ProcessCSR(WOLFSSL* ssl, byte* input, word32* inOutIdx,
DYNAMIC_TYPE_OCSP_REQUEST);

if (status == NULL || single == NULL || response == NULL) {
if (status)
XFREE(status, ssl->heap, DYNAMIC_TYPE_OCSP_STATUS);
if (single)
XFREE(single, ssl->heap, DYNAMIC_TYPE_OCSP_ENTRY);
if (response)
XFREE(response, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
XFREE(status, ssl->heap, DYNAMIC_TYPE_OCSP_STATUS);
XFREE(single, ssl->heap, DYNAMIC_TYPE_OCSP_ENTRY);
XFREE(response, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);

return MEMORY_ERROR;
}
Expand Down Expand Up @@ -15033,8 +15024,7 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx,
if (dCertAdd_inited)
FreeDecodedCert(dCertAdd);
#ifdef WOLFSSL_SMALL_STACK
if (dCertAdd)
XFREE(dCertAdd, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(dCertAdd, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
if (ret != 0)
goto exit_ppc;
Expand Down Expand Up @@ -16222,12 +16212,9 @@ static int DoCertificateStatus(WOLFSSL* ssl, byte* input, word32* inOutIdx,
DYNAMIC_TYPE_OCSP_REQUEST);

if (status == NULL || single == NULL || response == NULL) {
if (status)
XFREE(status, ssl->heap, DYNAMIC_TYPE_OCSP_STATUS);
if (single)
XFREE(single, ssl->heap, DYNAMIC_TYPE_OCSP_ENTRY);
if (response)
XFREE(response, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
XFREE(status, ssl->heap, DYNAMIC_TYPE_OCSP_STATUS);
XFREE(single, ssl->heap, DYNAMIC_TYPE_OCSP_ENTRY);
XFREE(response, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);

return MEMORY_ERROR;
}
Expand Down Expand Up @@ -37152,8 +37139,7 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
ssl->clSuites = NULL;
#endif
#ifdef WOLFSSL_SMALL_STACK
if (clSuites != NULL)
XFREE(clSuites, ssl->heap, DYNAMIC_TYPE_SUITES);
XFREE(clSuites, ssl->heap, DYNAMIC_TYPE_SUITES);
#endif
WOLFSSL_LEAVE("DoClientHello", ret);
WOLFSSL_END(WC_FUNC_CLIENT_HELLO_DO);
Expand Down
14 changes: 4 additions & 10 deletions src/ocsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,7 @@ int CheckOcspResponse(WOLFSSL_OCSP *ocsp, byte *response, int responseSz,
}

if (status != NULL) {
if (status->rawOcspResponse) {
XFREE(status->rawOcspResponse, ocsp->cm->heap,
DYNAMIC_TYPE_OCSP_STATUS);
}
XFREE(status->rawOcspResponse, ocsp->cm->heap, DYNAMIC_TYPE_OCSP_STATUS);

/* Replace existing certificate entry with updated */
ocspResponse->single->status->next = status->next;
Expand Down Expand Up @@ -831,13 +828,11 @@ WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_cert_to_id(
XFREE(certId, cm->heap, DYNAMIC_TYPE_OPENSSL);
certId = NULL;
}
if (certStatus)
XFREE(certStatus, cm->heap, DYNAMIC_TYPE_OPENSSL);
XFREE(certStatus, cm->heap, DYNAMIC_TYPE_OPENSSL);
}

#ifdef WOLFSSL_SMALL_STACK
if (cert != NULL)
XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
#endif

if (cm != NULL)
Expand Down Expand Up @@ -1167,8 +1162,7 @@ WOLFSSL_OCSP_ONEREQ* wolfSSL_OCSP_request_add0_id(OcspRequest *req,
XMEMCPY(req->issuerHash, cid->issuerHash, KEYID_SIZE);
XMEMCPY(req->issuerKeyHash, cid->issuerKeyHash, KEYID_SIZE);
if (cid->status->serialSz > req->serialSz) {
if (req->serial != NULL)
XFREE(req->serial, req->heap, DYNAMIC_TYPE_OCSP);
XFREE(req->serial, req->heap, DYNAMIC_TYPE_OCSP);
req->serial = (byte*)XMALLOC((size_t)cid->status->serialSz,
req->heap, DYNAMIC_TYPE_OCSP_REQUEST);
if (req->serial == NULL)
Expand Down
3 changes: 1 addition & 2 deletions src/pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -4691,8 +4691,7 @@ int wolfSSL_RSA_GenAdd(WOLFSSL_RSA* rsa)
mp_clear(t);

#ifdef WOLFSSL_SMALL_STACK
if (tmp != NULL)
XFREE(tmp, rsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmp, rsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif

return ret;
Expand Down
7 changes: 2 additions & 5 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -11559,8 +11559,7 @@ int wolfSSL_set_compression(WOLFSSL* ssl)
wc_FreeRng(&rng);
} else {
/* save password for wolfSSL_set_srp_username */
if (ctx->srp_password != NULL)
XFREE(ctx->srp_password,ctx->heap, DYNAMIC_TYPE_SRP);
XFREE(ctx->srp_password, ctx->heap, DYNAMIC_TYPE_SRP);

ctx->srp_password = (byte*)XMALLOC(XSTRLEN(password) + 1, ctx->heap,
DYNAMIC_TYPE_SRP);
Expand Down Expand Up @@ -13572,9 +13571,7 @@ WOLFSSL_STACK* wolfSSL_sk_new_node(void* heap)
/* free's node but does not free internal data such as in->data.x509 */
void wolfSSL_sk_free_node(WOLFSSL_STACK* in)
{
if (in != NULL) {
XFREE(in, in->heap, DYNAMIC_TYPE_OPENSSL);
}
XFREE(in, in->heap, DYNAMIC_TYPE_OPENSSL);
}

/* pushes node "in" onto "stack" and returns pointer to the new stack on success
Expand Down
4 changes: 1 addition & 3 deletions src/ssl_certman.c
Original file line number Diff line number Diff line change
Expand Up @@ -1382,9 +1382,7 @@ int CM_SaveCertCache(WOLFSSL_CERT_MANAGER* cm, const char* fname)
ret = FWRITE_ERROR;
}
}
if (mem != NULL) {
XFREE(mem, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(mem, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);

/* Unlock CA table. */
wc_UnLockMutex(&cm->caLock);
Expand Down
23 changes: 6 additions & 17 deletions src/ssl_p7p12.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,7 @@ int wolfSSL_PKCS7_final(PKCS7* pkcs7, WOLFSSL_BIO* in, int flags)
}
}

if (mem != NULL) {
XFREE(mem, in->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(mem, in->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
else {
#ifdef HAVE_SMIME
Expand Down Expand Up @@ -1034,19 +1032,11 @@ int wolfSSL_PEM_write_bio_PKCS7(WOLFSSL_BIO* bio, PKCS7* p7)

error:
#ifdef WOLFSSL_SMALL_STACK
if (outputHead) {
XFREE(outputHead, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
if (outputFoot) {
XFREE(outputFoot, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(outputHead, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(outputFoot, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
if (output) {
XFREE(output, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
if (pem) {
XFREE(pem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(output, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(pem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
return WOLFSSL_FAILURE;
}

Expand Down Expand Up @@ -1692,8 +1682,7 @@ WC_PKCS12* wolfSSL_d2i_PKCS12_bio(WOLFSSL_BIO* bio, WC_PKCS12** pkcs12)
}

/* cleanup */
if (mem != NULL)
XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
if (ret < 0 && localPkcs12 != NULL) {
wc_PKCS12_free(localPkcs12);
localPkcs12 = NULL;
Expand Down
15 changes: 5 additions & 10 deletions src/ssl_sess.c
Original file line number Diff line number Diff line change
Expand Up @@ -1309,8 +1309,7 @@ int wolfSSL_GetSessionFromCache(WOLFSSL* ssl, WOLFSSL_SESSION* output)
output->ticketLenAlloc = 0;
}
#ifdef WOLFSSL_SMALL_STACK
if (tmpTicket != NULL)
XFREE(tmpTicket, output->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmpTicket, output->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return WOLFSSL_FAILURE;
}
Expand Down Expand Up @@ -1417,8 +1416,7 @@ int wolfSSL_GetSessionFromCache(WOLFSSL* ssl, WOLFSSL_SESSION* output)
}
}
#ifdef WOLFSSL_SMALL_STACK
if (tmpTicket != NULL)
XFREE(tmpTicket, output->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmpTicket, output->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif

#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_TICKET_NONCE_MALLOC) && \
Expand Down Expand Up @@ -1455,8 +1453,7 @@ int wolfSSL_GetSessionFromCache(WOLFSSL* ssl, WOLFSSL_SESSION* output)
preallocNonce = NULL;
}
}
if (preallocNonce != NULL)
XFREE(preallocNonce, output->heap, DYNAMIC_TYPE_SESSION_TICK);
XFREE(preallocNonce, output->heap, DYNAMIC_TYPE_SESSION_TICK);
#endif /* WOLFSSL_TLS13 && WOLFSSL_TICKET_NONCE_MALLOC && FIPS_VERSION_GE(5,3)*/

#endif
Expand Down Expand Up @@ -1839,8 +1836,7 @@ int AddSessionToCache(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* addSession,
preallocNonce = (byte*)XMALLOC(addSession->ticketNonce.len,
addSession->heap, DYNAMIC_TYPE_SESSION_TICK);
if (preallocNonce == NULL) {
if (ticBuff != NULL)
XFREE(ticBuff, addSession->heap, DYNAMIC_TYPE_SESSION_TICK);
XFREE(ticBuff, addSession->heap, DYNAMIC_TYPE_SESSION_TICK);
return MEMORY_E;
}
preallocNonceLen = addSession->ticketNonce.len;
Expand Down Expand Up @@ -3906,8 +3902,7 @@ static int wolfSSL_DupSessionEx(const WOLFSSL_SESSION* input,
}
}
else {
if (ticBuff != NULL)
XFREE(ticBuff, output->heap, DYNAMIC_TYPE_SESSION_TICK);
XFREE(ticBuff, output->heap, DYNAMIC_TYPE_SESSION_TICK);
output->ticket = output->staticTicket;
output->ticketLenAlloc = 0;
}
Expand Down
27 changes: 8 additions & 19 deletions src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -6744,8 +6744,7 @@ int TLSX_Cookie_Use(const WOLFSSL* ssl, const byte* data, word16 len, byte* mac,
if (mac != NULL)
XMEMCPY(cookie->data + len, mac, macSz);

if (extension->data != NULL)
XFREE(extension->data, ssl->heap, DYNAMIC_TYPE_TLSX);
XFREE(extension->data, ssl->heap, DYNAMIC_TYPE_TLSX);

extension->data = (void*)cookie;
extension->resp = (byte)resp;
Expand Down Expand Up @@ -7953,10 +7952,8 @@ static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)

wc_KyberKey_Free(kem);
TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
if (pubKey != NULL)
XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
if (privKey != NULL)
XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);

return ret;
}
Expand Down Expand Up @@ -8731,9 +8728,7 @@ static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
ssl->arrays->preMasterSz = (word32) sharedSecretLen;
}

if (sharedSecret != NULL) {
XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
}
XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);

wc_ecc_free(&eccpubkey);
wc_KyberKey_Free(kem);
Expand Down Expand Up @@ -9220,9 +9215,7 @@ static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
}

if (ret == 0) {
if (keyShareEntry->ke != NULL) {
XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
}
XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);

keyShareEntry->ke = sharedSecret;
keyShareEntry->keLen = outlen + ssSz;
Expand All @@ -9240,10 +9233,8 @@ static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
}

TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
if (sharedSecret != NULL)
XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
if (ciphertext != NULL)
XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
wc_ecc_free(&eccpubkey);
wc_KyberKey_Free(kem);
return ret;
Expand Down Expand Up @@ -9308,9 +9299,7 @@ int TLSX_KeyShare_Use(const WOLFSSL* ssl, word16 group, word16 len, byte* data,
else
#endif
if (data != NULL) {
if (keyShareEntry->ke != NULL) {
XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
}
XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
keyShareEntry->ke = data;
keyShareEntry->keLen = len;
}
Expand Down
Loading

0 comments on commit d350ba6

Please sign in to comment.