security_dac.c: use g_auto* in hostdev label set/restore functions

Use g_auto* cleanup to avoid free() calls.

Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Daniel Henrique Barboza 2021-01-04 09:54:32 -03:00
parent e49319534e
commit 4ab0d1844a

View File

@ -1251,7 +1251,7 @@ virSecurityDACSetHostdevLabel(virSecurityManagerPtr mgr,
switch ((virDomainHostdevSubsysType)dev->source.subsys.type) { switch ((virDomainHostdevSubsysType)dev->source.subsys.type) {
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
virUSBDevicePtr usb; g_autoptr(virUSBDevice) usb = NULL;
if (dev->missing) if (dev->missing)
return 0; return 0;
@ -1262,41 +1262,35 @@ virSecurityDACSetHostdevLabel(virSecurityManagerPtr mgr,
ret = virUSBDeviceFileIterate(usb, ret = virUSBDeviceFileIterate(usb,
virSecurityDACSetUSBLabel, virSecurityDACSetUSBLabel,
&cbdata); &cbdata);
virUSBDeviceFree(usb);
break; break;
} }
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
virPCIDevicePtr pci = g_autoptr(virPCIDevice) pci = virPCIDeviceNew(&pcisrc->addr);
virPCIDeviceNew(&pcisrc->addr);
if (!pci) if (!pci)
return -1; return -1;
if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) { if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci); g_autofree char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci);
if (!vfioGroupDev) { if (!vfioGroupDev)
virPCIDeviceFree(pci);
return -1; return -1;
}
ret = virSecurityDACSetHostdevLabelHelper(vfioGroupDev, ret = virSecurityDACSetHostdevLabelHelper(vfioGroupDev,
false, false,
&cbdata); &cbdata);
VIR_FREE(vfioGroupDev);
} else { } else {
ret = virPCIDeviceFileIterate(pci, ret = virPCIDeviceFileIterate(pci,
virSecurityDACSetPCILabel, virSecurityDACSetPCILabel,
&cbdata); &cbdata);
} }
virPCIDeviceFree(pci);
break; break;
} }
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host; virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
virSCSIDevicePtr scsi = g_autoptr(virSCSIDevice) scsi =
virSCSIDeviceNew(NULL, virSCSIDeviceNew(NULL,
scsihostsrc->adapter, scsihostsrc->bus, scsihostsrc->adapter, scsihostsrc->bus,
scsihostsrc->target, scsihostsrc->unit, scsihostsrc->target, scsihostsrc->unit,
@ -1308,13 +1302,11 @@ virSecurityDACSetHostdevLabel(virSecurityManagerPtr mgr,
ret = virSCSIDeviceFileIterate(scsi, ret = virSCSIDeviceFileIterate(scsi,
virSecurityDACSetSCSILabel, virSecurityDACSetSCSILabel,
&cbdata); &cbdata);
virSCSIDeviceFree(scsi);
break; break;
} }
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: {
virSCSIVHostDevicePtr host = virSCSIVHostDeviceNew(hostsrc->wwpn); g_autoptr(virSCSIVHostDevice) host = virSCSIVHostDeviceNew(hostsrc->wwpn);
if (!host) if (!host)
return -1; return -1;
@ -1322,19 +1314,16 @@ virSecurityDACSetHostdevLabel(virSecurityManagerPtr mgr,
ret = virSCSIVHostDeviceFileIterate(host, ret = virSCSIVHostDeviceFileIterate(host,
virSecurityDACSetHostLabel, virSecurityDACSetHostLabel,
&cbdata); &cbdata);
virSCSIVHostDeviceFree(host);
break; break;
} }
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: {
char *vfiodev = NULL; g_autofree char *vfiodev = NULL;
if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr))) if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr)))
return -1; return -1;
ret = virSecurityDACSetHostdevLabelHelper(vfiodev, true, &cbdata); ret = virSecurityDACSetHostdevLabelHelper(vfiodev, true, &cbdata);
VIR_FREE(vfiodev);
break; break;
} }
@ -1420,7 +1409,7 @@ virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
switch ((virDomainHostdevSubsysType)dev->source.subsys.type) { switch ((virDomainHostdevSubsysType)dev->source.subsys.type) {
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
virUSBDevicePtr usb; g_autoptr(virUSBDevice) usb = NULL;
if (dev->missing) if (dev->missing)
return 0; return 0;
@ -1429,20 +1418,17 @@ virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
return -1; return -1;
ret = virUSBDeviceFileIterate(usb, virSecurityDACRestoreUSBLabel, mgr); ret = virUSBDeviceFileIterate(usb, virSecurityDACRestoreUSBLabel, mgr);
virUSBDeviceFree(usb);
break; break;
} }
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
virPCIDevicePtr pci = g_autoptr(virPCIDevice) pci = virPCIDeviceNew(&pcisrc->addr);
virPCIDeviceNew(&pcisrc->addr);
if (!pci) if (!pci)
return -1; return -1;
if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) { if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci); g_autofree char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci);
if (!vfioGroupDev) { if (!vfioGroupDev) {
virPCIDeviceFree(pci); virPCIDeviceFree(pci);
@ -1450,17 +1436,15 @@ virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
} }
ret = virSecurityDACRestoreFileLabelInternal(mgr, NULL, ret = virSecurityDACRestoreFileLabelInternal(mgr, NULL,
vfioGroupDev, false); vfioGroupDev, false);
VIR_FREE(vfioGroupDev);
} else { } else {
ret = virPCIDeviceFileIterate(pci, virSecurityDACRestorePCILabel, mgr); ret = virPCIDeviceFileIterate(pci, virSecurityDACRestorePCILabel, mgr);
} }
virPCIDeviceFree(pci);
break; break;
} }
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host; virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
virSCSIDevicePtr scsi = g_autoptr(virSCSIDevice) scsi =
virSCSIDeviceNew(NULL, virSCSIDeviceNew(NULL,
scsihostsrc->adapter, scsihostsrc->bus, scsihostsrc->adapter, scsihostsrc->bus,
scsihostsrc->target, scsihostsrc->unit, scsihostsrc->target, scsihostsrc->unit,
@ -1470,13 +1454,11 @@ virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
return -1; return -1;
ret = virSCSIDeviceFileIterate(scsi, virSecurityDACRestoreSCSILabel, mgr); ret = virSCSIDeviceFileIterate(scsi, virSecurityDACRestoreSCSILabel, mgr);
virSCSIDeviceFree(scsi);
break; break;
} }
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: {
virSCSIVHostDevicePtr host = virSCSIVHostDeviceNew(hostsrc->wwpn); g_autoptr(virSCSIVHostDevice) host = virSCSIVHostDeviceNew(hostsrc->wwpn);
if (!host) if (!host)
return -1; return -1;
@ -1484,19 +1466,16 @@ virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
ret = virSCSIVHostDeviceFileIterate(host, ret = virSCSIVHostDeviceFileIterate(host,
virSecurityDACRestoreHostLabel, virSecurityDACRestoreHostLabel,
mgr); mgr);
virSCSIVHostDeviceFree(host);
break; break;
} }
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: {
char *vfiodev = NULL; g_autofree char *vfiodev = NULL;
if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr))) if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr)))
return -1; return -1;
ret = virSecurityDACRestoreFileLabel(mgr, vfiodev); ret = virSecurityDACRestoreFileLabel(mgr, vfiodev);
VIR_FREE(vfiodev);
break; break;
} }