qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kurz <groug@kaod.org>
To: qemu-devel@nongnu.org
Cc: Laurent Vivier <lvivier@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Thomas Huth <thuth@redhat.com>,
	Christian Schoenebeck <qemu_oss@crudebyte.com>,
	Greg Kurz <groug@kaod.org>
Subject: [PATCH v2] tests/9pfs: Use g_autofree and g_autoptr where possible
Date: Mon, 31 Jan 2022 17:39:30 +0100	[thread overview]
Message-ID: <20220131163930.61521-1-groug@kaod.org> (raw)

It is recommended to use g_autofree or g_autoptr as it reduces
the odds of introducing memory leaks in future changes. Let's do
that everywhere a glib allocation is performed.

The virtio_9p_create_local_test_dir() function needs some extra
care though : the template pointer is eventually cached into the
local_test_path global variable for the duration of the tests and
should not be freed. Add the g_autofree annotation but negate it
with g_steal_pointer() to make it clear that the pointer ownership
is dropped on purpose.

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

diff --git a/tests/qtest/libqos/virtio-9p.c b/tests/qtest/libqos/virtio-9p.c
index ef96ef006adc..f0ffbc583492 100644
--- a/tests/qtest/libqos/virtio-9p.c
+++ b/tests/qtest/libqos/virtio-9p.c
@@ -37,18 +37,22 @@ static char *concat_path(const char* a, const char* b)
     return g_build_filename(a, b, NULL);
 }
 
-void virtio_9p_create_local_test_dir(void)
+static char *make_temp_dir(char *template)
 {
-    struct stat st;
-    char *pwd = g_get_current_dir();
-    char *template = concat_path(pwd, "qtest-9p-local-XXXXXX");
-
-    local_test_path = mkdtemp(template);
-    if (!local_test_path) {
+    char *path = mkdtemp(template);
+    if (!path) {
         g_test_message("mkdtemp('%s') failed: %s", template, strerror(errno));
     }
-    g_free(pwd);
+    return path;
+}
+
+void virtio_9p_create_local_test_dir(void)
+{
+    g_autofree char *pwd = g_get_current_dir();
+    g_autofree char *template = concat_path(pwd, "qtest-9p-local-XXXXXX");
+    struct stat st;
 
+    local_test_path = make_temp_dir(g_steal_pointer(&template));
     g_assert(local_test_path != NULL);
 
     /* ensure test directory exists now ... */
@@ -60,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);
 }
 
 char *virtio_9p_test_path(const char *path)
@@ -209,8 +212,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);
@@ -220,9 +223,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



             reply	other threads:[~2022-01-31 16:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-31 16:39 Greg Kurz [this message]
2022-01-31 17:43 ` [PATCH v2] tests/9pfs: Use g_autofree and g_autoptr where possible Christian Schoenebeck
2022-02-01 10:11   ` Greg Kurz
2022-02-01 13:00     ` Christian Schoenebeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220131163930.61521-1-groug@kaod.org \
    --to=groug@kaod.org \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu_oss@crudebyte.com \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).