summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--utils/hashtable.c18
-rw-r--r--utils/hashtable.h4
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 <stdlib.h>
#include <string.h>
+#include <stdbool.h>
#ifdef TEST_RIG
#include <assert.h>
#include <stdio.h>
@@ -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 <stdbool.h>
+
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);