vfio/iommufd: Fix memory leak
PATCH 1/1 vfio/iommufd: Fix memory leak
Make sure variable contents is freed if scanf fails.
--- hw/vfio/iommufd.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
hw/vfio/iommufd.c +3 -2
@@ -152,9 +152,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)if (sscanf (contents,"%d:%d" , &major, &minor) !=2 ) {error_setg (errp,"failed to get major:minor for \"%s\"" , vfio_dev_path); - goto out_free_dev_path; + goto out_free_contents; } -g_free (contents); vfio_devt =makedev (major, minor); vfio_path =g_strdup_printf ("/dev/vfio/devices/%s" , dent->d_name);
@@ -166,6 +165,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)trace_iommufd_cdev_getfd (vfio_path, ret);g_free (vfio_path); +out_free_contents: +g_free (contents); out_free_dev_path:g_free (vfio_dev_path); out_close_dir:
Signature · 1 line
2.44.0
Quoted 8 lines · Cédric Le Goater
Make sure variable contents is freed if scanf fails.
Cc Eric Auger <[email protected]>Cc Yi Liu <[email protected]>Cc Zhenzhong Duan <[email protected]>Fixes CID 1540007Fixes 5ee3dc7af785 ("vfio/iommufd: Implement the iommufd backend")Signed-off-by Cédric Le Goater <[email protected]>
Thanks!
Eric
Quoted 28 lines
--- hw/vfio/iommufd.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)hw/vfio/iommufd.c +3
-2@@ -152,9 +152,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)if (sscanf (contents,"%d:%d" , &major, &minor) !=2 ) {error_setg (errp,"failed to get major:minor for \"%s\"" , vfio_dev_path); - goto out_free_dev_path; + goto out_free_contents; } -g_free (contents); vfio_devt =makedev (major, minor); vfio_path =g_strdup_printf ("/dev/vfio/devices/%s" , dent->d_name);@@ -166,6 +165,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)trace_iommufd_cdev_getfd (vfio_path, ret);g_free (vfio_path); +out_free_contents: +g_free (contents); out_free_dev_path:g_free (vfio_dev_path); out_close_dir:
Quoted 28 lines · Cédric Le Goater
Make sure variable contents is freed if scanf fails.
Cc Eric Auger <[email protected]>Cc Yi Liu <[email protected]>Cc Zhenzhong Duan <[email protected]>Fixes CID 1540007Fixes 5ee3dc7af785 ("vfio/iommufd: Implement the iommufd backend")Signed-off-by Cédric Le Goater <[email protected]>--- hw/vfio/iommufd.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)hw/vfio/iommufd.c +1
-2@@ -152,9 +152,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)if (sscanf (contents,"%d:%d" , &major, &minor) !=2 ) {error_setg (errp,"failed to get major:minor for \"%s\"" , vfio_dev_path); - goto out_free_dev_path; + goto out_free_contents; } -g_free (contents); vfio_devt =makedev (major, minor); vfio_path =g_strdup_printf ("/dev/vfio/devices/%s" , dent->d_name);
hw/vfio/iommufd.c static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp) · line 172
Quoted 8 lines
hw/vfio/iommufd.c +2
-0@@ -166,6 +165,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)trace_iommufd_cdev_getfd (vfio_path, ret);g_free (vfio_path); +out_free_contents: +g_free (contents); out_free_dev_path:g_free (vfio_dev_path); out_close_dir:
good catch.
Signature · 2 lines
Regards, Yi Liu
Quoted 11 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Thursday, March 14, 2024 5:06 AM
To: [email protected]
Cc: Alex Williamson <[email protected]>; Cédric Le Goater <[email protected]>; Eric Auger <[email protected]>; Liu, Yi L <[email protected]>; Duan, Zhenzhong <[email protected]>
Subject: [PATCH] vfio/iommufd: Fix memory leak
Quoted 8 lines
Make sure variable contents is freed if scanf fails.
Cc Eric Auger <[email protected]>Cc Yi Liu <[email protected]>Cc Zhenzhong Duan <[email protected]>Fixes CID 1540007Fixes 5ee3dc7af785 ("vfio/iommufd: Implement the iommufd backend")Signed-off-by Cédric Le Goater <[email protected]>
Unrelated to this patch, I see there are four g_free calls, not clear if it's deserved to cleanup with g_autofree.
Thanks
Zhenzhong
Quoted 34 lines
---
hw/vfio/iommufd.c| 5 +++--1 file changed, 3 insertions(+), 2 deletions(-)hw/vfio/iommufd.c +0
-0index
a75a785e90c64cdcc4d10c88d217801b3f536cdb..cd549e0ee8573e75772c51cc96153762a6bc8550100644hw/vfio/iommufd.c +1
-2@@ -152,9 +152,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)if (sscanf (contents,"%d:%d" , &major, &minor) !=2 ) {error_setg (errp,"failed to get major:minor for \"%s\"" , vfio_dev_path); - goto out_free_dev_path; + goto out_free_contents; } -g_free (contents); vfio_devt =makedev (major, minor);vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);hw/vfio/iommufd.c +2
-0@@ -166,6 +165,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)trace_iommufd_cdev_getfd (vfio_path, ret);g_free (vfio_path); +out_free_contents: +g_free (contents);
out_free_dev_path:g_free(vfio_dev_path);
out_close_dir:
--
2.44.0
Quoted 20 lines · Zhenzhong
Quoted 11 lines
-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Thursday, March 14, 2024 5:06 AM
To: [email protected]
Cc: Alex Williamson <[email protected]>; Cédric Le Goater <[email protected]>; Eric Auger <[email protected]>; Liu, Yi L <[email protected]>; Duan, Zhenzhong <[email protected]>
Subject: [PATCH] vfio/iommufd: Fix memory leakQuoted 8 lines
Make sure variable contents is freed if scanf fails.
Cc Eric Auger <[email protected]>Cc Yi Liu <[email protected]>Cc Zhenzhong Duan <[email protected]>Fixes CID 1540007Fixes 5ee3dc7af785 ("vfio/iommufd: Implement the iommufd backend")Signed-off-by Cédric Le Goater <[email protected]>
Quoted 4 lines
Reviewed-by Zhenzhong Duan <[email protected]>Unrelated to this patch, I see there are four
g_freecalls, not clear if it's deserved to cleanup withg_autofree.
Ah yes. This is much better indeed.
Thanks,
C.