1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00

virhostdevtest: Decrease possibility of uninitialized @subsys

With the current way the myInit() is written, it's fairly easy to
miss initialization of @subsys variable as the variable is
allocated firstly on the stack and then it's assigned to
hostdev[i] which was allocated using g_new0() (this it is
containing nothing but all zeroes).

Make the subsys point to the corresponding member in hostdev[i]
from the start. This way only the important bits are overwritten
and the rest stays initialized to zero.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Michal Privoznik 2023-02-06 16:03:44 +01:00
parent af954d6046
commit 874e0916c3

View File

@ -123,23 +123,23 @@ myInit(void)
size_t i;
for (i = 0; i < nhostdevs; i++) {
virDomainHostdevSubsys subsys = {0};
virDomainHostdevSubsys *subsys;
hostdevs[i] = virDomainHostdevDefNew();
if (!hostdevs[i])
goto cleanup;
hostdevs[i]->mode = VIR_DOMAIN_HOSTDEV_MODE_SUBSYS;
subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI;
subsys.u.pci.addr.domain = 0;
subsys.u.pci.addr.bus = 0;
subsys.u.pci.addr.slot = i + 1;
subsys.u.pci.addr.function = 0;
subsys.u.pci.backend = VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO;
hostdevs[i]->source.subsys = subsys;
subsys = &hostdevs[i]->source.subsys;
subsys->type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI;
subsys->u.pci.addr.domain = 0;
subsys->u.pci.addr.bus = 0;
subsys->u.pci.addr.slot = i + 1;
subsys->u.pci.addr.function = 0;
subsys->u.pci.backend = VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO;
}
for (i = 0; i < nhostdevs; i++) {
virDomainHostdevSubsys subsys = hostdevs[i]->source.subsys;
if (!(dev[i] = virPCIDeviceNew(&subsys.u.pci.addr)))
virDomainHostdevSubsys *subsys = &hostdevs[i]->source.subsys;
if (!(dev[i] = virPCIDeviceNew(&subsys->u.pci.addr)))
goto cleanup;
virPCIDeviceSetStubDriver(dev[i], VIR_PCI_STUB_DRIVER_VFIO);