]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Fix crash when removing entries during hash iteration
authorJiri Denemark <jdenemar@redhat.com>
Tue, 12 Apr 2011 15:58:22 +0000 (17:58 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Tue, 12 Apr 2011 17:18:08 +0000 (19:18 +0200)
Commit 9677cd33eea4c65d78ba463b46b8b45ed2da1709 made it possible to
remove current entry when iterating through all hash entries. However,
it didn't properly handle a special case of removing first entry
assigned to a given key which contains several entries in its collision
list.

src/util/hash.c

index 48a94ad12dfa30fb55102d3c7a20e5004633db52..fc7652d2926dd5a40c464ea4dff08673847fe4bc 100644 (file)
@@ -585,10 +585,18 @@ int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data)
         while (entry) {
             virHashEntryPtr next = entry->next;
             if (entry->valid) {
+                void *name = (entry == table->table + i) ? entry->name : NULL;
+
                 table->current = entry;
                 iter(entry->payload, entry->name, data);
                 table->current = NULL;
                 count++;
+
+                /* revisit current entry if it was the first one in collision
+                 * list and its content changed, i.e. it was deleted by iter()
+                 */
+                if (name && name != entry->name)
+                    continue;
             }
             entry = next;
         }