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.
This commit is contained in:
Michael Vetter
2026-02-27 20:34:22 +01:00
parent e15f659277
commit 0218ba26c8

View File

@@ -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