All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/2] tests/9pfs: Fix leak and add some more g_auto* annotations
@ 2022-02-01 15:15 Greg Kurz
  2022-02-01 15:15 ` [PATCH v3 1/2] tests/9pfs: Fix leak of local_test_path Greg Kurz
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Greg Kurz @ 2022-02-01 15:15 UTC (permalink / raw)
  To: qemu-devel
  Cc: Laurent Vivier, Paolo Bonzini, Thomas Huth,
	Christian Schoenebeck, Greg Kurz

This is the continuation of:

https://lore.kernel.org/qemu-devel/20220201111137.732325b4@bahia/T/#t

v3: - fix leak in its own patch

Greg Kurz (2):
  tests/9pfs: Fix leak of local_test_path
  tests/9pfs: Use g_autofree and g_autoptr where possible

 tests/qtest/libqos/virtio-9p.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

-- 
2.34.1




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 1/2] tests/9pfs: Fix leak of local_test_path
  2022-02-01 15:15 [PATCH v3 0/2] tests/9pfs: Fix leak and add some more g_auto* annotations Greg Kurz
@ 2022-02-01 15:15 ` Greg Kurz
  2022-02-01 15:15 ` [PATCH v3 2/2] tests/9pfs: Use g_autofree and g_autoptr where possible Greg Kurz
  2022-02-02 13:31 ` [PATCH v3 0/2] tests/9pfs: Fix leak and add some more g_auto* annotations Christian Schoenebeck
  2 siblings, 0 replies; 4+ messages in thread
From: Greg Kurz @ 2022-02-01 15:15 UTC (permalink / raw)
  To: qemu-devel
  Cc: Laurent Vivier, Paolo Bonzini, Thomas Huth,
	Christian Schoenebeck, Greg Kurz

local_test_path is allocated in virtio_9p_create_local_test_dir() to hold the path
of the temporary directory. It should be freed in virtio_9p_remove_local_test_dir()
when the temporary directory is removed. Clarify the lifecycle of local_test_path
while here.

Based-on: <f6602123c6f7d0d593466231b04fba087817abbd.1642879848.git.qemu_oss@crudebyte.com>
Signed-off-by: Greg Kurz <groug@kaod.org>
---
 tests/qtest/libqos/virtio-9p.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tests/qtest/libqos/virtio-9p.c b/tests/qtest/libqos/virtio-9p.c
index ef96ef006adc..5d18e5eae544 100644
--- a/tests/qtest/libqos/virtio-9p.c
+++ b/tests/qtest/libqos/virtio-9p.c
@@ -39,8 +39,13 @@ static char *concat_path(const char* a, const char* b)
 
 void virtio_9p_create_local_test_dir(void)
 {
+    g_assert(local_test_path == NULL);
     struct stat st;
     char *pwd = g_get_current_dir();
+    /*
+     * template gets cached into local_test_path and freed in
+     * virtio_9p_remove_local_test_dir().
+     */
     char *template = concat_path(pwd, "qtest-9p-local-XXXXXX");
 
     local_test_path = mkdtemp(template);
@@ -66,6 +71,8 @@ void virtio_9p_remove_local_test_dir(void)
         /* ignore error, dummy check to prevent compiler error */
     }
     g_free(cmd);
+    g_free(local_test_path);
+    local_test_path = NULL;
 }
 
 char *virtio_9p_test_path(const char *path)
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 2/2] tests/9pfs: Use g_autofree and g_autoptr where possible
  2022-02-01 15:15 [PATCH v3 0/2] tests/9pfs: Fix leak and add some more g_auto* annotations Greg Kurz
  2022-02-01 15:15 ` [PATCH v3 1/2] tests/9pfs: Fix leak of local_test_path Greg Kurz
@ 2022-02-01 15:15 ` Greg Kurz
  2022-02-02 13:31 ` [PATCH v3 0/2] tests/9pfs: Fix leak and add some more g_auto* annotations Christian Schoenebeck
  2 siblings, 0 replies; 4+ messages in thread
From: Greg Kurz @ 2022-02-01 15:15 UTC (permalink / raw)
  To: qemu-devel
  Cc: Laurent Vivier, Paolo Bonzini, Thomas Huth,
	Christian Schoenebeck, Greg Kurz

It is recommended to use g_autofree or g_autoptr as it reduces
the odds of introducing memory leaks in future changes.

Signed-off-by: Greg Kurz <groug@kaod.org>
---
 tests/qtest/libqos/virtio-9p.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/tests/qtest/libqos/virtio-9p.c b/tests/qtest/libqos/virtio-9p.c
index 5d18e5eae544..f51f0635cc0c 100644
--- a/tests/qtest/libqos/virtio-9p.c
+++ b/tests/qtest/libqos/virtio-9p.c
@@ -41,7 +41,7 @@ void virtio_9p_create_local_test_dir(void)
 {
     g_assert(local_test_path == NULL);
     struct stat st;
-    char *pwd = g_get_current_dir();
+    g_autofree char *pwd = g_get_current_dir();
     /*
      * template gets cached into local_test_path and freed in
      * virtio_9p_remove_local_test_dir().
@@ -52,7 +52,6 @@ void virtio_9p_create_local_test_dir(void)
     if (!local_test_path) {
         g_test_message("mkdtemp('%s') failed: %s", template, strerror(errno));
     }
-    g_free(pwd);
 
     g_assert(local_test_path != NULL);
 
@@ -65,12 +64,11 @@ void virtio_9p_create_local_test_dir(void)
 void virtio_9p_remove_local_test_dir(void)
 {
     g_assert(local_test_path != NULL);
-    char *cmd = g_strdup_printf("rm -fr '%s'\n", local_test_path);
+    g_autofree char *cmd = g_strdup_printf("rm -fr '%s'\n", local_test_path);
     int res = system(cmd);
     if (res < 0) {
         /* ignore error, dummy check to prevent compiler error */
     }
-    g_free(cmd);
     g_free(local_test_path);
     local_test_path = NULL;
 }
@@ -216,8 +214,8 @@ static void *virtio_9p_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
 static void regex_replace(GString *haystack, const char *pattern,
                           const char *replace_fmt, ...)
 {
-    GRegex *regex;
-    char *replace, *s;
+    g_autoptr(GRegex) regex = NULL;
+    g_autofree char *replace = NULL, *s = NULL;
     va_list argp;
 
     va_start(argp, replace_fmt);
@@ -227,9 +225,6 @@ static void regex_replace(GString *haystack, const char *pattern,
     regex = g_regex_new(pattern, 0, 0, NULL);
     s = g_regex_replace(regex, haystack->str, -1, 0, replace, 0, NULL);
     g_string_assign(haystack, s);
-    g_free(s);
-    g_regex_unref(regex);
-    g_free(replace);
 }
 
 void virtio_9p_assign_local_driver(GString *cmd_line, const char *args)
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3 0/2] tests/9pfs: Fix leak and add some more g_auto* annotations
  2022-02-01 15:15 [PATCH v3 0/2] tests/9pfs: Fix leak and add some more g_auto* annotations Greg Kurz
  2022-02-01 15:15 ` [PATCH v3 1/2] tests/9pfs: Fix leak of local_test_path Greg Kurz
  2022-02-01 15:15 ` [PATCH v3 2/2] tests/9pfs: Use g_autofree and g_autoptr where possible Greg Kurz
@ 2022-02-02 13:31 ` Christian Schoenebeck
  2 siblings, 0 replies; 4+ messages in thread
From: Christian Schoenebeck @ 2022-02-02 13:31 UTC (permalink / raw)
  To: qemu-devel; +Cc: Greg Kurz, Laurent Vivier, Paolo Bonzini, Thomas Huth

On Dienstag, 1. Februar 2022 16:15:06 CET Greg Kurz wrote:
> This is the continuation of:
> 
> https://lore.kernel.org/qemu-devel/20220201111137.732325b4@bahia/T/#t
> 
> v3: - fix leak in its own patch
> 
> Greg Kurz (2):
>   tests/9pfs: Fix leak of local_test_path
>   tests/9pfs: Use g_autofree and g_autoptr where possible
> 
>  tests/qtest/libqos/virtio-9p.c | 20 +++++++++++---------
>  1 file changed, 11 insertions(+), 9 deletions(-)
> 
> -- 
> 2.34.1
> 

Queued on 9p.next:
https://github.com/cschoenebeck/qemu/commits/9p.next

Thanks!

Best regards,
Christian Schoenebeck




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-02 13:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-01 15:15 [PATCH v3 0/2] tests/9pfs: Fix leak and add some more g_auto* annotations Greg Kurz
2022-02-01 15:15 ` [PATCH v3 1/2] tests/9pfs: Fix leak of local_test_path Greg Kurz
2022-02-01 15:15 ` [PATCH v3 2/2] tests/9pfs: Use g_autofree and g_autoptr where possible Greg Kurz
2022-02-02 13:31 ` [PATCH v3 0/2] tests/9pfs: Fix leak and add some more g_auto* annotations Christian Schoenebeck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.