[PATCH] virscsi: Introduce virSCSIDeviceUsedByInfoFree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This resolves a Coverity RESOURCE_LEAK issue introduced by commit
id 'de6fa535' where the virSCSIDeviceSetUsedBy() didn't VIR_FREE
the 'copy' or possibly VIR_STRDUP()'d values.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/util/virscsi.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/util/virscsi.c b/src/util/virscsi.c
index 69eae24..66e3161 100644
--- a/src/util/virscsi.c
+++ b/src/util/virscsi.c
@@ -268,6 +268,14 @@ cleanup:
     return ret;
 }
 
+static void
+virSCSIDeviceUsedByInfoFree(virUsedByInfoPtr used_by)
+{
+    VIR_FREE(used_by->drvname);
+    VIR_FREE(used_by->domname);
+    VIR_FREE(used_by);
+}
+
 void
 virSCSIDeviceFree(virSCSIDevicePtr dev)
 {
@@ -279,11 +287,8 @@ virSCSIDeviceFree(virSCSIDevicePtr dev)
     VIR_FREE(dev->id);
     VIR_FREE(dev->name);
     VIR_FREE(dev->sg_path);
-    for (i = 0; i < dev->n_used_by; i++) {
-        VIR_FREE(dev->used_by[i]->drvname);
-        VIR_FREE(dev->used_by[i]->domname);
-        VIR_FREE(dev->used_by[i]);
-    }
+    for (i = 0; i < dev->n_used_by; i++)
+        virSCSIDeviceUsedByInfoFree(dev->used_by[i]);
     VIR_FREE(dev->used_by);
     VIR_FREE(dev);
 }
@@ -296,10 +301,11 @@ virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev,
     virUsedByInfoPtr copy;
     if (VIR_ALLOC(copy) < 0)
         return -1;
-    if (VIR_STRDUP(copy->drvname, drvname) < 0)
-        return -1;
-    if (VIR_STRDUP(copy->domname, domname) < 0)
+    if (VIR_STRDUP(copy->drvname, drvname) < 0 ||
+        VIR_STRDUP(copy->domname, domname) < 0) {
+        virSCSIDeviceUsedByInfoFree(copy);
         return -1;
+    }
 
     return VIR_APPEND_ELEMENT(dev->used_by, dev->n_used_by, copy);
 }
@@ -449,9 +455,7 @@ virSCSIDeviceListDel(virSCSIDeviceListPtr list,
         if (STREQ_NULLABLE(dev->used_by[i]->drvname, drvname) &&
             STREQ_NULLABLE(dev->used_by[i]->domname, domname)) {
             if (dev->n_used_by > 1) {
-                VIR_FREE(dev->used_by[i]->drvname);
-                VIR_FREE(dev->used_by[i]->domname);
-                VIR_FREE(dev->used_by[i]);
+                virSCSIDeviceUsedByInfoFree(dev->used_by[i]);
                 VIR_DELETE_ELEMENT(dev->used_by, i, dev->n_used_by);
             } else {
                 tmp = virSCSIDeviceListSteal(list, dev);
-- 
1.8.5.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]