[PATCH] util: json: fix memory leak in virJSONValueFromJsonC()

Elizaveta Tereshkina posted 1 patch 2 weeks, 3 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20260306104620.155169-1-teryoshkina.ea@gmail.com
src/util/virjson.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] util: json: fix memory leak in virJSONValueFromJsonC()
Posted by Elizaveta Tereshkina 2 weeks, 3 days ago
In the 'json_type_array' case, the error path uses 'g_free()',
which doesn't release other nested memory allocations.
Replace it with 'virJSONValueFree()' to properly free
the entire 'virJSONValue' structure.

Fixes: da66bf53b0 (util: json: check return value of virJSONValueFromJsonC)
Signed-off-by: Elizaveta Tereshkina <teryoshkina.ea@gmail.com>
---
 src/util/virjson.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/util/virjson.c b/src/util/virjson.c
index 454bd657be..72bd78453a 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -1438,7 +1438,7 @@ virJSONValueFromJsonC(json_object *jobj)
             cur = virJSONValueFromJsonC(val);
 
             if (!cur) {
-                g_free(ret);
+                virJSONValueFree(ret);
                 return NULL;
             }
 
-- 
2.39.2
Re: [PATCH] util: json: fix memory leak in virJSONValueFromJsonC()
Posted by Michal Prívozník via Devel 2 weeks, 3 days ago
On 3/6/26 11:46, Elizaveta Tereshkina wrote:
> In the 'json_type_array' case, the error path uses 'g_free()',
> which doesn't release other nested memory allocations.
> Replace it with 'virJSONValueFree()' to properly free
> the entire 'virJSONValue' structure.
> 
> Fixes: da66bf53b0 (util: json: check return value of virJSONValueFromJsonC)
> Signed-off-by: Elizaveta Tereshkina <teryoshkina.ea@gmail.com>
> ---
>  src/util/virjson.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/src/util/virjson.c b/src/util/virjson.c
> index 454bd657be..72bd78453a 100644
> --- a/src/util/virjson.c
> +++ b/src/util/virjson.c
> @@ -1438,7 +1438,7 @@ virJSONValueFromJsonC(json_object *jobj)
>              cur = virJSONValueFromJsonC(val);
>  
>              if (!cur) {
> -                g_free(ret);
> +                virJSONValueFree(ret);
>                  return NULL;
>              }
>  

There's one more occurrence under 'json_type_object' case. I'll fix that
too.

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

and merged. Congratulations on your first libvirt contribution!

Michal