http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52673

             Bug #: 52673
           Summary: implement -fheap-arrays in gfortran
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: fortran
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: arnau...@users.sourceforge.net


Consider p1
      subroutine ss3(ia)
      integer :: ia(*)
      ia(6)=1  ! out-of-bound access
      end subroutine ss3
      subroutine ss2(ia)
      integer :: ia(*)
      call ss3(ia)
      end subroutine ss2
      subroutine ss1
      integer :: ia(3) ! local array
      call ss2(ia)
      end subroutine ss1
      program p1
      call ss1
      end program p1
and p2
      subroutine ss3(ia)
      integer :: ia(*)
      ia(6)=1  ! out-of-bound access
      end subroutine ss3
      subroutine ss2(ia)
      integer :: ia(*)
      call ss3(ia)
      end subroutine ss2
      subroutine ss1
      integer, allocatable :: ia(:) ! local allocatable array
      allocate( ia(3) )
      call ss2(ia)
      end subroutine ss1
      program p2
      call ss1
      end program p2
Both p1 and p2 contain an array out-of-bound access. However, valgrind will
only detect the illegal in p2 as the local array on p1 is allocated on the
stack.
Compiling with "-fstack-protector-all" may occasionally help but not in this
case.

A useful option would be "-fheap-arrays" which would allocate all local arrays
on the heap to make it easier to detect memory corruption using tool such as
valgrind. All local arrays of constant size or unknown size ("automatic
arrays") should be dealt with. 
This is essentially the opposite of "-fstack-arrays": make the program slower
to detect memory problems.

An possible small optimisation could be to keep the arrays not passed to other
routines on the stack as local bound run-time checking (-fcheck=bounds) should
be sufficient to detect problems.

Note that the Intel Fortran compiler offers "-heap-arrays" to allocate
automatic arrays on the heap. This does not do anything to local arrays of size
known at compile time.

Reply via email to