https://gcc.gnu.org/g:04e98c053c2b6734923e79f057f7e1a36d5446fa

commit r17-6328-g04e98c053c2b6734923e79f057f7e1a36d5446fa
Author: Jerry DeLisle <[email protected]>
Date:   Sat Nov 14 19:05:06 2026 -0800

    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.

Diff:
---
 gcc/fortran/trans-array.cc             |  3 +++
 gcc/testsuite/gfortran.dg/pr128736.f90 | 48 ++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 83735ea85f4e..61e5611e1a71 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 000000000000..3cf87198b4b4
--- /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

Reply via email to