From ea1eadd6a31f2cce3f62f17619eac04d59b68fde Mon Sep 17 00:00:00 2001 From: John Ferlan Date: Fri, 7 Mar 2014 09:46:21 -0500 Subject: [PATCH] virscsi: Introduce virSCSIDeviceUsedByInfoFree 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. It also ensures that the VIR_APPEND_ELEMENT is successful... --- src/util/virscsi.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/util/virscsi.c b/src/util/virscsi.c index 69eae241cf..802f515c1e 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,12 +301,18 @@ 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) - return -1; + if (VIR_STRDUP(copy->drvname, drvname) < 0 || + VIR_STRDUP(copy->domname, domname) < 0) + goto cleanup; - return VIR_APPEND_ELEMENT(dev->used_by, dev->n_used_by, copy); + if (VIR_APPEND_ELEMENT(dev->used_by, dev->n_used_by, copy) < 0) + goto cleanup; + + return 0; + +cleanup: + virSCSIDeviceUsedByInfoFree(copy); + return -1; } bool @@ -449,9 +460,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);