This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug fortran/30720] New: runtime: check for empty array slices before allocating a negative amount of memory
- From: "dfranke at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 6 Feb 2007 20:08:29 -0000
- Subject: [Bug fortran/30720] New: runtime: check for empty array slices before allocating a negative amount of memory
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
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