[PATCH] node_device_conf: Avoid memleak in virNodeDeviceGetPCIVPDDynamicCap()

Michal Privoznik posted 1 patch 1 year, 5 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/512b8043c28eb88ac62cf6edec74827d35315a1d.1669821905.git.mprivozn@redhat.com
src/conf/node_device_conf.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
[PATCH] node_device_conf: Avoid memleak in virNodeDeviceGetPCIVPDDynamicCap()
Posted by Michal Privoznik 1 year, 5 months ago
The virNodeDeviceGetPCIVPDDynamicCap() function is called from
virNodeDeviceGetPCIDynamicCaps() and therefore has to be a wee
bit more clever about adding VPD capability. Namely, it has to
remove the old one before adding a new one. This is how other
functions called from virNodeDeviceGetPCIDynamicCaps() behave
as well.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2143235
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/conf/node_device_conf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c
index 83e66b85e3..af331baaf3 100644
--- a/src/conf/node_device_conf.c
+++ b/src/conf/node_device_conf.c
@@ -3068,6 +3068,9 @@ virNodeDeviceGetPCIVPDDynamicCap(virNodeDevCapPCIDev *devCapPCIDev)
     virPCIDeviceAddress devAddr = { 0 };
     g_autoptr(virPCIVPDResource) res = NULL;
 
+    g_clear_pointer(&devCapPCIDev->vpd, virPCIVPDResourceFree);
+    devCapPCIDev->flags &= ~VIR_NODE_DEV_CAP_FLAG_PCI_VPD;
+
     devAddr.domain = devCapPCIDev->domain;
     devAddr.bus = devCapPCIDev->bus;
     devAddr.slot = devCapPCIDev->slot;
@@ -3081,8 +3084,6 @@ virNodeDeviceGetPCIVPDDynamicCap(virNodeDevCapPCIDev *devCapPCIDev)
         if ((res = virPCIDeviceGetVPD(pciDev))) {
             devCapPCIDev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_VPD;
             devCapPCIDev->vpd = g_steal_pointer(&res);
-        } else {
-            virPCIVPDResourceFree(g_steal_pointer(&devCapPCIDev->vpd));
         }
     }
     return 0;
-- 
2.37.4
Re: [PATCH] node_device_conf: Avoid memleak in virNodeDeviceGetPCIVPDDynamicCap()
Posted by Peter Krempa 1 year, 5 months ago
On Wed, Nov 30, 2022 at 16:25:05 +0100, Michal Privoznik wrote:
> The virNodeDeviceGetPCIVPDDynamicCap() function is called from
> virNodeDeviceGetPCIDynamicCaps() and therefore has to be a wee
> bit more clever about adding VPD capability. Namely, it has to
> remove the old one before adding a new one. This is how other
> functions called from virNodeDeviceGetPCIDynamicCaps() behave
> as well.
> 
> Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2143235
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/conf/node_device_conf.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)

Reviewed-by: Peter Krempa <pkrempa@redhat.com>