Skip to contentSkip to conversation

qga: Fix memory leak in split_list

PATCH In review Miaoqian Lin 3 participants 4 messages Oct 18, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qga: Fix memory leak in split_list

We should use g_strfreev to free the memory allocated by g_strsplit(). Use g_free() will cause a memory leak.

---
 qga/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qga/main.c +1 -1
index 5a9d8252e075..04902076b25d 100644
@@ -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;
 }
Signature · 1 line
2.25.1
#1 lore raw

Hi

Quoted 13 lines · Miaoqian Lin

We should use g_strfreev to free the memory allocated by g_strsplit(). Use g_free() will cause a memory leak.

---
 qga/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qga/main.c +0 -0
index 5a9d8252e075..04902076b25d 100644

qga/main.c static GList *split_list(const gchar *str, const gchar · line 939

Quoted 7 lines
qga/main.c +1 -1
@@ -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

#2 lore raw

Hi,

Quoted 24 lines · Marc-André Lureau

Hi

On Tue, Oct 18, 2022 at 8:47 AM Miaoqian Lin <[email protected]> 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 <[email protected]>
    ---
     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);
Quoted 2 lines

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.

Quoted 1 line

thanks

Reviewed-by on v1 1/1
#3 lore raw
In reply to Miaoqian Lin
Quoted 13 lines · Miaoqian Lin

We should use g_strfreev to free the memory allocated by g_strsplit(). Use g_free() will cause a memory leak.

---
  qga/main.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
qga/main.c +0 -0
index 5a9d8252e075..04902076b25d 100644

qga/main.c static GList *split_list(const gchar *str, const gchar *delim) · line 940

Quoted 8 lines
qga/main.c +1 -1
@@ -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")