diff mbox

[Fortran,committed] PR58579 - fix allocation of string temporaries: Avoid overallocation

Message ID 524B37F4.1070603@net-b.de
State New
Headers show

Commit Message

Tobias Burnus Oct. 1, 2013, 9 p.m. UTC
In gfc_conv_string_tmp, gfortran allocates temporary strings. However, 
using "TYPE_SIZE (type)" didn't yield one byte as intended but 64 - 
which means that gfortran allocated 64 times as much memory as needed.

I wonder whether the same issue also occurs elsewhere.

Committed (Rev. ) after building and regtesting on x86-64-gnu-linux. I 
didn't see a simple way to generate a test case - but the dump of the 
PR's test case looks fine both for kind=1 and kind=4 strings.

Tobias
diff mbox

Patch

Index: gcc/fortran/ChangeLog
===================================================================
--- gcc/fortran/ChangeLog	(Revision 203085)
+++ gcc/fortran/ChangeLog	(Arbeitskopie)
@@ -1,3 +1,9 @@ 
+2013-10-01  Tobias Burnus  <burnus@net-b.de>
+
+	PR fortran/58579
+	* trans-expr.c (gfc_conv_string_tmp): Correctly obtain
+	the byte size of a single character.
+
 2013-09-27  Janne Blomqvist  <jb@gcc.gnu.org>
 
 	* intrinsic.texi (DATE_AND_TIME): Fix example.
Index: gcc/fortran/trans-expr.c
===================================================================
--- gcc/fortran/trans-expr.c	(Revision 203085)
+++ gcc/fortran/trans-expr.c	(Arbeitskopie)
@@ -2355,11 +2355,14 @@  gfc_conv_string_tmp (gfc_se * se, tree type, tree
     {
       /* Allocate a temporary to hold the result.  */
       var = gfc_create_var (type, "pstr");
-      tmp = gfc_call_malloc (&se->pre, type,
-			     fold_build2_loc (input_location, MULT_EXPR,
-					      TREE_TYPE (len), len,
-					      fold_convert (TREE_TYPE (len),
-							    TYPE_SIZE (type))));
+      gcc_assert (POINTER_TYPE_P (type));
+      tmp = TREE_TYPE (type);
+      gcc_assert (TREE_CODE (tmp) == ARRAY_TYPE);
+      tmp = TYPE_SIZE_UNIT (TREE_TYPE (tmp));
+      tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
+			    fold_convert (size_type_node, len),
+			    fold_convert (size_type_node, tmp));
+      tmp = gfc_call_malloc (&se->pre, type, tmp);
       gfc_add_modify (&se->pre, var, tmp);
 
       /* Free the temporary afterwards.  */