[PATCH 2/3] virNetDevVlanParse: Use g_autofree for temporary variables

Peter Krempa via Devel posted 3 patches 1 week ago
[PATCH 2/3] virNetDevVlanParse: Use g_autofree for temporary variables
Posted by Peter Krempa via Devel 1 week ago
From: Peter Krempa <pkrempa@redhat.com>

Automatically free the variables to prevent leaks when returning from
middle of the function.

Fixes: 1de6fd5edb5
Closes: https://gitlab.com/libvirt/libvirt/-/issues/824
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/conf/netdev_vlan_conf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/src/conf/netdev_vlan_conf.c b/src/conf/netdev_vlan_conf.c
index b98c4d92cf..012a28034e 100644
--- a/src/conf/netdev_vlan_conf.c
+++ b/src/conf/netdev_vlan_conf.c
@@ -34,8 +34,8 @@ virNetDevVlanParse(xmlNodePtr node, xmlXPathContextPtr ctxt, virNetDevVlan *def)
 {
     int ret = -1;
     VIR_XPATH_NODE_AUTORESTORE(ctxt)
-    char *trunk = NULL;
-    xmlNodePtr *tagNodes = NULL;
+    g_autofree char *trunk = NULL;
+    g_autofree xmlNodePtr *tagNodes = NULL;
     int nTags;
     size_t i;

@@ -120,8 +120,6 @@ virNetDevVlanParse(xmlNodePtr node, xmlXPathContextPtr ctxt, virNetDevVlan *def)

     ret = 0;
  cleanup:
-    VIR_FREE(tagNodes);
-    VIR_FREE(trunk);
     return ret;
 }

-- 
2.51.0
Re: [PATCH 2/3] virNetDevVlanParse: Use g_autofree for temporary variables
Posted by Michal Prívozník via Devel 1 week ago
On 10/20/25 15:21, Peter Krempa via Devel wrote:
> From: Peter Krempa <pkrempa@redhat.com>
> 
> Automatically free the variables to prevent leaks when returning from
> middle of the function.
> 
> Fixes: 1de6fd5edb5
> Closes: https://gitlab.com/libvirt/libvirt/-/issues/824
> Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> ---
>  src/conf/netdev_vlan_conf.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/src/conf/netdev_vlan_conf.c b/src/conf/netdev_vlan_conf.c
> index b98c4d92cf..012a28034e 100644
> --- a/src/conf/netdev_vlan_conf.c
> +++ b/src/conf/netdev_vlan_conf.c
> @@ -34,8 +34,8 @@ virNetDevVlanParse(xmlNodePtr node, xmlXPathContextPtr ctxt, virNetDevVlan *def)
>  {
>      int ret = -1;
>      VIR_XPATH_NODE_AUTORESTORE(ctxt)
> -    char *trunk = NULL;
> -    xmlNodePtr *tagNodes = NULL;
> +    g_autofree char *trunk = NULL;
> +    g_autofree xmlNodePtr *tagNodes = NULL;
>      int nTags;
>      size_t i;
> 
> @@ -120,8 +120,6 @@ virNetDevVlanParse(xmlNodePtr node, xmlXPathContextPtr ctxt, virNetDevVlan *def)
> 
>      ret = 0;
>   cleanup:
> -    VIR_FREE(tagNodes);
> -    VIR_FREE(trunk);
>      return ret;
>  }
> 

Ah, took me a while to realize what the actual problem is. There's one
'return -1' call which should have been 'goto cleanup' instead.

Michal