From bebcf1f40fc6a9f06cf913185b68b258c7a3f1ac Mon Sep 17 00:00:00 2001 From: Stathis Kamperis Date: Mon, 10 Sep 2007 20:00:19 +0300 Subject: [PATCH] htable_insert() doesn't ovewrite existing data --- genstructs/htable/htable.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/genstructs/htable/htable.c b/genstructs/htable/htable.c index ca09870..e9d831a 100644 --- a/genstructs/htable/htable.c +++ b/genstructs/htable/htable.c @@ -111,19 +111,11 @@ htret_t htable_insert(htable_t *htable, const void *key, void *data) /* Calculate hash */ hash = htable->ht_hashf(key); - /* Search across chain if there is already an entry - 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. */ + /* Search across chain if there is already an entry with the same key. */ 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_key); - free(pnode->hn_data); - pnode->hn_key = key; - pnode->hn_data = data; + if (htable->ht_cmpf(pnode->hn_key, key) == 0) return HT_REPLACED; - } /* Allocate memory for new entry */ if ((pnode = malloc(sizeof *pnode)) == NULL) -- 2.11.4.GIT