From eda98712d5c79246d08e518c83472ec70f3cf600 Mon Sep 17 00:00:00 2001 From: Lealem Amedie Date: Thu, 23 Jan 2025 16:14:45 -0700 Subject: [PATCH] Fix for NO_REALLOC build crash --- src/bio.c | 8 +++++--- src/conf.c | 2 +- src/internal.c | 2 +- src/pk.c | 2 +- src/ssl.c | 4 ++-- src/ssl_asn1.c | 2 +- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/bio.c b/src/bio.c index b6587eba1b..2433727317 100644 --- a/src/bio.c +++ b/src/bio.c @@ -2778,9 +2778,11 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio) #ifdef WOLFSSL_NO_REALLOC tmp = b->ip; b->ip = (char*)XMALLOC(newLen+1, b->heap, DYNAMIC_TYPE_OPENSSL); - XMEMCPY(b->ip, tmp, newLen); - XFREE(tmp, b->heap, DYNAMIC_TYPE_OPENSSL); - tmp = NULL; + if (b->ip != NULL && tmp != NULL) { + XMEMCPY(b->ip, tmp, newLen); + XFREE(tmp, b->heap, DYNAMIC_TYPE_OPENSSL); + tmp = NULL; + } #else b->ip = (char*)XREALLOC(b->ip, newLen + 1, b->heap, DYNAMIC_TYPE_OPENSSL); diff --git a/src/conf.c b/src/conf.c index a134f46c83..3413856963 100644 --- a/src/conf.c +++ b/src/conf.c @@ -776,7 +776,7 @@ static char* expandValue(WOLFSSL_CONF *conf, const char* section, #ifdef WOLFSSL_NO_REALLOC newRet = (char*)XMALLOC(strLen + 1, NULL, DYNAMIC_TYPE_OPENSSL); - if (newRet != NULL) { + if (newRet != NULL && ret != NULL) { XMEMCPY(newRet, ret, (strLen - valueLen) + 1); XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL); ret = NULL; diff --git a/src/internal.c b/src/internal.c index ad40aa2773..eb8479024f 100644 --- a/src/internal.c +++ b/src/internal.c @@ -39407,7 +39407,7 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx, #ifdef WOLFSSL_NO_REALLOC tmp = (byte*)XMALLOC(sizeof(InternalTicket), ssl->heap, DYNAMIC_TYPE_TLSX); - if (tmp != NULL) + if (tmp != NULL && psk->identity != NULL) { XMEMCPY(tmp, psk->identity, psk->identityLen); XFREE(psk->identity, ssl->heap, DYNAMIC_TYPE_TLSX); diff --git a/src/pk.c b/src/pk.c index 71723a87d8..bb383a4619 100644 --- a/src/pk.c +++ b/src/pk.c @@ -521,7 +521,7 @@ static int der_to_enc_pem_alloc(unsigned char* der, int derSz, #ifdef WOLFSSL_NO_REALLOC tmpBuf = (byte*)XMALLOC((size_t)(derSz + blockSz), heap, DYNAMIC_TYPE_TMP_BUFFER); - if (tmpBuf != NULL) + if (tmpBuf != NULL && der != NULL) { XMEMCPY(tmpBuf, der, (size_t)(derSz)); XFREE(der, heap, DYNAMIC_TYPE_TMP_BUFFER); diff --git a/src/ssl.c b/src/ssl.c index 2877132725..5e78e7a505 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -24809,7 +24809,7 @@ int wolfSSL_BUF_MEM_grow_ex(WOLFSSL_BUF_MEM* buf, size_t len, #ifdef WOLFSSL_NO_REALLOC tmp = (char*)XMALLOC(mx, NULL, DYNAMIC_TYPE_OPENSSL); - if (tmp != NULL) { + if (tmp != NULL && buf->data != NULL) { XMEMCPY(tmp, buf->data, len_int); XFREE(buf->data, NULL, DYNAMIC_TYPE_OPENSSL); buf->data = NULL; @@ -24862,7 +24862,7 @@ int wolfSSL_BUF_MEM_resize(WOLFSSL_BUF_MEM* buf, size_t len) /* We want to shrink the internal buffer */ #ifdef WOLFSSL_NO_REALLOC tmp = (char*)XMALLOC(mx, NULL, DYNAMIC_TYPE_OPENSSL); - if (tmp != NULL ) + if (tmp != NULL && buf->data != NULL) { XMEMCPY(tmp, buf->data, len); XFREE(buf->data,NULL,DYNAMIC_TYPE_OPENSSL); diff --git a/src/ssl_asn1.c b/src/ssl_asn1.c index 0a7a339290..29975f1a74 100644 --- a/src/ssl_asn1.c +++ b/src/ssl_asn1.c @@ -799,7 +799,7 @@ static int wolfssl_asn1_bit_string_grow(WOLFSSL_ASN1_BIT_STRING* bitStr, #ifdef WOLFSSL_NO_REALLOC tmp = (byte*)XMALLOC((size_t)len, NULL, DYNAMIC_TYPE_OPENSSL); - if (tmp != NULL) { + if (tmp != NULL && bitStr->data != NULL) { XMEMCPY(tmp, bitStr->data, bitStr->length); XFREE(bitStr->data, NULL, DYNAMIC_TYPE_OPENSSL); bitStr->data = NULL;