From 43e2788252173e6892f405fd2178f25eadfbccd3 Mon Sep 17 00:00:00 2001 From: Harald Reif Date: Tue, 28 May 2024 10:38:28 +0200 Subject: [PATCH 1/2] Fix possible memory leak In `MQTTAsync_receiveThread`, when `pack` is not `NULL` but none of the `if`-`else if`-conditions is fulfilled, `pack` is not freed. To fix this, an `else` block has been added to cleanup the memory of `pack`. Signed-off-by: Harald Reif --- src/MQTTAsyncUtils.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/MQTTAsyncUtils.c b/src/MQTTAsyncUtils.c index 29117fccc..4c32a2a3b 100644 --- a/src/MQTTAsyncUtils.c +++ b/src/MQTTAsyncUtils.c @@ -2320,6 +2320,11 @@ thread_return_type WINAPI MQTTAsync_receiveThread(void* n) m->c->connected = 0; /* don't send disconnect packet back */ nextOrClose(m, discrc, "Received disconnect"); } + else + { + free(pack); + pack = NULL; + } } } } From 6b1fee426e13cbbdd4339a0fc481f6a17ed2fdb4 Mon Sep 17 00:00:00 2001 From: Harald Reif Date: Tue, 7 Jan 2025 08:14:57 +0100 Subject: [PATCH 2/2] Replace free() with Log() As this else block should never be reached, a log message is created when it is reached Signed-off-by: Harald Reif --- src/MQTTAsyncUtils.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/MQTTAsyncUtils.c b/src/MQTTAsyncUtils.c index 4c32a2a3b..f8cd51e78 100644 --- a/src/MQTTAsyncUtils.c +++ b/src/MQTTAsyncUtils.c @@ -2322,8 +2322,7 @@ thread_return_type WINAPI MQTTAsync_receiveThread(void* n) } else { - free(pack); - pack = NULL; + Log(TRACE_MIN, -1, "An unexpected packet type %u has been received", pack->header.bits.type); } } }