From nobody Fri May 17 08:24:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1643734668267787.0216730576848; Tue, 1 Feb 2022 08:57:48 -0800 (PST) Received: from localhost ([::1]:48018 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nEwTS-0007ii-DA for importer@patchew.org; Tue, 01 Feb 2022 11:57:46 -0500 Received: from eggs.gnu.org ([209.51.188.92]:41706) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEusw-0004e9-Ak for qemu-devel@nongnu.org; Tue, 01 Feb 2022 10:16:01 -0500 Received: from us-smtp-delivery-44.mimecast.com ([205.139.111.44]:20707) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEuss-00028s-GG for qemu-devel@nongnu.org; Tue, 01 Feb 2022 10:15:57 -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-60-CNwASMpTP8qkyXXOG2zjgw-1; Tue, 01 Feb 2022 10:15:37 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A90941898292; Tue, 1 Feb 2022 15:15:36 +0000 (UTC) Received: from bahia.redhat.com (unknown [10.39.192.203]) by smtp.corp.redhat.com (Postfix) with ESMTP id 35DFF10A48CB; Tue, 1 Feb 2022 15:15:35 +0000 (UTC) X-MC-Unique: CNwASMpTP8qkyXXOG2zjgw-1 From: Greg Kurz To: qemu-devel@nongnu.org Subject: [PATCH v3 1/2] tests/9pfs: Fix leak of local_test_path Date: Tue, 1 Feb 2022 16:15:07 +0100 Message-Id: <20220201151508.190035-2-groug@kaod.org> In-Reply-To: <20220201151508.190035-1-groug@kaod.org> References: <20220201151508.190035-1-groug@kaod.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kaod.org Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: softfail client-ip=205.139.111.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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1643734670442100001 Content-Type: text/plain; charset="utf-8" local_test_path is allocated in virtio_9p_create_local_test_dir() to hold t= he path of the temporary directory. It should be freed in virtio_9p_remove_local_te= st_dir() when the temporary directory is removed. Clarify the lifecycle of local_tes= t_path while here. Based-on: Signed-off-by: Greg Kurz --- 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) =20 void virtio_9p_create_local_test_dir(void) { + g_assert(local_test_path =3D=3D NULL); struct stat st; char *pwd =3D g_get_current_dir(); + /* + * template gets cached into local_test_path and freed in + * virtio_9p_remove_local_test_dir(). + */ char *template =3D concat_path(pwd, "qtest-9p-local-XXXXXX"); =20 local_test_path =3D 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 =3D NULL; } =20 char *virtio_9p_test_path(const char *path) --=20 2.34.1 From nobody Fri May 17 08:24:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1643735150205653.2025175415023; Tue, 1 Feb 2022 09:05:50 -0800 (PST) Received: from localhost ([::1]:56424 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nEwbE-0005Gj-Lv for importer@patchew.org; Tue, 01 Feb 2022 12:05:48 -0500 Received: from eggs.gnu.org ([209.51.188.92]:41780) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEusy-0004eC-GA for qemu-devel@nongnu.org; Tue, 01 Feb 2022 10:16:04 -0500 Received: from us-smtp-delivery-44.mimecast.com ([205.139.111.44]:36810) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEusu-00029e-5O for qemu-devel@nongnu.org; Tue, 01 Feb 2022 10:16:00 -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-364-8SbFdvbPN3WRtlrznFAQ_g-1; Tue, 01 Feb 2022 10:15:39 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 50EAF51082; Tue, 1 Feb 2022 15:15:38 +0000 (UTC) Received: from bahia.redhat.com (unknown [10.39.192.203]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0177810A4B2B; Tue, 1 Feb 2022 15:15:36 +0000 (UTC) X-MC-Unique: 8SbFdvbPN3WRtlrznFAQ_g-1 From: Greg Kurz To: qemu-devel@nongnu.org Subject: [PATCH v3 2/2] tests/9pfs: Use g_autofree and g_autoptr where possible Date: Tue, 1 Feb 2022 16:15:08 +0100 Message-Id: <20220201151508.190035-3-groug@kaod.org> In-Reply-To: <20220201151508.190035-1-groug@kaod.org> References: <20220201151508.190035-1-groug@kaod.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=groug@kaod.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kaod.org Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: softfail client-ip=205.139.111.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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1643735154003100001 Content-Type: text/plain; charset="utf-8" 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 --- 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 =3D=3D NULL); struct stat st; - char *pwd =3D g_get_current_dir(); + g_autofree char *pwd =3D 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(errn= o)); } - g_free(pwd); =20 g_assert(local_test_path !=3D NULL); =20 @@ -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 !=3D NULL); - char *cmd =3D g_strdup_printf("rm -fr '%s'\n", local_test_path); + g_autofree char *cmd =3D g_strdup_printf("rm -fr '%s'\n", local_test_p= ath); int res =3D system(cmd); if (res < 0) { /* ignore error, dummy check to prevent compiler error */ } - g_free(cmd); g_free(local_test_path); local_test_path =3D NULL; } @@ -216,8 +214,8 @@ static void *virtio_9p_pci_create(void *pci_bus, QGuest= Allocator *t_alloc, static void regex_replace(GString *haystack, const char *pattern, const char *replace_fmt, ...) { - GRegex *regex; - char *replace, *s; + g_autoptr(GRegex) regex =3D NULL; + g_autofree char *replace =3D NULL, *s =3D NULL; va_list argp; =20 va_start(argp, replace_fmt); @@ -227,9 +225,6 @@ static void regex_replace(GString *haystack, const char= *pattern, regex =3D g_regex_new(pattern, 0, 0, NULL); s =3D 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); } =20 void virtio_9p_assign_local_driver(GString *cmd_line, const char *args) --=20 2.34.1