]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR fortran/53389 (-frealloc-lhs: memory leak when assigning array function result...
authorTobias Burnus <burnus@gcc.gnu.org>
Tue, 22 May 2012 10:10:47 +0000 (12:10 +0200)
committerTobias Burnus <burnus@gcc.gnu.org>
Tue, 22 May 2012 10:10:47 +0000 (12:10 +0200)
2012-05-22  Tobias Burnus  <burnus@net-b.de>

        PR fortran/53389
        * trans-array.c (gfc_add_loop_ss_code): Don't evaluate
        * expression, if
        ss->is_alloc_lhs is set.

2012-05-22  Tobias Burnus  <burnus@net-b.de>

        PR fortran/53389
        * gfortran.dg/realloc_on_assign_15.f90: New.

From-SVN: r187769

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

index 0880b3172292d4d3f5427c4ced68327a95e25335..38bff786afa8734b25a2be452b57c2f21563e369 100644 (file)
@@ -1,8 +1,14 @@
+2012-05-22  Tobias Burnus  <burnus@net-b.de>
+
+       PR fortran/53389
+       * trans-array.c (gfc_add_loop_ss_code): Don't evaluate expression, if
+       ss->is_alloc_lhs is set.
+
 2012-05-22  Dodji Seketeli  <dodji@redhat.com>
 
        PR c++/53322
        * f95-lang.c (gfc_init_builtin_functions): Remove the unused
-       typedef builtin_type.
+       typedef builtin_type.
 
 2012-05-14  Janne Blomqvist  <jb@gcc.gnu.org>
 
index b24d1c323ede58f1f57e80e86dfaa94725fe73d4..02bb38d41e43ed71501e167203834aa52be13dca 100644 (file)
@@ -2401,6 +2401,11 @@ gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
   bool skip_nested = false;
   int n;
 
+  /* Don't evaluate the arguments for realloc_lhs_loop_for_fcn_call; otherwise,
+     arguments could get evaluated multiple times.  */
+  if (ss->is_alloc_lhs)
+    return;
+
   outer_loop = outermost_loop (loop);
 
   /* TODO: This can generate bad code if there are ordering dependencies,
index 0d21111d5268fe28857aef1f18be6697b2eaff3a..21f448daa52e9827c8da24e710eafdf9c6390cff 100644 (file)
@@ -1,3 +1,8 @@
+2012-05-22  Tobias Burnus  <burnus@net-b.de>
+
+       PR fortran/53389
+       * gfortran.dg/realloc_on_assign_15.f90: New.
+
 2012-05-22  Richard Guenther  <rguenther@suse.de>
 
        PR middle-end/51071
@@ -12,7 +17,7 @@
 
        PR c++/53322
        * g++.dg/warn/Wunused-local-typedefs.C: Adjust to use -Wunused
-       instead of -Wunused-local-typedefs.
+       instead of -Wunused-local-typedefs.
 
 2012-05-21  Paolo Carlini  <paolo.carlini@oracle.com>
 
diff --git a/gcc/testsuite/gfortran.dg/realloc_on_assign_15.f90 b/gcc/testsuite/gfortran.dg/realloc_on_assign_15.f90
new file mode 100644 (file)
index 0000000..2a0e5be
--- /dev/null
@@ -0,0 +1,40 @@
+! { dg-do run }
+!
+! PR fortran/53389
+!
+! The program was leaking memory before due to
+! realloc on assignment and nested functions.
+!
+module foo
+  implicit none
+  contains
+
+  function filler(array, val)
+    real, dimension(:), intent(in):: array
+    real, dimension(size(array)):: filler
+    real, intent(in):: val
+
+    filler=val
+
+  end function filler
+end module
+
+program test
+  use foo
+  implicit none
+
+  real, dimension(:), allocatable:: x, y
+  integer, parameter:: N=1000 !*1000
+  integer:: i
+
+!  allocate( x(N) )
+  allocate( y(N) )
+  y=0.0
+
+  do i=1, N
+!    print *,i
+    x=filler(filler(y, real(2*i)), real(i))
+    y=y+x
+  end do
+
+end program test