Fortran: fix minor front-end memleaks

Message ID trinity-18b11752-b516-43d2-8f80-6f5cc6d2f36b-1732650686165@trinity-msg-rest-gmx-gmx-live-5cd5dd5458-mb7dr
State New
Headers
Series Fortran: fix minor front-end memleaks |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed

Commit Message

Harald Anlauf Nov. 26, 2024, 7:51 p.m. UTC
  Dear all,

the attached patch fixes two minor front-end memleaks I saw when working
on recent PRs (pr117774 is one of them) and running f951 under valgrind.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald
  

Comments

Paul Richard Thomas Nov. 26, 2024, 8:09 p.m. UTC | #1
Hi Harald,

Looks good to me.

Thanks

Paul

On Tue, 26 Nov 2024 at 19:51, Harald Anlauf <anlauf@gmx.de> wrote:

> Dear all,
>
> the attached patch fixes two minor front-end memleaks I saw when working
> on recent PRs (pr117774 is one of them) and running f951 under valgrind.
>
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>
> Thanks,
> Harald
>
>
  
Harald Anlauf Nov. 26, 2024, 10:12 p.m. UTC | #2
Am 26.11.24 um 21:09 schrieb Paul Richard Thomas:
> Hi Harald,
> 
> Looks good to me.
> 
> Thanks
> 
> Paul

Thanks, Paul.

Pushed as r15-5701-geff7e72815ada5 .

> On Tue, 26 Nov 2024 at 19:51, Harald Anlauf <anlauf@gmx.de> wrote:
> 
>> Dear all,
>>
>> the attached patch fixes two minor front-end memleaks I saw when working
>> on recent PRs (pr117774 is one of them) and running f951 under valgrind.
>>
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>
>> Thanks,
>> Harald
>>
>>
>
  
Harald Anlauf Nov. 26, 2024, 10:22 p.m. UTC | #3
Am 26.11.24 um 23:12 schrieb Harald Anlauf:
> Am 26.11.24 um 21:09 schrieb Paul Richard Thomas:
>> Hi Harald,
>>
>> Looks good to me.
>>
>> Thanks
>>
>> Paul
>
> Thanks, Paul.
>
> Pushed as r15-5701-geff7e72815ada5 .
This should have read r15-5708-g94f98f67f7271f,
the testcase in the other one was used in the testing for the present one.

Duh!

>
>> On Tue, 26 Nov 2024 at 19:51, Harald Anlauf <anlauf@gmx.de> wrote:
>>
>>> Dear all,
>>>
>>> the attached patch fixes two minor front-end memleaks I saw when working
>>> on recent PRs (pr117774 is one of them) and running f951 under valgrind.
>>>
>>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>>
>>> Thanks,
>>> Harald
>>>
>>>
>>
>
>
>
  

Patch

From d5236446302a01eb11c017862a0813fafd0b95a1 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Tue, 26 Nov 2024 20:37:35 +0100
Subject: [PATCH] Fortran: fix minor front-end memleaks

gcc/fortran/ChangeLog:

	* expr.cc (find_inquiry_ref): Fix memleak introduced by scanning
	the reference chain to find and simplify inquiry references.
	* symbol.cc (gfc_copy_formal_args_intr): Free formal namespace
	when not needed to avoid a front-end memleak.
---
 gcc/fortran/expr.cc   | 6 +++++-
 gcc/fortran/symbol.cc | 2 ++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc
index 6b40e8e0aa5..a997bdae726 100644
--- a/gcc/fortran/expr.cc
+++ b/gcc/fortran/expr.cc
@@ -1833,6 +1833,7 @@  find_inquiry_ref (gfc_expr *p, gfc_expr **newp)
 {
   gfc_ref *ref;
   gfc_ref *inquiry = NULL;
+  gfc_ref *inquiry_head;
   gfc_expr *tmp;

   tmp = gfc_copy_expr (p);
@@ -1858,6 +1859,7 @@  find_inquiry_ref (gfc_expr *p, gfc_expr **newp)
       return false;
     }

+  inquiry_head = inquiry;
   gfc_resolve_expr (tmp);

   /* Leave these to the backend since the type and kind is not confirmed until
@@ -1930,7 +1932,7 @@  find_inquiry_ref (gfc_expr *p, gfc_expr **newp)
 		    mpc_imagref (tmp->value.complex), GFC_RND_MODE);
 	  break;
 	}
-      // TODO: Fix leaking expr tmp, when simplify is done twice.
+
       if (inquiry->next)
 	gfc_replace_expr (tmp, *newp);
     }
@@ -1944,10 +1946,12 @@  find_inquiry_ref (gfc_expr *p, gfc_expr **newp)
     }

   gfc_free_expr (tmp);
+  gfc_free_ref_list (inquiry_head);
   return true;

 cleanup:
   gfc_free_expr (tmp);
+  gfc_free_ref_list (inquiry_head);
   return false;
 }

diff --git a/gcc/fortran/symbol.cc b/gcc/fortran/symbol.cc
index e803cdd93c9..f13cb1883ea 100644
--- a/gcc/fortran/symbol.cc
+++ b/gcc/fortran/symbol.cc
@@ -4910,6 +4910,8 @@  gfc_copy_formal_args_intr (gfc_symbol *dest, gfc_intrinsic_sym *src,
   if (dest->formal != NULL)
     /* The current ns should be that for the dest proc.  */
     dest->formal_ns = gfc_current_ns;
+  else
+    gfc_free_namespace (gfc_current_ns);
   /* Restore the current namespace to what it was on entry.  */
   gfc_current_ns = parent_ns;
 }
--
2.35.3