[libvirt PATCH] node_device: Fix memory leak in udevProcessMediatedDevice

Jiri Denemark posted 1 patch 2 years, 6 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/e86cb5f60a796a8ad7b6f55b73102f40ba154798.1635175945.git.jdenemar@redhat.com
src/node_device/node_device_udev.c | 17 ++++++-----------
1 file changed, 6 insertions(+), 11 deletions(-)
[libvirt PATCH] node_device: Fix memory leak in udevProcessMediatedDevice
Posted by Jiri Denemark 2 years, 6 months ago
One of the paths returned -1 directly without going through the cleanup
section.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/node_device/node_device_udev.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index 7c3bb762b3..cd1722f934 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -1020,10 +1020,9 @@ static int
 udevProcessMediatedDevice(struct udev_device *dev,
                           virNodeDeviceDef *def)
 {
-    int ret = -1;
     int iommugrp = -1;
-    char *linkpath = NULL;
-    char *canonicalpath = NULL;
+    g_autofree char *linkpath = NULL;
+    g_autofree char *canonicalpath = NULL;
     virNodeDevCapMdev *data = &def->caps->data.mdev;
     struct udev_device *parent_device = NULL;
 
@@ -1039,19 +1038,19 @@ udevProcessMediatedDevice(struct udev_device *dev,
         virReportSystemError(errno,
                              _("failed to wait for file '%s' to appear"),
                              linkpath);
-        goto cleanup;
+        return -1;
     }
 
     if (virFileResolveLink(linkpath, &canonicalpath) < 0) {
         virReportSystemError(errno, _("failed to resolve '%s'"), linkpath);
-        goto cleanup;
+        return -1;
     }
 
     data->type = g_path_get_basename(canonicalpath);
 
     data->uuid = g_strdup(udev_device_get_sysname(dev));
     if ((iommugrp = virMediatedDeviceGetIOMMUGroupNum(data->uuid)) < 0)
-        goto cleanup;
+        return -1;
 
     /* lookup the address of parent device */
     parent_device = udev_device_get_parent(dev);
@@ -1072,11 +1071,7 @@ udevProcessMediatedDevice(struct udev_device *dev,
 
     data->iommuGroupNumber = iommugrp;
 
-    ret = 0;
- cleanup:
-    VIR_FREE(linkpath);
-    VIR_FREE(canonicalpath);
-    return ret;
+    return 0;
 }
 
 
-- 
2.33.1

Re: [libvirt PATCH] node_device: Fix memory leak in udevProcessMediatedDevice
Posted by Laine Stump 2 years, 6 months ago
On 10/25/21 11:32 AM, Jiri Denemark wrote:
> One of the paths returned -1 directly without going through the cleanup
> section.
> 
> Signed-off-by: Jiri Denemark <jdenemar@redhat.com>

Reviewed-by: Laine Stump <laine@redhat.com>