Skip to content

Commit 4d8a6b8

Browse files
authored
Merge pull request #7760 from douzzer/20240718-BIO_DGRAM-memory-leak
20240718-BIO_DGRAM-memory-leak
2 parents 16a2d2e + 787397b commit 4d8a6b8

7 files changed

Lines changed: 183 additions & 146 deletions

File tree

src/bio.c

Lines changed: 137 additions & 117 deletions
Large diffs are not rendered by default.

src/ocsp.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -952,18 +952,18 @@ OcspResponse* wolfSSL_d2i_OCSP_RESPONSE_bio(WOLFSSL_BIO* bio,
952952
long fcur;
953953
long flen;
954954

955-
if (bio->ptr == NULL)
955+
if (bio->ptr.fh == NULL)
956956
return NULL;
957957

958-
fcur = XFTELL((XFILE)bio->ptr);
958+
fcur = XFTELL(bio->ptr.fh);
959959
if (fcur < 0)
960960
return NULL;
961-
if(XFSEEK((XFILE)bio->ptr, 0, SEEK_END) != 0)
961+
if(XFSEEK(bio->ptr.fh, 0, SEEK_END) != 0)
962962
return NULL;
963-
flen = XFTELL((XFILE)bio->ptr);
963+
flen = XFTELL(bio->ptr.fh);
964964
if (flen < 0)
965965
return NULL;
966-
if (XFSEEK((XFILE)bio->ptr, fcur, SEEK_SET) != 0)
966+
if (XFSEEK(bio->ptr.fh, fcur, SEEK_SET) != 0)
967967
return NULL;
968968

969969
/* check calculated length */

src/wolfio.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ int BioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx)
301301
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
302302
}
303303
#ifdef USE_WOLFSSL_IO
304-
recvd = TranslateIoReturnCode(recvd, ssl->biord->num, SOCKET_RECEIVING);
304+
recvd = TranslateIoReturnCode(recvd, ssl->biord->num.fd,
305+
SOCKET_RECEIVING);
305306
#endif
306307
return recvd;
307308
}
@@ -346,7 +347,8 @@ int BioSend(WOLFSSL* ssl, char *buf, int sz, void *ctx)
346347
if (sent <= 0) {
347348
if (ssl->biowr->type == WOLFSSL_BIO_SOCKET) {
348349
#ifdef USE_WOLFSSL_IO
349-
sent = TranslateIoReturnCode(sent, ssl->biowr->num, SOCKET_SENDING);
350+
sent = TranslateIoReturnCode(sent, ssl->biowr->num.fd,
351+
SOCKET_SENDING);
350352
#endif
351353
return sent;
352354
}

tests/api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,14 @@ static int testDevId = INVALID_DEVID;
601601
static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
602602
int len)
603603
{
604-
if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
604+
if ((bio == NULL) || (bio->ptr.mem_buf_data == NULL) || (data == NULL)) {
605605
len = 0;
606606
}
607607
else {
608608
if (bio->wrSz - bio->wrIdx < len) {
609609
len = bio->wrSz - bio->wrIdx;
610610
}
611-
XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
611+
XMEMCPY(bio->ptr.mem_buf_data + bio->wrIdx, data, len);
612612
bio->wrIdx += len;
613613
}
614614

@@ -617,14 +617,14 @@ static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
617617

618618
static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
619619
{
620-
if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
620+
if ((bio == NULL) || (bio->ptr.mem_buf_data == NULL) || (data == NULL)) {
621621
len = 0;
622622
}
623623
else {
624624
if (bio->wrSz - bio->rdIdx < len) {
625625
len = bio->wrSz - bio->rdIdx;
626626
}
627-
XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
627+
XMEMCPY(data, bio->ptr.mem_buf_data + bio->rdIdx, len);
628628
bio->rdIdx += len;
629629
}
630630

wolfssl/internal.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,7 +2753,16 @@ struct WOLFSSL_BIO {
27532753
WOLFSSL_BIO* next; /* next in chain */
27542754
WOLFSSL_BIO* pair; /* BIO paired with */
27552755
void* heap; /* user heap hint */
2756-
void* ptr; /* WOLFSSL, file descriptor, MD, or mem buf */
2756+
union {
2757+
byte* mem_buf_data;
2758+
#ifndef WOLFCRYPT_ONLY
2759+
WOLFSSL* ssl;
2760+
WOLFSSL_EVP_MD_CTX* md_ctx;
2761+
#endif
2762+
#ifndef NO_FILESYSTEM
2763+
XFILE fh;
2764+
#endif
2765+
} ptr;
27572766
void* usrCtx; /* user set pointer */
27582767
char* ip; /* IP address for wolfIO_TcpConnect */
27592768
word16 port; /* Port for wolfIO_TcpConnect */
@@ -2764,7 +2773,10 @@ struct WOLFSSL_BIO {
27642773
int wrIdx; /* current index for write buffer */
27652774
int rdIdx; /* current read index */
27662775
int readRq; /* read request */
2767-
int num; /* socket num or length */
2776+
union {
2777+
SOCKET_T fd;
2778+
size_t length;
2779+
} num;
27682780
int eof; /* eof flag */
27692781
int flags;
27702782
byte type; /* method type */

wolfssl/wolfcrypt/wc_port.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,25 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
859859
#define XSPRINTF sprintf
860860
#endif
861861

862+
#ifdef USE_WINDOWS_API
863+
#ifndef SOCKET_T
864+
#ifdef __MINGW64__
865+
typedef size_t SOCKET_T;
866+
#else
867+
typedef unsigned int SOCKET_T;
868+
#endif
869+
#endif
870+
#ifndef SOCKET_INVALID
871+
#define SOCKET_INVALID INVALID_SOCKET
872+
#endif
873+
#else
874+
#ifndef SOCKET_T
875+
typedef int SOCKET_T;
876+
#endif
877+
#ifndef SOCKET_INVALID
878+
#define SOCKET_INVALID (-1)
879+
#endif
880+
#endif
862881

863882
/* MIN/MAX MACRO SECTION */
864883
/* Windows API defines its own min() macro. */

wolfssl/wolfio.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -404,22 +404,6 @@
404404
#endif
405405
#endif
406406

407-
#ifdef USE_WINDOWS_API
408-
#if defined(__MINGW64__)
409-
typedef size_t SOCKET_T;
410-
#else
411-
typedef unsigned int SOCKET_T;
412-
#endif
413-
#ifndef SOCKET_INVALID
414-
#define SOCKET_INVALID INVALID_SOCKET
415-
#endif
416-
#else
417-
typedef int SOCKET_T;
418-
#ifndef SOCKET_INVALID
419-
#define SOCKET_INVALID (-1)
420-
#endif
421-
#endif
422-
423407
#ifndef WOLFSSL_NO_SOCK
424408
#ifndef XSOCKLENT
425409
#ifdef USE_WINDOWS_API

0 commit comments

Comments
 (0)