public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/30720]  New: runtime: check for empty array slices before allocating a negative amount of memory
@ 2007-02-06 20:08 dfranke at gcc dot gnu dot org
  2007-02-06 20:34 ` [Bug fortran/30720] " fxcoudert at gcc dot gnu dot org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: dfranke at gcc dot gnu dot org @ 2007-02-06 20:08 UTC (permalink / raw)
  To: gcc-bugs

The program below terminates with a runtime error due to an attempt to allocate
a negative amount of memory. The error occurs while allocating memory for a
temporary, empty, array slice. 

$> cat pr.f90
program runtime_error
  REAL    :: a(5), b
  INTEGER :: l, u
  l = 4
  u = 2

  a = (/ 1.0, 2.0, 3.0, 4.0, 5.0 /)
  b = f(a(l:u) - 3.0)

  CONTAINS
    REAL FUNCTION f(x)
      REAL, DIMENSION(:), INTENT(in) :: x
      f = sum(x)
    end function
END PROGRAM


There are two issues here:
 a) the runtime error as described above

$> gfortran-svn -O -fdump-tree-original -fdump-tree-optimized pr.f90

from dump-tree-original:
      int4 D.1036;
      void * D.1035;
      int4 D.1034;
      struct array1_real4 atmp.7;
      int4 D.1032;
      int4 D.1031;

      D.1031 = l;
      D.1032 = u;
      atmp.7.dtype = 281;
      atmp.7.dim[0].stride = 1;
      atmp.7.dim[0].lbound = 0;
      atmp.7.dim[0].ubound = u - D.1031;
      D.1034 = (u - D.1031) + 1;
      D.1035 = _gfortran_internal_malloc (D.1034 * 4);
      atmp.7.data = D.1035;

from dump-tree-optimized:
  void * SR.31;
  [...]
  SR.31 = _gfortran_internal_malloc (-4);
  _gfortran_internal_free (SR.31);


Here, if l > u+1, D.1035 will be negative and trigger the runtime error.
In this case, a runtime check whether the slice is empty before calling
_gfortran_internal_malloc() would result in code not stopping with a
(seemingly) unrelated error message.


 b) missed optimization during constant folding:
from dump-tree-original:
      struct array1_real4 atmp.7;

      atmp.7.dtype = 281;
      atmp.7.dim[0].stride = 1;
      atmp.7.dim[0].lbound = 0;
      atmp.7.dim[0].ubound = -1;
      atmp.7.data = 0B;
      [...]
      _gfortran_internal_free (atmp.7.data);

from dump-tree-optimzed:
  _gfortran_internal_free (0B);

If the boundaries are known at compile time, e.g. a(4:2), no memory is
allocated for the array descriptor, but _gfortran_internal_free(0B) is called
nonetheless. This call is still visible after optimization.

$> gfortran-svn -v
gcc version 4.3.0 20070126 (experimental)


-- 
           Summary: runtime: check for empty array slices before allocating
                    a negative amount of memory
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dfranke at gcc dot gnu dot org


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


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2007-02-16 15:55 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-06 20:08 [Bug fortran/30720] New: runtime: check for empty array slices before allocating a negative amount of memory dfranke at gcc dot gnu dot org
2007-02-06 20:34 ` [Bug fortran/30720] " fxcoudert at gcc dot gnu dot org
2007-02-06 21:00 ` fxcoudert at gcc dot gnu dot org
2007-02-06 23:13 ` fxcoudert at gcc dot gnu dot org
2007-02-06 23:16 ` fxcoudert at gcc dot gnu dot org
2007-02-07 17:53 ` tkoenig at gcc dot gnu dot org
2007-02-07 19:10 ` fxcoudert at gcc dot gnu dot org
2007-02-07 20:49 ` tkoenig at gcc dot gnu dot org
2007-02-09 20:31 ` fxcoudert at gcc dot gnu dot org
2007-02-09 20:33 ` [Bug fortran/30720] [4.2/4.1 only] " fxcoudert at gcc dot gnu dot org
2007-02-16 12:20 ` fxcoudert at gcc dot gnu dot org
2007-02-16 15:55 ` [Bug fortran/30720] [4.1 " fxcoudert at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).