From patchwork Sat Feb 25 16:35:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikael Morin X-Patchwork-Id: 65648 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 76A4A385085A for ; Sat, 25 Feb 2023 16:35:33 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp.smtpout.orange.fr (smtp-15.smtpout.orange.fr [80.12.242.15]) by sourceware.org (Postfix) with ESMTPS id 5B30538582BC for ; Sat, 25 Feb 2023 16:35:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5B30538582BC Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=orange.fr Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=orange.fr Received: from [192.168.1.16] ([86.215.161.51]) by smtp.orange.fr with ESMTPA id VxVopCOHkkAmIVxVup75wt; Sat, 25 Feb 2023 17:35:11 +0100 X-ME-Helo: [192.168.1.16] X-ME-Auth: bW9yaW4tbWlrYWVsQG9yYW5nZS5mcg== X-ME-Date: Sat, 25 Feb 2023 17:35:11 +0100 X-ME-IP: 86.215.161.51 Message-ID: <48878e99-0ecb-3688-0c2e-db7ec69856df@orange.fr> Date: Sat, 25 Feb 2023 17:35:04 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.2 From: Mikael Morin Subject: fortran: Reuse associated_dummy memory if previously allocated [PR108923] To: gfortran , gcc-patches Cc: Harald Anlauf Content-Language: en-US X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, LOCALPART_IN_SUBJECT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hello, Harald found a testcase with memory still leaking despite my previous patch for PR108923. That patch was fixing a leak caused by absence of memory release, the attached patch fixes a leak caused by pointer overwrite. I haven't investigated why sort_actual is called several times( which causes the memory leak) nor tried to avoid that. Theoretically, one could assert that the previous associated_dummy value is the same as the one to be written (it should be the same at each sort_actual invocation), but I have preferred to silently overwrite, and fix just the memory problem. Manually tested on Harald's testcase (predcom-1.f) and ran the full fortran testsuite on x86_64-pc-linux-gnu. OK for master and 12 and 11? From 9b88208ec4130712d33d5c7ed74fc17466624a0c Mon Sep 17 00:00:00 2001 From: Mikael Morin Date: Sat, 25 Feb 2023 16:27:24 +0100 Subject: [PATCH] fortran: Reuse associated_dummy memory if previously allocated [PR108923] This avoids making the associted_dummy field point to a new memory chunk if it's already pointing somewhere, in which case doing so would leak the previously allocated chunk. gcc/fortran/ChangeLog: * intrinsic.cc (get_intrinsic_dummy_arg, set_intrinsic_dummy_arg): Rename the former to the latter. Remove the return value, add a reference to the lhs as argument, and do the pointer assignment inside the function. Don't do it if the pointer is already non-NULL. (sort_actual): Update caller. --- gcc/fortran/intrinsic.cc | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/gcc/fortran/intrinsic.cc b/gcc/fortran/intrinsic.cc index 17ee999c3b9..e69e541efe0 100644 --- a/gcc/fortran/intrinsic.cc +++ b/gcc/fortran/intrinsic.cc @@ -4259,15 +4259,14 @@ remove_nullargs (gfc_actual_arglist **ap) } -static gfc_dummy_arg * -get_intrinsic_dummy_arg (gfc_intrinsic_arg *intrinsic) +static void +set_intrinsic_dummy_arg (gfc_dummy_arg *&dummy_arg, gfc_intrinsic_arg *intrinsic) { - gfc_dummy_arg * const dummy_arg = gfc_get_dummy_arg (); + if (dummy_arg == NULL) + dummy_arg = gfc_get_dummy_arg (); dummy_arg->intrinsicness = GFC_INTRINSIC_DUMMY_ARG; dummy_arg->u.intrinsic = intrinsic; - - return dummy_arg; } @@ -4430,7 +4429,7 @@ do_sort: if (a == NULL) a = gfc_get_actual_arglist (); - a->associated_dummy = get_intrinsic_dummy_arg (f); + set_intrinsic_dummy_arg (a->associated_dummy, f); if (actual == NULL) *ap = a; -- 2.39.1