Patchwork [Fortran,committed] PR53310 - Fix multi-malloc memory leak in EOSHIFT

login
register
mail settings
Submitter Tobias Burnus
Date May 11, 2012, 8:15 a.m.
Message ID <4FACCAB2.9040401@net-b.de>
Download mbox | patch
Permalink /patch/158459/
State New
Headers show

Comments

Tobias Burnus - May 11, 2012, 8:15 a.m.
I have committed the attached patch as obvious. libgfortran (since GCC 
4.5) allocates the memory rank()-times instead of only once in eoshift2. 
For the test case of the PR, gfortran leaked 6 MB per call!

Build and regtested on x86-64-linux and comitted as Rev. 187395 to the 
4.8 trunk. I intent to backport the patch, but I have not yet decided 
whether it will be down to 4.5 or not.

Tobias

PS: In the ChangeLog I have converted some spaces into tabs.

Patch

Index: libgfortran/intrinsics/eoshift2.c
===================================================================
--- libgfortran/intrinsics/eoshift2.c	(revision 187394)
+++ libgfortran/intrinsics/eoshift2.c	(working copy)
@@ -77,6 +77,10 @@  eoshift2 (gfc_array_char *ret, const gfc_array_cha
 
       ret->offset = 0;
       ret->dtype = array->dtype;
+
+      /* xmalloc allocates a single byte for zero size.  */
+      ret->base_addr = xmalloc (size * arraysize);
+
       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
         {
 	  index_type ub, str;
@@ -90,10 +94,6 @@  eoshift2 (gfc_array_char *ret, const gfc_array_cha
 	      * GFC_DESCRIPTOR_STRIDE(ret,i-1);
 
 	  GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
-
-          /* xmalloc allocates a single byte for zero size.  */
-	  ret->base_addr = xmalloc (size * arraysize);
-
         }
     }
   else if (unlikely (compile_options.bounds_check))
Index: libgfortran/ChangeLog
===================================================================
--- libgfortran/ChangeLog	(revision 187394)
+++ libgfortran/ChangeLog	(working copy)
@@ -1,17 +1,23 @@ 
+2012-05-11  Tobias Burnus  <burnus@net-b.de>
+
+	PR fortran/53310
+	* intrinsics/eoshift2.c (eoshift2): Do not leak
+	memory by allocating it in the loop.
+
 2012-05-05  Janne Blomqvist  <jb@gcc.gnu.org>
 
-        * config.h.in: Regenerated.
-        * configure: Regenerated.
-        * configure.ac: Add checks for getegid and __secure_getenv.
-        * io/unix.c (P_tmpdir): Fallback definition for macro.
-        (tempfile_open): New function.
-        (tempfile): Use secure_getenv, call tempfile_open to try each
-        directory in turn.
-        * libgfortran.h (DEFAULT_TMPDIR): Remove macro.
-        (secure_getenv): New macro/prototype.
-        * runtime/environ.c (secure_getenv): New function.
-        (variable_table): Rename GFORTRAN_TMPDIR to TMPDIR.
-        * runtime/main.c (find_addr2line): Use secure_getenv.
+	* config.h.in: Regenerated.
+	* configure: Regenerated.
+	* configure.ac: Add checks for getegid and __secure_getenv.
+	* io/unix.c (P_tmpdir): Fallback definition for macro.
+	(tempfile_open): New function.
+	(tempfile): Use secure_getenv, call tempfile_open to try each
+	directory in turn.
+	* libgfortran.h (DEFAULT_TMPDIR): Remove macro.
+	(secure_getenv): New macro/prototype.
+	* runtime/environ.c (secure_getenv): New function.
+	(variable_table): Rename GFORTRAN_TMPDIR to TMPDIR.
+	* runtime/main.c (find_addr2line): Use secure_getenv.
 
 2012-04-22  Tobias Burnus  <burnus@net-b.de>
 
@@ -479,24 +485,24 @@ 
 
 2012-03-15  Janne Blomqvist  <jb@gcc.gnu.org>
 
-        PR libfortran/52434
-        PR libfortran/48878
-        PR libfortran/38199
-        * io/unit.c (get_internal_unit): Default to ROUND_UNSPECIFIED.
-        (init_units): Likewise.
-        * io/write_float.def (determine_precision): New function.
-        (output_float): Take into account buffer with %f format, no need
-        for our own rounding if unspecified or processor specified
-        rounding.
-        (DTOA): Simplify format string, add parameters.
-        (FDTOA): New macros similar to DTOA, but using %f format.
-        (OUTPUT_FLOAT_FMT_G): Stack allocate newf, determine correct
-        precision and fill buffer.
-        (EN_PREC): New macro.
-        (determine_en_precision): New function.
-        (WRITE_FLOAT): For G format, move buffer filling into
-        output_float_FMT_G, use FDTOA for F format.
-        (write_float): Increase buffer due to F format.
+	PR libfortran/52434
+	PR libfortran/48878
+	PR libfortran/38199
+	* io/unit.c (get_internal_unit): Default to ROUND_UNSPECIFIED.
+	(init_units): Likewise.
+	* io/write_float.def (determine_precision): New function.
+	(output_float): Take into account buffer with %f format, no need
+	for our own rounding if unspecified or processor specified
+	rounding.
+	(DTOA): Simplify format string, add parameters.
+	(FDTOA): New macros similar to DTOA, but using %f format.
+	(OUTPUT_FLOAT_FMT_G): Stack allocate newf, determine correct
+	precision and fill buffer.
+	(EN_PREC): New macro.
+	(determine_en_precision): New function.
+	(WRITE_FLOAT): For G format, move buffer filling into
+	output_float_FMT_G, use FDTOA for F format.
+	(write_float): Increase buffer due to F format.
 
 2012-03-14  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>