]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virhostdevtest: Decrease possibility of uninitialized @subsys
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 6 Feb 2023 15:03:44 +0000 (16:03 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 6 Feb 2023 15:33:26 +0000 (16:33 +0100)
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>
tests/virhostdevtest.c

index 92bafcbb49f4d5a348dc26f292033f4f51b05287..1aed0d2b6d167d726e806a868f15aa8c49b97d77 100644 (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);