From 27fb21e8f0011ee651762a19aa2a86c888e72ad7 Mon Sep 17 00:00:00 2001 From: Juergen Kosel Date: Tue, 3 Sep 2024 16:20:16 +0200 Subject: [PATCH 1/2] Release memory of pack->topic together with pack witin error cases of MQTTPacket_publish() As seen in https://github.com/eclipse/paho.mqtt.c/issues/1518 the memory allocated for pack-topic were not released in all cases. Signed-off-by: Juergen Kosel --- src/MQTTPacket.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/MQTTPacket.c b/src/MQTTPacket.c index e44fa5a0..e44fa2c7 100644 --- a/src/MQTTPacket.c +++ b/src/MQTTPacket.c @@ -575,6 +575,8 @@ void* MQTTPacket_publish(int MQTTVersion, unsigned char aHeader, char* data, siz { if (enddata - curdata < 2) /* Is there enough data for the msgid? */ { + free(pack->topic); + pack->topic = NULL; free(pack); pack = NULL; goto exit; @@ -589,6 +591,8 @@ void* MQTTPacket_publish(int MQTTVersion, unsigned char aHeader, char* data, siz pack->properties = props; if (MQTTProperties_read(&pack->properties, &curdata, enddata) != 1) { + free(pack->topic); + pack->topic = NULL; if (pack->properties.array) free(pack->properties.array); if (pack) From a2fd510110e6dc43aadaddb780a24e9485fbd9c8 Mon Sep 17 00:00:00 2001 From: Juergen Kosel Date: Wed, 4 Sep 2024 10:02:49 +0200 Subject: [PATCH 2/2] MQTTAsync_receiveThread() logs an error in case of unexpected and therefore unprocessed pack type Unprocessed pack lead to memory leak, as in https://github.com/eclipse/paho.mqtt.c/issues/1518 Signed-off-by: Juergen Kosel --- src/MQTTAsyncUtils.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/MQTTAsyncUtils.c b/src/MQTTAsyncUtils.c index 29117fcc..c6f07eaa 100644 --- a/src/MQTTAsyncUtils.c +++ b/src/MQTTAsyncUtils.c @@ -2320,6 +2320,10 @@ thread_return_type WINAPI MQTTAsync_receiveThread(void* n) m->c->connected = 0; /* don't send disconnect packet back */ nextOrClose(m, discrc, "Received disconnect"); } + else + { + Log(LOG_ERROR, -1, "Unexpected packet type %d", pack->header.bits.type); + } } } }