From 66e3b0e578320947f618df92dc17976099c83664 Mon Sep 17 00:00:00 2001 From: Ailin Nemui Date: Wed, 18 May 2022 09:29:27 +0200 Subject: [PATCH] avoid memory leak in ignore cache #1373 --- src/common.h | 2 +- src/core/ignore.c | 7 ++++++- src/core/nickmatch-cache.c | 12 +++++++----- src/core/nickmatch-cache.h | 3 ++- src/fe-common/core/hilight-text.c | 2 +- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/common.h b/src/common.h index a6dfd3b9..1d4fd207 100644 --- a/src/common.h +++ b/src/common.h @@ -6,7 +6,7 @@ #define IRSSI_GLOBAL_CONFIG "irssi.conf" /* config file name in /etc/ */ #define IRSSI_HOME_CONFIG "config" /* config file name in ~/.irssi/ */ -#define IRSSI_ABI_VERSION 48 +#define IRSSI_ABI_VERSION 49 #define DEFAULT_SERVER_ADD_PORT 6667 #define DEFAULT_SERVER_ADD_TLS_PORT 6697 diff --git a/src/core/ignore.c b/src/core/ignore.c index 0bc12ed3..da892c1e 100644 --- a/src/core/ignore.c +++ b/src/core/ignore.c @@ -490,6 +490,11 @@ static void read_ignores(void) nickmatch_rebuild(nickmatch); } +static void free_cache_matches(GSList *matches) +{ + g_slist_free(matches); +} + static void ignore_nick_cache(GHashTable *list, CHANNEL_REC *channel, NICK_REC *nick) { @@ -520,7 +525,7 @@ static void ignore_nick_cache(GHashTable *list, CHANNEL_REC *channel, void ignore_init(void) { ignores = NULL; - nickmatch = nickmatch_init(ignore_nick_cache); + nickmatch = nickmatch_init(ignore_nick_cache, (GDestroyNotify) free_cache_matches); time_tag = g_timeout_add(1000, (GSourceFunc) unignore_timeout, NULL); read_ignores(); diff --git a/src/core/nickmatch-cache.c b/src/core/nickmatch-cache.c index cae6620d..c8fc1157 100644 --- a/src/core/nickmatch-cache.c +++ b/src/core/nickmatch-cache.c @@ -28,12 +28,13 @@ static GSList *lists; -NICKMATCH_REC *nickmatch_init(NICKMATCH_REBUILD_FUNC func) +NICKMATCH_REC *nickmatch_init(NICKMATCH_REBUILD_FUNC func, GDestroyNotify value_destroy_func) { NICKMATCH_REC *rec; rec = g_new0(NICKMATCH_REC, 1); rec->func = func; + rec->value_destroy_func = value_destroy_func; lists = g_slist_append(lists, rec); return rec; @@ -43,8 +44,9 @@ void nickmatch_deinit(NICKMATCH_REC *rec) { lists = g_slist_remove(lists, rec); - g_hash_table_destroy(rec->nicks); - g_free(rec); + if (rec->nicks != NULL) + g_hash_table_destroy(rec->nicks); + g_free(rec); } static void nickmatch_check_channel(CHANNEL_REC *channel, NICKMATCH_REC *rec) @@ -65,8 +67,8 @@ void nickmatch_rebuild(NICKMATCH_REC *rec) if (rec->nicks != NULL) g_hash_table_destroy(rec->nicks); - rec->nicks = g_hash_table_new((GHashFunc) g_direct_hash, - (GCompareFunc) g_direct_equal); + rec->nicks = g_hash_table_new_full((GHashFunc) g_direct_hash, (GCompareFunc) g_direct_equal, + NULL, (GDestroyNotify) rec->value_destroy_func); g_slist_foreach(channels, (GFunc) nickmatch_check_channel, rec); } diff --git a/src/core/nickmatch-cache.h b/src/core/nickmatch-cache.h index ff58d853..bae99c1d 100644 --- a/src/core/nickmatch-cache.h +++ b/src/core/nickmatch-cache.h @@ -7,9 +7,10 @@ typedef void (*NICKMATCH_REBUILD_FUNC) (GHashTable *list, typedef struct { GHashTable *nicks; NICKMATCH_REBUILD_FUNC func; + GDestroyNotify value_destroy_func; } NICKMATCH_REC; -NICKMATCH_REC *nickmatch_init(NICKMATCH_REBUILD_FUNC func); +NICKMATCH_REC *nickmatch_init(NICKMATCH_REBUILD_FUNC func, GDestroyNotify value_destroy_func); void nickmatch_deinit(NICKMATCH_REC *rec); /* Calls rebuild function for all nicks in all channels. diff --git a/src/fe-common/core/hilight-text.c b/src/fe-common/core/hilight-text.c index d308bd04..66e2dfd3 100644 --- a/src/fe-common/core/hilight-text.c +++ b/src/fe-common/core/hilight-text.c @@ -793,7 +793,7 @@ void hilight_text_init(void) read_settings(); - nickmatch = nickmatch_init(hilight_nick_cache); + nickmatch = nickmatch_init(hilight_nick_cache, NULL); read_hilight_config(); signal_add_first("print text", (SIGNAL_FUNC) sig_print_text);