[libvirt] [PATCH] util: Fix crash when removing entries during hash iteration

Jiri Denemark jdenemar at redhat.com
Tue Apr 12 16:54:53 UTC 2011


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.
---
This is an alternate less invasive fix to "util: Simplify hash implementation".
I prefer the more invasive fix for upstream but I'm sending this alternative
here for completeness.

 src/util/hash.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/src/util/hash.c b/src/util/hash.c
index 48a94ad..fc7652d 100644
--- a/src/util/hash.c
+++ b/src/util/hash.c
@@ -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;
         }
-- 
1.7.5.rc1




More information about the libvir-list mailing list