[PATCH 2/2] virhostdevtest: Decrease possibility of uninitialized @subsys

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 tests/virhostdevtest.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/tests/virhostdevtest.c b/tests/virhostdevtest.c
index 92bafcbb49..1aed0d2b6d 100644
--- a/tests/virhostdevtest.c
+++ b/tests/virhostdevtest.c
@@ -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);
-- 
2.39.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux