]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
vbox: handle errors of virDomainHostdevDefAlloc correctly
authorRyota Ozaki <ozaki.ryota@gmail.com>
Sun, 1 Dec 2013 14:46:06 +0000 (23:46 +0900)
committerDaniel Veillard <veillard@redhat.com>
Mon, 2 Dec 2013 02:59:21 +0000 (10:59 +0800)
The original code ignored errors of virDomainHostdevDefAlloc,
however, we should properly do error return from the function
if it occurs.

The fix pulls out virDomainHostdevDefAlloc from the loop and
executes it all together before the loop. So we can easily
return on errors without the notion of other memory allocations
in the loop.

The deallocation code is separated from the allocation code
because it will be used by a further patch for fixing other error
handlings.

Reported-by: Laine Stump <laine@laine.org>
Signed-off-by: Ryota Ozaki <ozaki.ryota@gmail.com>
src/vbox/vbox_tmpl.c

index cc5f27571416c8f9355e5564639982109e9b334d..9336514abfcf9d9f6e44cef823a84cc367c4e527 100644 (file)
@@ -2269,6 +2269,12 @@ static void vboxHostDeviceGetXMLDesc(vboxGlobalData *data, virDomainDefPtr def,
     if (VIR_ALLOC_N(def->hostdevs, def->nhostdevs) < 0)
         goto release_filters;
 
+    for (i = 0; i < def->nhostdevs; i++) {
+        def->hostdevs[i] = virDomainHostdevDefAlloc();
+        if (!def->hostdevs[i])
+            goto release_hostdevs;
+    }
+
     for (i = 0; i < deviceFilters.count; i++) {
         PRBool active                  = PR_FALSE;
         IUSBDeviceFilter *deviceFilter = deviceFilters.items[i];
@@ -2284,10 +2290,6 @@ static void vboxHostDeviceGetXMLDesc(vboxGlobalData *data, virDomainDefPtr def,
         if (!active)
             continue;
 
-        def->hostdevs[USBFilterCount] = virDomainHostdevDefAlloc();
-        if (!def->hostdevs[USBFilterCount])
-            continue;
-
         def->hostdevs[USBFilterCount]->mode =
             VIR_DOMAIN_HOSTDEV_MODE_SUBSYS;
         def->hostdevs[USBFilterCount]->source.subsys.type =
@@ -2322,6 +2324,15 @@ release_controller:
 #else
     VBOX_RELEASE(USBDeviceFilters);
 #endif
+
+    return;
+
+release_hostdevs:
+    for (i = 0; i < def->nhostdevs; i++)
+        virDomainHostdevDefFree(def->hostdevs[i]);
+    VIR_FREE(def->hostdevs);
+
+    goto release_filters;
 }
 
 static char *vboxDomainGetXMLDesc(virDomainPtr dom, unsigned int flags) {