See attached patch. For w = z, with w an allocatable TYPE(t) array and
z a CLASS(t) array, reallocation on assignment took the element size
from the type node of z, which is the class container (72 bytes at rank
1), not t. A t larger than that overflowed w, and the new test
segfaults on trunk; a smaller t was over-allocated and w got the
container size as its span. The variable and expr have the same
declared type here (Table 10.8), so the patch takes the element size
from the declared type of z, as is already done when w is CLASS too.
The fix is in gfc_alloc_allocatable_for_assignment in trans-array.cc;
the rest is the new test.
Regression tested on x86_64.
OK for mainline?
Regards,
Jerry
---
fortran: [PR128736] Heap overflow assigning CLASS array to TYPE array
When a CLASS array was assigned to an allocatable array of its declared
type, reallocation on assignment sized each element of the variable as
the class container instead of the declared type. A type larger than
the container overflowed the new array, and a smaller one was
over-allocated and given the wrong span. Take the element size from the
declared type of the CLASS expression.
Assisted-by: Claude Opus 5.5
PR fortran/128736
gcc/fortran/ChangeLog:
* trans-array.cc (gfc_alloc_allocatable_for_assignment): Take the
element size of a variable that is not polymorphic from the declared
type of a polymorphic expression.
gcc/testsuite/ChangeLog:
* gfortran.dg/pr128736.f90: New test.
>From 382dd6768e4c16ec24c55f57df7dd80530e5727e Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 14 Nov 2026 19:05:06 -0800
Subject: [PATCH] fortran: [PR128736] Heap overflow assigning CLASS array to
TYPE array
When a CLASS array was assigned to an allocatable array of its declared
type, reallocation on assignment sized each element of the variable as
the class container instead of the declared type. A type larger than
the container overflowed the new array, and a smaller one was
over-allocated and given the wrong span. Take the element size from the
declared type of the CLASS expression.
Assisted-by: Claude Opus 5.5
PR fortran/128736
gcc/fortran/ChangeLog:
* trans-array.cc (gfc_alloc_allocatable_for_assignment): Take the
element size of a variable that is not polymorphic from the declared
type of a polymorphic expression.
gcc/testsuite/ChangeLog:
* gfortran.dg/pr128736.f90: New test.
---
gcc/fortran/trans-array.cc | 3 ++
gcc/testsuite/gfortran.dg/pr128736.f90 | 48 ++++++++++++++++++++++++++
2 files changed, 51 insertions(+)
create mode 100644 gcc/testsuite/gfortran.dg/pr128736.f90
diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index ea72cbd7ef5..63f056db23f 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -12235,6 +12235,9 @@ gfc_alloc_allocatable_for_assignment (gfc_loopinfo *loop,
else
tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&CLASS_DATA (expr2)->ts));
}
+ else if (expr2->ts.type == BT_CLASS)
+ /* A variable that is not polymorphic has the declared type of expr2. */
+ tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&CLASS_DATA (expr2)->ts));
else
tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
elemsize2 = fold_convert (gfc_array_index_type, tmp);
diff --git a/gcc/testsuite/gfortran.dg/pr128736.f90 b/gcc/testsuite/gfortran.dg/pr128736.f90
new file mode 100644
index 00000000000..3cf87198b4b
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr128736.f90
@@ -0,0 +1,48 @@
+! { dg-do run }
+!
+! PR 128736
+
+module m
+ type t
+ integer :: i(100)
+ end type
+ type, extends(t) :: t2
+ integer :: j(50) = -1
+ end type
+contains
+ function f (n) result (r)
+ integer :: n
+ class(t), allocatable :: r(:)
+ integer :: k
+ allocate (t2 :: r(n))
+ do k = 1, n
+ r(k)%i = k
+ end do
+ end function
+end module
+
+program p
+ use m
+ call s
+contains
+ subroutine s
+ class(t), allocatable :: z(:)
+ type(t), allocatable :: w(:)
+ integer :: k
+ allocate (z(1000))
+ do k = 1, 1000
+ z(k)%i = k
+ end do
+ w = z
+ if (size (w) /= 1000 .or. any (w(1000)%i /= 1000)) stop 1
+ deallocate (z)
+ allocate (t2 :: z(10))
+ do k = 1, 10
+ z(k)%i = 10*k
+ end do
+ w = z(::3)
+ if (size (w) /= 4 .or. any (w%i(1) /= [10, 40, 70, 100])) stop 2
+ w = f (7)
+ if (size (w) /= 7 .or. any (w(7)%i /= 7)) stop 3
+ end subroutine
+end program
--
2.56.0