Dear all,

while running f951 under valgrind on testcase gfortran.dg/sizeof_6.f90
I found two minor memleaks with GMP variables that were not cleared.

Regtested on x86_64-pc-linux-gnu.

I intend to commit to mainline soon unless there are comments.

(And no, this does not address the recent intermittent runtime failures
reported in pr116261).

Thanks,
Harald

From 0cef868a87050c9854ac17c5a604c1aa72ea1862 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anl...@gmx.de>
Date: Tue, 13 Aug 2024 21:17:45 +0200
Subject: [PATCH] Fortran: fix minor frontend GMP leaks

gcc/fortran/ChangeLog:

	* simplify.cc (gfc_simplify_sizeof): Clear used gmp variable.
	* target-memory.cc (gfc_target_expr_size): Likewise.
---
 gcc/fortran/simplify.cc      | 10 +++++++---
 gcc/fortran/target-memory.cc |  2 ++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index 8ddd491de11..953d59efd70 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -7778,9 +7778,13 @@ gfc_simplify_sizeof (gfc_expr *x)
 	  || x->ts.u.cl->length->expr_type != EXPR_CONSTANT))
     return NULL;

-  if (x->rank && x->expr_type != EXPR_ARRAY
-      && !gfc_array_size (x, &array_size))
-    return NULL;
+  if (x->rank && x->expr_type != EXPR_ARRAY)
+    {
+      if (!gfc_array_size (x, &array_size))
+	return NULL;
+
+      mpz_clear (array_size);
+    }

   result = gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
 				  &x->where);
diff --git a/gcc/fortran/target-memory.cc b/gcc/fortran/target-memory.cc
index a02db7a06e4..0a289f32d37 100644
--- a/gcc/fortran/target-memory.cc
+++ b/gcc/fortran/target-memory.cc
@@ -158,6 +158,8 @@ gfc_target_expr_size (gfc_expr *e, size_t *size)
 	asz = mpz_get_ui (tmp);
       else
 	return false;
+
+      mpz_clear (tmp);
     }
   else
     asz = 1;
--
2.35.3

Reply via email to