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@xxxxxxxxx> Signed-off-by: Ryota Ozaki <ozaki.ryota@xxxxxxxxx> --- src/vbox/vbox_tmpl.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index cc5f275..9336514 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -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) { -- 1.8.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list