From 1f9fed85566a8eac647f0cd62db5380113ea0ef0 Mon Sep 17 00:00:00 2001 From: Rob Kendrick Date: Sun, 20 Aug 2006 13:46:30 +0000 Subject: Make hash_add() return success/failure bool svn path=/trunk/netsurf/; revision=2872 --- utils/hashtable.c | 18 +++++++++++++++++- utils/hashtable.h | 4 +++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/utils/hashtable.c b/utils/hashtable.c index 6f06332b7..d34b19960 100644 --- a/utils/hashtable.c +++ b/utils/hashtable.c @@ -9,6 +9,7 @@ #include #include +#include #ifdef TEST_RIG #include #include @@ -60,7 +61,7 @@ void hash_destroy(struct hash_table *ht) free(ht); } -void hash_add(struct hash_table *ht, const char *key, const char *value) +bool hash_add(struct hash_table *ht, const char *key, const char *value) { unsigned int h = hash_string_fnv(key); unsigned int c = h % ht->nchains; @@ -68,9 +69,24 @@ void hash_add(struct hash_table *ht, const char *key, const char *value) sizeof(struct hash_entry)); e->key = strdup(key); + if (e->key == NULL) { + LOG(("Unable to strdup() key for hash table.")); + free(e); + return false; + } + e->value = strdup(value); + if (e->value == NULL) { + LOG(("Unable to strdup() value for hash table.")); + free(e->key); + free(e); + return false; + } + e->next = ht->chain[c]; ht->chain[c] = e; + + return true; } const char *hash_get(struct hash_table *ht, const char *key) diff --git a/utils/hashtable.h b/utils/hashtable.h index 8f9df5009..44c40d7c8 100644 --- a/utils/hashtable.h +++ b/utils/hashtable.h @@ -10,6 +10,8 @@ #ifndef _NETSURF_HASH_H_ #define _NETSURF_HASH_H_ +#include + struct hash_entry { char *key; char *value; @@ -23,7 +25,7 @@ struct hash_table { struct hash_table *hash_create(unsigned int chains); void hash_destroy(struct hash_table *ht); -void hash_add(struct hash_table *ht, const char *key, const char *value); +bool hash_add(struct hash_table *ht, const char *key, const char *value); const char *hash_get(struct hash_table *ht, const char *key); unsigned int hash_string_fnv(const char *datum); -- cgit v1.2.3