From 7354b1880a2cdd3a1bb2a26930af6316014111e8 Mon Sep 17 00:00:00 2001 From: Stathis Kamperis Date: Mon, 10 Sep 2007 06:01:11 +0300 Subject: [PATCH] Free old data when inserting new entry with existing key --- genstructs/htable/htable.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/genstructs/htable/htable.c b/genstructs/htable/htable.c index 3422400..f0a8ab2 100644 --- a/genstructs/htable/htable.c +++ b/genstructs/htable/htable.c @@ -110,10 +110,13 @@ htret_t htable_insert(htable_t *htable, const void *key, void *data) hash = htable->ht_hashf(key); /* Search across chain if there is already an entry - with the same key. If there is, replace it. */ + with the same key. If there is, replace it. + Don't forget to free the old data, before overwriting + the pointer or else we will leak. */ phead = &htable->ht_table[hash & (htable->ht_size - 1)]; TAILQ_FOREACH(pnode, phead, hn_next) if (htable->ht_cmpf(pnode->hn_key, key) == 0) { + free(pnode->hn_data); pnode->hn_data = data; return HT_OK; } -- 2.11.4.GIT