re PR fortran/58058 (Memory leak with transfer function)
authorJanus Weil <janus@gcc.gnu.org>
Fri, 9 Aug 2013 16:57:58 +0000 (18:57 +0200)
committerJanus Weil <janus@gcc.gnu.org>
Fri, 9 Aug 2013 16:57:58 +0000 (18:57 +0200)
2013-08-09  Janus Weil  <janus@gcc.gnu.org>

PR fortran/58058
* trans-intrinsic.c (gfc_conv_intrinsic_transfer): Free the temporary
string, if necessary.

2013-08-09  Janus Weil  <janus@gcc.gnu.org>

PR fortran/58058
* gfortran.dg/transfer_intrinsic_6.f90: New.

From-SVN: r201633

gcc/fortran/ChangeLog
gcc/fortran/trans-intrinsic.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/transfer_intrinsic_6.f90 [new file with mode: 0644]

index bcd91a928971ef2128fc69a8aa362df20ba6b1ef..8aa70a3adbeba9f9c2d608991c8f876964e0a5ea 100644 (file)
@@ -1,3 +1,9 @@
+2013-08-09  Janus Weil  <janus@gcc.gnu.org>
+
+       PR fortran/58058
+       * trans-intrinsic.c (gfc_conv_intrinsic_transfer): Free the temporary
+       string, if necessary.
+
 2013-08-06  Martin Jambor  <mjambor@suse.cz>
 
        PR fortran/57987
index 3fbf193d03ca37faeeac0bf5523e0f6e6f70a0a9..6b85b5b78db65431cc8c0cda2ac2474e524056c3 100644 (file)
@@ -5652,8 +5652,7 @@ scalar_transfer:
 
   if (expr->ts.type == BT_CHARACTER)
     {
-      tree direct;
-      tree indirect;
+      tree direct, indirect, free;
 
       ptr = convert (gfc_get_pchar_type (expr->ts.kind), source);
       tmpdecl = gfc_create_var (gfc_get_pchar_type (expr->ts.kind),
@@ -5686,6 +5685,13 @@ scalar_transfer:
       tmp = build3_v (COND_EXPR, tmp, direct, indirect);
       gfc_add_expr_to_block (&se->pre, tmp);
 
+      /* Free the temporary string, if necessary.  */
+      free = gfc_call_free (tmpdecl);
+      tmp = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
+                            dest_word_len, source_bytes);
+      tmp = build3_v (COND_EXPR, tmp, free, build_empty_stmt (input_location));
+      gfc_add_expr_to_block (&se->post, tmp);
+
       se->expr = tmpdecl;
       se->string_length = fold_convert (gfc_charlen_type_node, dest_word_len);
     }
index 9f9a932e15a0cfe0bd9d64e6cfad7248b02bf67c..f14978402cad8c6ea1bd6c89bb5fe96282a80432 100644 (file)
@@ -1,3 +1,8 @@
+2013-08-09  Janus Weil  <janus@gcc.gnu.org>
+
+       PR fortran/58058
+       * gfortran.dg/transfer_intrinsic_6.f90: New.
+
 2013-08-09  Paolo Carlini  <paolo.carlini@oracle.com>
 
        Revert:
diff --git a/gcc/testsuite/gfortran.dg/transfer_intrinsic_6.f90 b/gcc/testsuite/gfortran.dg/transfer_intrinsic_6.f90
new file mode 100644 (file)
index 0000000..e76bc49
--- /dev/null
@@ -0,0 +1,20 @@
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+!
+! PR 58058: [4.7/4.8/4.9 Regression] Memory leak with transfer function
+!
+! Contributed by Thomas Jourdan <thomas.jourdan@orange.fr>
+
+  implicit none
+
+  integer, dimension(3) :: t1
+  character(len=64) :: str
+  
+  t1 = (/1,2,3/)
+
+  str = transfer(t1,str)
+
+end
+
+! { dg-final { scan-tree-dump-times "__builtin_free" 1 "original" } }
+! { dg-final { cleanup-tree-dump "original" } }