https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128736
Bug ID: 128736
Summary: [OOP] Heap overflow when a CLASS array is assigned to
an allocatable TYPE array
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: jvdelisle at gcc dot gnu.org
Target Milestone: ---
When the allocatable left side of an intrinsic assignment is a
non-polymorphic array and the right side is a polymorphic array of the
same declared type, reallocation on assignment sizes each element as the
class container instead of the declared type:
program p
type t
integer :: i(100)
end type
class(t), allocatable :: z(:)
type(t), allocatable :: w(:)
allocate (z(1000), source=t(1))
w = z
print *, sum (w(1000)%i)
end program
gfortran:
Program received signal SIGSEGV: Segmentation fault - invalid memory
reference.
flang prints 100.
-fdump-tree-original shows w allocated with 72 bytes per element, the
size of the rank-1 class container, and the elements then copied at
400 bytes each:
w.span = 72;
D.4864 = MAX_EXPR <(unsigned long) (D.4863 * 72), 1>;
w.data = (void * restrict) __builtin_malloc (D.4864);
For a type smaller than the container this over-allocates instead (18
times for an INTEGER component), and w.span stays wrong.
gfc_alloc_allocatable_for_assignment (trans-array.cc) takes elemsize2
from TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts)) when expr1 is
not CLASS. For a CLASS expr2 that type node is the class container; the
size should come from the declared type, as for a CLASS expr1.