From efb9fd036bd5e7586d3afd7e27c851bfb9fe315e Mon Sep 17 00:00:00 2001 From: James Bursa Date: Sun, 20 Aug 2006 16:02:22 +0000 Subject: Check for malloc failing in hash_add(). Remove unnecessary casts. svn path=/trunk/netsurf/; revision=2873 --- utils/hashtable.c | 18 ++++++++++-------- utils/hashtable.h | 7 ++++--- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'utils') diff --git a/utils/hashtable.c b/utils/hashtable.c index d34b19960..51ed0c330 100644 --- a/utils/hashtable.c +++ b/utils/hashtable.c @@ -5,7 +5,8 @@ * Copyright 2006 Rob Kendrick */ -/** Write-Once hash table for string to string mappings */ +/** \file + * Write-Once hash table for string to string mappings */ #include #include @@ -19,8 +20,7 @@ struct hash_table *hash_create(unsigned int chains) { - struct hash_table *r = (struct hash_table *)malloc( - sizeof(struct hash_table)); + struct hash_table *r = malloc(sizeof(struct hash_table)); if (r == NULL) { LOG(("Not enough memory for hash table.")); @@ -28,8 +28,7 @@ struct hash_table *hash_create(unsigned int chains) } r->nchains = chains; - r->chain = (struct hash_entry **) - calloc(chains, sizeof(struct hash_entry)); + r->chain = calloc(chains, sizeof(struct hash_entry)); if (r->chain == NULL) { LOG(("Not enough memory for %d hash table chains.", chains)); @@ -42,7 +41,7 @@ struct hash_table *hash_create(unsigned int chains) void hash_destroy(struct hash_table *ht) { - int i; + unsigned int i; for (i = 0; i < ht->nchains; i++) { if (ht->chain[i] != NULL) { @@ -65,8 +64,11 @@ 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; - struct hash_entry *e = (struct hash_entry *)malloc( - sizeof(struct hash_entry)); + struct hash_entry *e = malloc(sizeof(struct hash_entry)); + if (e == NULL) { + LOG(("Not enough memory for hash entry.")); + return false; + } e->key = strdup(key); if (e->key == NULL) { diff --git a/utils/hashtable.h b/utils/hashtable.h index 44c40d7c8..567cc28a7 100644 --- a/utils/hashtable.h +++ b/utils/hashtable.h @@ -5,10 +5,11 @@ * Copyright 2006 Rob Kendrick */ -/** Write-Once hash table for string to string mappings */ +/** \file + * Write-Once hash table for string to string mappings */ -#ifndef _NETSURF_HASH_H_ -#define _NETSURF_HASH_H_ +#ifndef _NETSURF_UTILS_HASHTABLE_H_ +#define _NETSURF_UTILS_HASHTABLE_H_ #include -- cgit v1.2.3