]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR fortran/53389 (-frealloc-lhs: memory leak when assigning array function result...
authorTobias Burnus <burnus@net-b.de>
Wed, 23 May 2012 19:13:27 +0000 (21:13 +0200)
committerTobias Burnus <burnus@gcc.gnu.org>
Wed, 23 May 2012 19:13:27 +0000 (21:13 +0200)
2012-05-23  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-23  Tobias Burnus  <burnus@net-b.de>

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

From-SVN: r187810

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 b5789984b5fad3c85d26ef92129f8c804f552e35..e9d912eb6cf89172a8410d52aec822eb73851de7 100644 (file)
@@ -1,3 +1,9 @@
+2012-05-23  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-02  Tobias Burnus  <burnus@net-b.de>
 
        Backport from mainline
index a4be6e7b1652f1cf1f280ce9311fbaa93220abbc..d0d0900eb1d6b1513793efc8aac8af651382f645 100644 (file)
@@ -2056,6 +2056,11 @@ gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript,
   gfc_se se;
   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;
+
   /* TODO: This can generate bad code if there are ordering dependencies,
      e.g., a callee allocated function and an unknown size constructor.  */
   gcc_assert (ss != NULL);
index 7e11c70fe824a0f1e009e9536ce83ccf5336472c..e342cb81f84188e8dc338d2086f213b138e2b365 100644 (file)
@@ -1,3 +1,8 @@
+2012-05-23  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>
 
        Backport from mainline
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