From 0218ba26c88b2197f6df07901f10436239ad8db0 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Fri, 27 Feb 2026 20:34:22 +0100 Subject: [PATCH] fix: fix memory leak and potential crash in iq_id_handler_add `userdata` was not freed if a handler could not be registered. This occurs when the 'id' parameter is NULL. In such cases, the handler cannot be stored in the 'id_handlers' table, but ownership of 'userdata' has already been transferred to this function. This commit ensures 'free_func' is called if 'id' is NULL since we always need to have an ID. --- src/xmpp/iq.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index b943286e..ebf7c1a7 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -364,14 +364,19 @@ _iq_id_handler_free(ProfIqHandler* handler) void iq_id_handler_add(const char* const id, ProfIqCallback func, ProfIqFreeCallback free_func, void* userdata) { - ProfIqHandler* handler = g_new0(ProfIqHandler, 1); - if (handler) { - handler->func = func; - handler->free_func = free_func; - handler->userdata = userdata; - - g_hash_table_insert(id_handlers, strdup(id), handler); + if (id == NULL) { + if (free_func) { + free_func(userdata); + } + return; } + + ProfIqHandler* handler = g_new0(ProfIqHandler, 1); + handler->func = func; + handler->free_func = free_func; + handler->userdata = userdata; + + g_hash_table_insert(id_handlers, strdup(id), handler); } void