提交 ea1eadd6 编写于 作者: J John Ferlan

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...
上级 8914b875
...@@ -268,6 +268,14 @@ cleanup: ...@@ -268,6 +268,14 @@ cleanup:
return ret; return ret;
} }
static void
virSCSIDeviceUsedByInfoFree(virUsedByInfoPtr used_by)
{
VIR_FREE(used_by->drvname);
VIR_FREE(used_by->domname);
VIR_FREE(used_by);
}
void void
virSCSIDeviceFree(virSCSIDevicePtr dev) virSCSIDeviceFree(virSCSIDevicePtr dev)
{ {
...@@ -279,11 +287,8 @@ virSCSIDeviceFree(virSCSIDevicePtr dev) ...@@ -279,11 +287,8 @@ virSCSIDeviceFree(virSCSIDevicePtr dev)
VIR_FREE(dev->id); VIR_FREE(dev->id);
VIR_FREE(dev->name); VIR_FREE(dev->name);
VIR_FREE(dev->sg_path); VIR_FREE(dev->sg_path);
for (i = 0; i < dev->n_used_by; i++) { for (i = 0; i < dev->n_used_by; i++)
VIR_FREE(dev->used_by[i]->drvname); virSCSIDeviceUsedByInfoFree(dev->used_by[i]);
VIR_FREE(dev->used_by[i]->domname);
VIR_FREE(dev->used_by[i]);
}
VIR_FREE(dev->used_by); VIR_FREE(dev->used_by);
VIR_FREE(dev); VIR_FREE(dev);
} }
...@@ -296,12 +301,18 @@ virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev, ...@@ -296,12 +301,18 @@ virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev,
virUsedByInfoPtr copy; virUsedByInfoPtr copy;
if (VIR_ALLOC(copy) < 0) if (VIR_ALLOC(copy) < 0)
return -1; return -1;
if (VIR_STRDUP(copy->drvname, drvname) < 0) if (VIR_STRDUP(copy->drvname, drvname) < 0 ||
return -1; VIR_STRDUP(copy->domname, domname) < 0)
if (VIR_STRDUP(copy->domname, domname) < 0) goto cleanup;
return -1;
if (VIR_APPEND_ELEMENT(dev->used_by, dev->n_used_by, copy) < 0)
goto cleanup;
return VIR_APPEND_ELEMENT(dev->used_by, dev->n_used_by, copy); return 0;
cleanup:
virSCSIDeviceUsedByInfoFree(copy);
return -1;
} }
bool bool
...@@ -449,9 +460,7 @@ virSCSIDeviceListDel(virSCSIDeviceListPtr list, ...@@ -449,9 +460,7 @@ virSCSIDeviceListDel(virSCSIDeviceListPtr list,
if (STREQ_NULLABLE(dev->used_by[i]->drvname, drvname) && if (STREQ_NULLABLE(dev->used_by[i]->drvname, drvname) &&
STREQ_NULLABLE(dev->used_by[i]->domname, domname)) { STREQ_NULLABLE(dev->used_by[i]->domname, domname)) {
if (dev->n_used_by > 1) { if (dev->n_used_by > 1) {
VIR_FREE(dev->used_by[i]->drvname); virSCSIDeviceUsedByInfoFree(dev->used_by[i]);
VIR_FREE(dev->used_by[i]->domname);
VIR_FREE(dev->used_by[i]);
VIR_DELETE_ELEMENT(dev->used_by, i, dev->n_used_by); VIR_DELETE_ELEMENT(dev->used_by, i, dev->n_used_by);
} else { } else {
tmp = virSCSIDeviceListSteal(list, dev); tmp = virSCSIDeviceListSteal(list, dev);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册