Skip to contentSkip to conversation

cutils: fix memory leak in get_relocated_path()

PATCH Applied Stefano Garzarella 5 participants 8 messages Apr 12, 2021 – May 13, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cutils: fix memory leak in get_relocated_path()

get_relocated_path() allocates a GString object and returns the character data (C string) to the caller without freeing the memory allocated for that object as reported by valgrind:

24 bytes in 1 blocks are definitely lost in loss record 2,805 of 6,532

     at 0x4839809: malloc (vg_replace_malloc.c:307)
     by 0x55AABB8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C2481: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C4827: g_string_sized_new (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C4CEA: g_string_new (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x906314: get_relocated_path (cutils.c:1036)
     by 0x6E1F77: qemu_read_default_config_file (vl.c:2122)
     by 0x6E1F77: qemu_init (vl.c:2687)
     by 0x3E3AF8: main (main.c:49)

Let's use g_string_free(gstring, false) to free only the GString object and transfer the ownership of the character data to the caller.

Fixes f4f5ed2cbd ("cutils: introduce get_relocated_path")
---
 util/cutils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/cutils.c +1 -1
index ee908486da..c9b91e7535 100644
@@ -1055,5 +1055,5 @@ char *get_relocated_path(const char *dir)
         assert(G_IS_DIR_SEPARATOR(dir[-1]));
         g_string_append(result, dir - 1);
     }
-    return result->str;
+    return g_string_free(result, false);
 }
Signature · 1 line
2.30.2
#1 lore raw

Is this fix aiming at 6.0 release?

Quoted 36 lines · Stefano Garzarella

get_relocated_path() allocates a GString object and returns the character data (C string) to the caller without freeing the memory allocated for that object as reported by valgrind:

24 bytes in 1 blocks are definitely lost in loss record 2,805 of 6,532

     at 0x4839809: malloc (vg_replace_malloc.c:307)
     by 0x55AABB8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C2481: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C4827: g_string_sized_new (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C4CEA: g_string_new (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x906314: get_relocated_path (cutils.c:1036)
     by 0x6E1F77: qemu_read_default_config_file (vl.c:2122)
     by 0x6E1F77: qemu_init (vl.c:2687)
     by 0x3E3AF8: main (main.c:49)

Let's use g_string_free(gstring, false) to free only the GString object and transfer the ownership of the character data to the caller.

Fixes f4f5ed2cbd ("cutils: introduce get_relocated_path")
---
 util/cutils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/cutils.c +1 -1
index ee908486da..c9b91e7535 100644
@@ -1055,5 +1055,5 @@ char *get_relocated_path(const char *dir)
         assert(G_IS_DIR_SEPARATOR(dir[-1]));
         g_string_append(result, dir - 1);
     }
-    return result->str;
+    return g_string_free(result, false);
 }
#2 lore raw
Quoted 1 line · Philippe Mathieu-Daudé

Is this fix aiming at 6.0 release?

The leak is minimal, but the fix is very simple.
So, I think it can go if someone has a pull request to send with other patches, but I'm not sure with which tree.

Thanks,
Stefano

Quoted 39 lines · Stefano Garzarella
Quoted 36 lines

get_relocated_path() allocates a GString object and returns the character data (C string) to the caller without freeing the memory allocated for that object as reported by valgrind:

24 bytes in 1 blocks are definitely lost in loss record 2,805 of 6,532

     at 0x4839809: malloc (vg_replace_malloc.c:307)
     by 0x55AABB8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C2481: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C4827: g_string_sized_new (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C4CEA: g_string_new (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x906314: get_relocated_path (cutils.c:1036)
     by 0x6E1F77: qemu_read_default_config_file (vl.c:2122)
     by 0x6E1F77: qemu_init (vl.c:2687)
     by 0x3E3AF8: main (main.c:49)

Let's use g_string_free(gstring, false) to free only the GString object and transfer the ownership of the character data to the caller.

Fixes f4f5ed2cbd ("cutils: introduce get_relocated_path")
---
 util/cutils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/cutils.c +1 -1
index ee908486da..c9b91e7535 100644
@@ -1055,5 +1055,5 @@ char *get_relocated_path(const char *dir)
         assert(G_IS_DIR_SEPARATOR(dir[-1]));
         g_string_append(result, dir - 1);
     }
-    return result->str;
+    return g_string_free(result, false);
 }
#3 lore raw
Quoted 6 lines · Stefano Garzarella
Quoted 1 line · Philippe Mathieu-Daudé

Is this fix aiming at 6.0 release?

The leak is minimal, but the fix is very simple.
So, I think it can go if someone has a pull request to send with other patches, but I'm not sure with which tree.

I'd say Paolo...

Cc'ing Daniel/Marc-André who have a good GLib understanding.

Quoted 45 lines

Thanks,
Stefano

Quoted 40 lines · Stefano Garzarella
Quoted 37 lines

get_relocated_path() allocates a GString object and returns the character data (C string) to the caller without freeing the memory allocated for that object as reported by valgrind:

  24 bytes in 1 blocks are definitely lost in loss record 2,805 of 6,532 at 0x4839809: malloc (vg_replace_malloc.c:307)
     by 0x55AABB8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.8) by 0x55C2481: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.6600.8) by 0x55C4827: g_string_sized_new (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C4CEA: g_string_new (in /usr/lib64/libglib-2.0.so.0.6600.8) by 0x906314: get_relocated_path (cutils.c:1036)
     by 0x6E1F77: qemu_read_default_config_file (vl.c:2122) by 0x6E1F77: qemu_init (vl.c:2687)
     by 0x3E3AF8: main (main.c:49)

Let's use g_string_free(gstring, false) to free only the GString object and transfer the ownership of the character data to the caller.

Fixes f4f5ed2cbd ("cutils: introduce get_relocated_path")

---
 util/cutils.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
util/cutils.c +1 -1
index ee908486da..c9b91e7535 100644
@@ -1055,5 +1055,5 @@ char *get_relocated_path(const char *dir)
          assert(G_IS_DIR_SEPARATOR(dir[-1]));
          g_string_append(result, dir - 1);
      }
-    return result->str;
+    return g_string_free(result, false);
  }
Reviewed-by on v1 1/1
#4 lore raw
In reply to Stefano Garzarella
Quoted 16 lines · Stefano Garzarella

get_relocated_path() allocates a GString object and returns the character data (C string) to the caller without freeing the memory allocated for that object as reported by valgrind:

24 bytes in 1 blocks are definitely lost in loss record 2,805 of 6,532

     at 0x4839809: malloc (vg_replace_malloc.c:307)
     by 0x55AABB8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C2481: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C4827: g_string_sized_new (in
/usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x55C4CEA: g_string_new (in /usr/lib64/libglib-2.0.so.0.6600.8)
     by 0x906314: get_relocated_path (cutils.c:1036)
     by 0x6E1F77: qemu_read_default_config_file (vl.c:2122)
     by 0x6E1F77: qemu_init (vl.c:2687)
     by 0x3E3AF8: main (main.c:49)
Quoted 6 lines

Let's use g_string_free(gstring, false) to free only the GString object and transfer the ownership of the character data to the caller.

Fixes f4f5ed2cbd ("cutils: introduce get_relocated_path")

---

Quoted 19 lines
 util/cutils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/cutils.c +1 -2
index ee908486da..c9b91e7535 100644
@@ -1055,5 +1055,5 @@ char *get_relocated_path(const char *dir)
         assert(G_IS_DIR_SEPARATOR(dir[-1]));
         g_string_append(result, dir - 1);
     }
-    return result->str;
+    return g_string_free(result, false);
 }
--

2.30.2

Signature · 1 line
Marc-André Lureau
#5 lore raw
In reply to Stefano Garzarella

Ping :-)

Should I resend for 6.1?

Thanks
Stefano

Quoted 39 lines · Stefano Garzarella

get_relocated_path() allocates a GString object and returns the character data (C string) to the caller without freeing the memory allocated for that object as reported by valgrind:

24 bytes in 1 blocks are definitely lost in loss record 2,805 of 6,532

    at 0x4839809: malloc (vg_replace_malloc.c:307)
    by 0x55AABB8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
    by 0x55C2481: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
    by 0x55C4827: g_string_sized_new (in /usr/lib64/libglib-2.0.so.0.6600.8)
    by 0x55C4CEA: g_string_new (in /usr/lib64/libglib-2.0.so.0.6600.8)
    by 0x906314: get_relocated_path (cutils.c:1036)
    by 0x6E1F77: qemu_read_default_config_file (vl.c:2122)
    by 0x6E1F77: qemu_init (vl.c:2687)
    by 0x3E3AF8: main (main.c:49)

Let's use g_string_free(gstring, false) to free only the GString object and transfer the ownership of the character data to the caller.

Fixes f4f5ed2cbd ("cutils: introduce get_relocated_path")

---
util/cutils.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
util/cutils.c +1 -1
index ee908486da..c9b91e7535 100644
@@ -1055,5 +1055,5 @@ char *get_relocated_path(const char *dir)
        assert(G_IS_DIR_SEPARATOR(dir[-1]));
        g_string_append(result, dir - 1);
    }
-    return result->str;
+    return g_string_free(result, false);
 }
Signature · 1 line
2.30.2
#7 lore raw

Le 11/05/2021 à 07:38, Markus Armbruster a écrit :

Quoted 12 lines
Quoted 3 lines · Stefano Garzarella

Ping :-)

Should I resend for 6.1?

I'm cc'ing qemu-trivial.

For good measure:

Applied to my trivial-patches branch.

Thanks,
Laurent