Patchwork [fortran,committed] Free memory allocated for data statements

login
register
mail settings
Submitter Mikael Morin
Date July 28, 2010, 5:39 p.m.
Message ID <4C506B60.6090601@sfr.fr>
Download mbox | patch
Permalink /patch/60164/
State New
Headers show

Comments

Mikael Morin - July 28, 2010, 5:39 p.m.
Hello,

This patch fixes two valgrind warnings shown with the testcase:

!!!!!!!
   data i /0/
   print *, i
   end
!!!!!!!


==59757== 8 bytes in 1 blocks are definitely lost in loss record 4 of 368
==59757==    at 0x25A67B: malloc (in 
/usr/local/lib/valgrind/vgpreload_memcheck-amd64-freebsd.so)
==59757==    by 0x1C523B8: __gmp_default_allocate (in 
/usr/local/lib/libgmp.so.10)
==59757==    by 0x1C60607: __gmpz_init (in /usr/local/lib/libgmp.so.10)
==59757==    by 0x4A2737: top_val_list.isra.9 (decl.c:428)
==59757==    by 0x4A2958: gfc_match_data (decl.c:541)
==59757==    by 0x4E7C35: match_word (parse.c:65)
==59757==    by 0x4E8936: decode_statement (parse.c:361)
==59757==    by 0x4E98C4: next_statement (parse.c:727)
==59757==    by 0x4ED1D8: gfc_parse_file (parse.c:4311)
==59757==    by 0x522EA7: gfc_be_parse_file (f95-lang.c:241)
==59757==    by 0x80F6BD: toplev_main (toplev.c:945)
==59757==    by 0x48E17B: (below main) (in 
/usr/home/mik/gcc46/build/gcc/f951)
==59757==
==59757== 8 bytes in 1 blocks are definitely lost in loss record 5 of 368
==59757==    at 0x25A67B: malloc (in 
/usr/local/lib/valgrind/vgpreload_memcheck-amd64-freebsd.so)
==59757==    by 0x1C523B8: __gmp_default_allocate (in 
/usr/local/lib/libgmp.so.10)
==59757==    by 0x1C61937: __gmpz_init_set_si (in 
/usr/local/lib/libgmp.so.10)
==59757==    by 0x49F809: gfc_assign_data_value (data.c:210)
==59757==    by 0x4FBE31: traverse_data_var (resolve.c:12131)
==59757==    by 0x4FEEF8: resolve_types (resolve.c:12315)
==59757==    by 0x4F895F: gfc_resolve (resolve.c:13085)
==59757==    by 0x4ED7C3: gfc_parse_file (parse.c:4211)
==59757==    by 0x522EA7: gfc_be_parse_file (f95-lang.c:241)
==59757==    by 0x80F6BD: toplev_main (toplev.c:945)
==59757==    by 0x48E17B: (below main) (in 
/usr/home/mik/gcc46/build/gcc/f951)


Committed as obvious after regression testing.

Mikael

Patch

Index: decl.c
===================================================================
--- decl.c	(revision 162648)
+++ decl.c	(revision 162649)
@@ -134,6 +134,7 @@  free_value (gfc_data_value *p)
   for (; p; p = q)
     {
       q = p->next;
+      mpz_clear (p->repeat);
       gfc_free_expr (p->expr);
       gfc_free (p);
     }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 162648)
+++ ChangeLog	(revision 162649)
@@ -1,3 +1,8 @@ 
+2010-07-28  Mikael Morin  <mikael@gcc.gnu.org>
+
+	* decl.c (free_value): Also free repeat field.
+	* data.c (gfc_assign_data_value): Always free offset before returning.
+
 2010-07-28  Daniel Kraft  <d@domob.eu>
 
 	* gfortran.h (gfc_build_intrinsic_call): New method.
Index: data.c
===================================================================
--- data.c	(revision 162648)
+++ data.c	(revision 162649)
@@ -244,7 +244,7 @@  gfc_assign_data_value (gfc_expr *lvalue, gfc_expr
 	      gfc_error ("'%s' at %L already is initialized at %L",
 			 lvalue->symtree->n.sym->name, &lvalue->where,
 			 &init->where);
-	      return FAILURE;
+	      goto abort;
 	    }
 
 	  if (init == NULL)
@@ -267,7 +267,7 @@  gfc_assign_data_value (gfc_expr *lvalue, gfc_expr
 	    {
 	      gfc_error ("Data element below array lower bound at %L",
 			 &lvalue->where);
-	      return FAILURE;
+	      goto abort;
 	    }
 	  else
 	    {
@@ -275,12 +275,12 @@  gfc_assign_data_value (gfc_expr *lvalue, gfc_expr
 	      if (spec_size (ref->u.ar.as, &size) == SUCCESS)
 		{
 		  if (mpz_cmp (offset, size) >= 0)
-		  {
-		    mpz_clear (size);
-		    gfc_error ("Data element above array upper bound at %L",
-			       &lvalue->where);
-		    return FAILURE;
-		  }
+		    {
+		      mpz_clear (size);
+		      gfc_error ("Data element above array upper bound at %L",
+		                 &lvalue->where);
+		      goto abort;
+		    }
 		  mpz_clear (size);
 		}
 	    }
@@ -336,6 +336,8 @@  gfc_assign_data_value (gfc_expr *lvalue, gfc_expr
       last_con = con;
     }
 
+  mpz_clear (offset);
+
   if (ref || last_ts->type == BT_CHARACTER)
     {
       if (lvalue->ts.u.cl->length == NULL && !(ref && ref->u.ss.length != NULL))
@@ -371,6 +373,10 @@  gfc_assign_data_value (gfc_expr *lvalue, gfc_expr
     last_con->expr = expr;
 
   return SUCCESS;
+
+abort:
+  mpz_clear (offset);
+  return FAILURE;
 }