X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fhash.c;h=b3595ad4ce4517f8f4b50ab5a7ee847ae1513422;hb=5cbb24e1974b59f7580428768201b950423b88e2;hp=2b856f6810df38cc739c62e7b2695b5586b1cc69;hpb=96626a07ae084d613fd85fa9eb87fd8b3495af59;p=rocksndiamonds.git diff --git a/src/libgame/hash.c b/src/libgame/hash.c index 2b856f68..b3595ad4 100644 --- a/src/libgame/hash.c +++ b/src/libgame/hash.c @@ -423,18 +423,24 @@ hashtable_iterator(struct hashtable *h) /* key - return the key of the (key, value) pair at the current position */ void * -hashtable_iterator_key(struct hashtable_itr *i) +hashtable_iterator_key(struct hashtable_itr *itr) { - return i->e->k; + if (itr == NULL || itr->e == NULL) + return NULL; + + return itr->e->k; } /*****************************************************************************/ /* value - return the value of the (key, value) pair at the current position */ void * -hashtable_iterator_value(struct hashtable_itr *i) +hashtable_iterator_value(struct hashtable_itr *itr) { - return i->e->v; + if (itr == NULL || itr->e == NULL) + return NULL; + + return itr->e->v; } /*****************************************************************************/ @@ -529,17 +535,21 @@ hashtable_foreach_remove(struct hashtable *h, hashtable_remove_fn fn, void *user free(itr); - struct hashtable_itr *itr_remove = hashtable_iterator(remove); unsigned int num_removed = 0; - do + if (hashtable_count(remove) > 0) { - hashtable_remove(h, hashtable_iterator_key(itr_remove)); - num_removed++; - } - while (hashtable_iterator_advance(itr_remove)); + struct hashtable_itr *itr_remove = hashtable_iterator(remove); - free(itr_remove); + do + { + hashtable_remove(h, hashtable_iterator_key(itr_remove)); + num_removed++; + } + while (hashtable_iterator_advance(itr_remove)); + + free(itr_remove); + } hashtable_destroy(remove);