From 5fd5aab2be62d0e042b00c9b1f3004e379198aab Mon Sep 17 00:00:00 2001 From: MassimilianoPinto Date: Fri, 14 Feb 2014 10:49:06 +0100 Subject: [PATCH] Added checks for possible handle hashtable_add() failure Added checks for possible handle hashtable_add() failure with key, value and copyfn --- server/core/hashtable.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/server/core/hashtable.c b/server/core/hashtable.c index f29c3c476..50857bfec 100644 --- a/server/core/hashtable.c +++ b/server/core/hashtable.c @@ -184,6 +184,9 @@ hashtable_add(HASHTABLE *table, void *key, void *value) int hashkey; HASHENTRIES *entry; + if (key == NULL || value == NULL) + return 0; + if (table->hashsize <= 0) { return 0; } else { @@ -209,8 +212,27 @@ hashtable_add(HASHTABLE *table, void *key, void *value) hashtable_write_unlock(table); return 0; } + + /* copy the key */ ptr->key = table->kcopyfn(key); + + /* check succesfull key copy */ + if ( ptr->key == NULL) { + return 0; + } + + /* copy the value */ ptr->value = table->vcopyfn(value); + + /* check succesfull value copy */ + if ( ptr->value == NULL) { + /* remove the key ! */ + table->kfreefn(ptr->key); + + /* value not copied, return */ + return 0; + } + ptr->next = table->entries[hashkey % table->hashsize]; table->entries[hashkey % table->hashsize] = ptr; }