Added null checks to hashtable functions and fixed possible crash with reload users.

This commit is contained in:
Markus Makela
2015-07-10 20:38:10 +03:00
parent ce0d9684d4
commit 2fe6d837fc
2 changed files with 22 additions and 5 deletions

View File

@ -210,7 +210,7 @@ HASHTABLE *oldresources;
oldusers = service->users; oldusers = service->users;
/* digest compare */ /* digest compare */
if (memcmp(oldusers->cksum, newusers->cksum, SHA_DIGEST_LENGTH) == 0) { if (oldusers && memcmp(oldusers->cksum, newusers->cksum, SHA_DIGEST_LENGTH) == 0) {
/* same data, nothing to do */ /* same data, nothing to do */
LOGIF(LD, (skygw_log_write_flush( LOGIF(LD, (skygw_log_write_flush(
LOGFILE_DEBUG, LOGFILE_DEBUG,
@ -234,7 +234,7 @@ HASHTABLE *oldresources;
spinlock_release(&service->spin); spinlock_release(&service->spin);
if (i) { if (i && oldusers) {
/* free the old table */ /* free the old table */
users_free(oldusers); users_free(oldusers);
} }

View File

@ -170,6 +170,9 @@ hashtable_free(HASHTABLE *table)
int i; int i;
HASHENTRIES *entry, *ptr; HASHENTRIES *entry, *ptr;
if(table == NULL)
return;
hashtable_write_lock(table); hashtable_write_lock(table);
for (i = 0; i < table->hashsize; i++) for (i = 0; i < table->hashsize; i++)
{ {
@ -235,7 +238,7 @@ hashtable_add(HASHTABLE *table, void *key, void *value)
unsigned int hashkey; unsigned int hashkey;
HASHENTRIES *entry; HASHENTRIES *entry;
if (key == NULL || value == NULL) if (table == NULL || key == NULL || value == NULL)
return 0; return 0;
if (table->hashsize <= 0) { if (table->hashsize <= 0) {
@ -308,9 +311,13 @@ hashtable_add(HASHTABLE *table, void *key, void *value)
int int
hashtable_delete(HASHTABLE *table, void *key) hashtable_delete(HASHTABLE *table, void *key)
{ {
unsigned int hashkey = table->hashfn(key) % table->hashsize; unsigned int hashkey;
HASHENTRIES *entry, *ptr; HASHENTRIES *entry, *ptr;
if(table == NULL || key == NULL)
return 0;
hashkey = table->hashfn(key) % table->hashsize;
hashtable_write_lock(table); hashtable_write_lock(table);
entry = table->entries[hashkey % table->hashsize]; entry = table->entries[hashkey % table->hashsize];
while (entry && entry->key && table->cmpfn(key, entry->key) != 0) while (entry && entry->key && table->cmpfn(key, entry->key) != 0)
@ -369,9 +376,13 @@ HASHENTRIES *entry, *ptr;
void * void *
hashtable_fetch(HASHTABLE *table, void *key) hashtable_fetch(HASHTABLE *table, void *key)
{ {
unsigned int hashkey = table->hashfn(key) % table->hashsize; unsigned int hashkey;
HASHENTRIES *entry; HASHENTRIES *entry;
if(table == NULL || key == NULL)
return NULL;
hashkey = table->hashfn(key) % table->hashsize;
hashtable_read_lock(table); hashtable_read_lock(table);
entry = table->entries[hashkey % table->hashsize]; entry = table->entries[hashkey % table->hashsize];
while (entry && entry->key && table->cmpfn(key, entry->key) != 0) while (entry && entry->key && table->cmpfn(key, entry->key) != 0)
@ -401,6 +412,9 @@ hashtable_stats(HASHTABLE *table)
int total, longest, i, j; int total, longest, i, j;
HASHENTRIES *entries; HASHENTRIES *entries;
if(table == NULL)
return;
printf("Hashtable: %p, size %d\n", table, table->hashsize); printf("Hashtable: %p, size %d\n", table, table->hashsize);
total = 0; total = 0;
longest = 0; longest = 0;
@ -606,6 +620,9 @@ hashtable_next(HASHITERATOR *iter)
int i; int i;
HASHENTRIES *entries; HASHENTRIES *entries;
if(iter == NULL)
return NULL;
iter->depth++; iter->depth++;
while (iter->chain < iter->table->hashsize) while (iter->chain < iter->table->hashsize)
{ {