[libvirt] [PATCH 1/2] hostdev: Fix index error in loop after remove an element

Huanle Han hanxueluo at gmail.com
Thu Mar 26 14:23:46 UTC 2015


'virPCIDeviceList' is actually an array. Removing one element makes the
rest of the element move.

Use while loop, increase index only when not virPCIDeviceListDel(pcidevs, dev)

Signed-off-by: Huanle Han <hanxueluo at gmail.com>
---
 src/util/virhostdev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
index 23365a3..83f567d 100644
--- a/src/util/virhostdev.c
+++ b/src/util/virhostdev.c
@@ -785,7 +785,8 @@ virHostdevReAttachPCIDevices(virHostdevManagerPtr hostdev_mgr,
      * them and reset all the devices before re-attach.
      * Attach mac and port profile parameters to devices
      */
-    for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
+    i = 0;
+    while (i < virPCIDeviceListCount(pcidevs)) {
         virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i);
         virPCIDevicePtr activeDev = NULL;
 
@@ -806,6 +807,7 @@ virHostdevReAttachPCIDevices(virHostdevManagerPtr hostdev_mgr,
         }
 
         virPCIDeviceListDel(hostdev_mgr->activePCIHostdevs, dev);
+        i++;
     }
 
     /* At this point, any device that had been used by the guest is in
-- 
1.9.1




More information about the libvir-list mailing list