From patchwork Mon Jan 31 16:39:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kurz X-Patchwork-Id: 1586882 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JnYv812rQz9s8q for ; Tue, 1 Feb 2022 03:49:38 +1100 (AEDT) Received: from localhost ([::1]:44404 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nEZrz-0006A7-3Y for incoming@patchwork.ozlabs.org; Mon, 31 Jan 2022 11:49:35 -0500 Received: from eggs.gnu.org ([209.51.188.92]:42208) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEZix-00042C-AB for qemu-devel@nongnu.org; Mon, 31 Jan 2022 11:40:15 -0500 Received: from us-smtp-delivery-44.mimecast.com ([207.211.30.44]:40945) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEZiu-0002Zh-CY for qemu-devel@nongnu.org; Mon, 31 Jan 2022 11:40:14 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-617-7t4VR_IAMTanG6_Ka8Mecw-1; Mon, 31 Jan 2022 11:39:59 -0500 X-MC-Unique: 7t4VR_IAMTanG6_Ka8Mecw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A0F341006AA0; Mon, 31 Jan 2022 16:39:57 +0000 (UTC) Received: from bahia.redhat.com (unknown [10.39.196.42]) by smtp.corp.redhat.com (Postfix) with ESMTP id EC35D84D2D; Mon, 31 Jan 2022 16:39:31 +0000 (UTC) From: Greg Kurz To: qemu-devel@nongnu.org Subject: [PATCH v2] tests/9pfs: Use g_autofree and g_autoptr where possible Date: Mon, 31 Jan 2022 17:39:30 +0100 Message-Id: <20220131163930.61521-1-groug@kaod.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kaod.org Received-SPF: softfail client-ip=207.211.30.44; envelope-from=groug@kaod.org; helo=us-smtp-delivery-44.mimecast.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_SOFTFAIL=0.665, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Paolo Bonzini , Thomas Huth , Christian Schoenebeck , Greg Kurz Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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: Signed-off-by: Greg Kurz --- 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)