From ada06e218e4e2c961e30662fd4e00ed9e9365683 Mon Sep 17 00:00:00 2001 From: Davide Beatrici Date: Wed, 19 Dec 2018 20:00:18 +0100 Subject: [PATCH 1/2] Mayaqua: revamp RecvHttpHeader() so that cleanup functions are not called twice --- src/Mayaqua/Network.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/Mayaqua/Network.c b/src/Mayaqua/Network.c index 2f4bf0ce..a469d752 100644 --- a/src/Mayaqua/Network.c +++ b/src/Mayaqua/Network.c @@ -20563,13 +20563,15 @@ HTTP_HEADER *RecvHttpHeader(SOCK *s) // Split into tokens token = ParseToken(str, " "); - if (token->NumTokens < 3) - { - goto LABEL_ERROR; - } Free(str); + if (token->NumTokens < 3) + { + FreeToken(token); + return NULL; + } + // Creating a header object header = NewHttpHeader(token->Token[0], token->Token[1], token->Token[2]); FreeToken(token); @@ -20594,21 +20596,15 @@ HTTP_HEADER *RecvHttpHeader(SOCK *s) if (AddHttpValueStr(header, str) == false) { - goto LABEL_ERROR; + Free(str); + FreeHttpHeader(header); + break; } Free(str); } return header; - -LABEL_ERROR: - // Memory release - Free(str); - FreeToken(token); - FreeHttpHeader(header); - - return NULL; } // Receive a line From bd01cbff9a731d923f48a5f4c7cc6298309f78db Mon Sep 17 00:00:00 2001 From: Davide Beatrici Date: Thu, 20 Dec 2018 02:52:22 +0100 Subject: [PATCH 2/2] Mayaqua: add new FreeSafe() function which calls Free() and set the pointer's value to NULL --- src/Mayaqua/Memory.c | 7 +++++++ src/Mayaqua/Memory.h | 1 + src/Mayaqua/Network.c | 8 ++++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Mayaqua/Memory.c b/src/Mayaqua/Memory.c index 3bdc8ada..2658e83c 100644 --- a/src/Mayaqua/Memory.c +++ b/src/Mayaqua/Memory.c @@ -3777,6 +3777,13 @@ void Free(void *addr) InternalFree(tag); } +// Free and set pointer's value to NULL +void FreeSafe(void **addr) +{ + Free(*addr); + *addr = NULL; +} + // Check the memtag void CheckMemTag(MEMTAG *tag) { diff --git a/src/Mayaqua/Memory.h b/src/Mayaqua/Memory.h index ba2f1912..894a0e51 100644 --- a/src/Mayaqua/Memory.h +++ b/src/Mayaqua/Memory.h @@ -276,6 +276,7 @@ void *ZeroMalloc(UINT size); void *ZeroMallocEx(UINT size, bool zero_clear_when_free); void *ReAlloc(void *addr, UINT size); void Free(void *addr); +void FreeSafe(void **addr); void CheckMemTag(MEMTAG *tag); UINT GetMemSize(void *addr); diff --git a/src/Mayaqua/Network.c b/src/Mayaqua/Network.c index a469d752..4be28ec3 100644 --- a/src/Mayaqua/Network.c +++ b/src/Mayaqua/Network.c @@ -20564,7 +20564,7 @@ HTTP_HEADER *RecvHttpHeader(SOCK *s) // Split into tokens token = ParseToken(str, " "); - Free(str); + FreeSafe((void **)&str); if (token->NumTokens < 3) { @@ -20590,18 +20590,18 @@ HTTP_HEADER *RecvHttpHeader(SOCK *s) if (IsEmptyStr(str)) { // End of header - Free(str); + FreeSafe((void **)&str); break; } if (AddHttpValueStr(header, str) == false) { - Free(str); + FreeSafe((void **)&str); FreeHttpHeader(header); break; } - Free(str); + FreeSafe((void **)&str); } return header;