diff mbox series

[v2,01/11] vfio/pci: Use g_autofree in vfio_realize

Message ID 20240507064252.457884-2-zhenzhong.duan@intel.com (mailing list archive)
State New, archived
Headers show
Series VFIO: misc cleanups | expand

Commit Message

Duan, Zhenzhong May 7, 2024, 6:42 a.m. UTC
Local pointer name is allocated before vfio_attach_device() call
and freed after the call.

Same for tmp when calling realpath().

Use 'g_autofree' to avoid the g_free() calls.

Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
 hw/vfio/pci.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

Comments

Cédric Le Goater May 14, 2024, 3:45 p.m. UTC | #1
On 5/7/24 08:42, Zhenzhong Duan wrote:
> Local pointer name is allocated before vfio_attach_device() call
> and freed after the call.
> 
> Same for tmp when calling realpath().
> 
> Use 'g_autofree' to avoid the g_free() calls.
> 
> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>


Reviewed-by: Cédric Le Goater <clg@redhat.com>

Thanks,

C.


> ---
>   hw/vfio/pci.c | 7 +++----
>   1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index 64780d1b79..576b21e2bb 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -2946,12 +2946,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
>       ERRP_GUARD();
>       VFIOPCIDevice *vdev = VFIO_PCI(pdev);
>       VFIODevice *vbasedev = &vdev->vbasedev;
> -    char *tmp, *subsys;
> +    char *subsys;
>       Error *err = NULL;
>       int i, ret;
>       bool is_mdev;
>       char uuid[UUID_STR_LEN];
> -    char *name;
> +    g_autofree char *name = NULL;
> +    g_autofree char *tmp = NULL;
>   
>       if (vbasedev->fd < 0 && !vbasedev->sysfsdev) {
>           if (!(~vdev->host.domain || ~vdev->host.bus ||
> @@ -2982,7 +2983,6 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
>        */
>       tmp = g_strdup_printf("%s/subsystem", vbasedev->sysfsdev);
>       subsys = realpath(tmp, NULL);
> -    g_free(tmp);
>       is_mdev = subsys && (strcmp(subsys, "/sys/bus/mdev") == 0);
>       free(subsys);
>   
> @@ -3003,7 +3003,6 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
>   
>       ret = vfio_attach_device(name, vbasedev,
>                                pci_device_iommu_address_space(pdev), errp);
> -    g_free(name);
>       if (ret) {
>           goto error;
>       }
diff mbox series

Patch

diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 64780d1b79..576b21e2bb 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -2946,12 +2946,13 @@  static void vfio_realize(PCIDevice *pdev, Error **errp)
     ERRP_GUARD();
     VFIOPCIDevice *vdev = VFIO_PCI(pdev);
     VFIODevice *vbasedev = &vdev->vbasedev;
-    char *tmp, *subsys;
+    char *subsys;
     Error *err = NULL;
     int i, ret;
     bool is_mdev;
     char uuid[UUID_STR_LEN];
-    char *name;
+    g_autofree char *name = NULL;
+    g_autofree char *tmp = NULL;
 
     if (vbasedev->fd < 0 && !vbasedev->sysfsdev) {
         if (!(~vdev->host.domain || ~vdev->host.bus ||
@@ -2982,7 +2983,6 @@  static void vfio_realize(PCIDevice *pdev, Error **errp)
      */
     tmp = g_strdup_printf("%s/subsystem", vbasedev->sysfsdev);
     subsys = realpath(tmp, NULL);
-    g_free(tmp);
     is_mdev = subsys && (strcmp(subsys, "/sys/bus/mdev") == 0);
     free(subsys);
 
@@ -3003,7 +3003,6 @@  static void vfio_realize(PCIDevice *pdev, Error **errp)
 
     ret = vfio_attach_device(name, vbasedev,
                              pci_device_iommu_address_space(pdev), errp);
-    g_free(name);
     if (ret) {
         goto error;
     }