[PATCH] qga: Fix memory leak in split_list

Miaoqian Lin posted 1 patch 1 year, 7 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20221018044645.863859-1-linmq006@gmail.com
Maintainers: Michael Roth <michael.roth@amd.com>, Konstantin Kostiuk <kkostiuk@redhat.com>
qga/main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] qga: Fix memory leak in split_list
Posted by Miaoqian Lin 1 year, 7 months ago
We should use g_strfreev to free the memory allocated by g_strsplit().
Use g_free() will cause a memory leak.

Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
 qga/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/qga/main.c b/qga/main.c
index 5a9d8252e075..04902076b25d 100644
--- a/qga/main.c
+++ b/qga/main.c
@@ -934,7 +934,7 @@ static GList *split_list(const gchar *str, const gchar *delim)
     for (i = 0; strv[i]; i++) {
         list = g_list_prepend(list, strv[i]);
     }
-    g_free(strv);
+    g_strfreev(strv);
 
     return list;
 }
-- 
2.25.1
Re: [PATCH] qga: Fix memory leak in split_list
Posted by Philippe Mathieu-Daudé 1 year, 7 months ago
On 18/10/22 06:46, Miaoqian Lin wrote:
> We should use g_strfreev to free the memory allocated by g_strsplit().
> Use g_free() will cause a memory leak.
> 
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
>   qga/main.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/qga/main.c b/qga/main.c
> index 5a9d8252e075..04902076b25d 100644
> --- a/qga/main.c
> +++ b/qga/main.c
> @@ -934,7 +934,7 @@ static GList *split_list(const gchar *str, const gchar *delim)
>       for (i = 0; strv[i]; i++) {
>           list = g_list_prepend(list, strv[i]);
>       }
> -    g_free(strv);
> +    g_strfreev(strv);
>   
>       return list;
>   }

Fixes: 4bca81ceed ("qga: make split_list() return allocated strings")
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>

Re: [PATCH] qga: Fix memory leak in split_list
Posted by Marc-André Lureau 1 year, 7 months ago
Hi

On Tue, Oct 18, 2022 at 8:47 AM Miaoqian Lin <linmq006@gmail.com> wrote:

> We should use g_strfreev to free the memory allocated by g_strsplit().
> Use g_free() will cause a memory leak.
>
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
>  qga/main.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/qga/main.c b/qga/main.c
> index 5a9d8252e075..04902076b25d 100644
> --- a/qga/main.c
> +++ b/qga/main.c
> @@ -934,7 +934,7 @@ static GList *split_list(const gchar *str, const gchar
> *delim)
>      for (i = 0; strv[i]; i++) {
>          list = g_list_prepend(list, strv[i]);
>      }
> -    g_free(strv);
> +    g_strfreev(strv);
>

No, this is intentional. We are building a list of allocated strings, we
shouldn't free those.

thanks
Re: [PATCH] qga: Fix memory leak in split_list
Posted by Miaoqian Lin 1 year, 7 months ago
Hi,

On 2022/10/18 14:32, Marc-André Lureau wrote:
> Hi
>
> On Tue, Oct 18, 2022 at 8:47 AM Miaoqian Lin <linmq006@gmail.com> wrote:
>
>     We should use g_strfreev to free the memory allocated by g_strsplit().
>     Use g_free() will cause a memory leak.
>
>     Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
>     ---
>      qga/main.c | 2 +-
>      1 file changed, 1 insertion(+), 1 deletion(-)
>
>     diff --git a/qga/main.c b/qga/main.c
>     index 5a9d8252e075..04902076b25d 100644
>     --- a/qga/main.c
>     +++ b/qga/main.c
>     @@ -934,7 +934,7 @@ static GList *split_list(const gchar *str, const gchar *delim)
>          for (i = 0; strv[i]; i++) {
>              list = g_list_prepend(list, strv[i]);
>          }
>     -    g_free(strv);
>     +    g_strfreev(strv);
>
>
> No, this is intentional. We are building a list of allocated strings, we shouldn't free those.
>  

I get it. Thanks for your response. Sorry for the mistake.


> thanks