From patchwork Tue Feb 1 15:15:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kurz X-Patchwork-Id: 12732032 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 41501C433EF for ; Tue, 1 Feb 2022 16:57:35 +0000 (UTC) Received: from localhost ([::1]:48018 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nEwTG-0007ii-3o for qemu-devel@archiver.kernel.org; Tue, 01 Feb 2022 11:57:34 -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 X-MC-Unique: CNwASMpTP8qkyXXOG2zjgw-1 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) 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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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: 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) 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) From patchwork Tue Feb 1 15:15:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kurz X-Patchwork-Id: 12732040 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 40318C433F5 for ; Tue, 1 Feb 2022 17:05:05 +0000 (UTC) Received: from localhost ([::1]:56424 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nEwaW-0005Gj-23 for qemu-devel@archiver.kernel.org; Tue, 01 Feb 2022 12:05:04 -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 X-MC-Unique: 8SbFdvbPN3WRtlrznFAQ_g-1 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) 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 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+qemu-devel=archiver.kernel.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. 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 == 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)