https://gcc.gnu.org/g:e78a0cb8604cd3e0fdbc606ed5e7094b646ded02
commit r13-9212-ge78a0cb8604cd3e0fdbc606ed5e7094b646ded02 Author: Paul Thomas <pa...@gcc.gnu.org> Date: Sun Nov 24 14:22:06 2024 +0000 Fortran: Fix segfault in allocation of unlimited poly array [PR84869] 2024-11-24 Paul Thomas <pa...@gcc.gnu.org> gcc/fortran/ChangeLog PR fortran/84869 * trans-expr.cc (trans_class_vptr_len_assignment): To access the '_len' field, 're' must be unlimited polymorphic. gcc/testsuite/ PR fortran/84869 * gfortran.dg/pr84869.f90: Comment out test of component refs. (cherry picked from commit 911a870a6198a2fe50af8bbeb63de1dfaa90de0e) Diff: --- gcc/fortran/trans-expr.cc | 2 +- gcc/testsuite/gfortran.dg/pr84869.f90 | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index 59a7ff8d8d06..df109bd40547 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -9856,7 +9856,7 @@ trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le, vptr_expr = NULL; se.expr = gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR ( re->symtree->n.sym->backend_decl)); - if (to_len) + if (to_len && UNLIMITED_POLY (re)) from_len = gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR ( re->symtree->n.sym->backend_decl)); } diff --git a/gcc/testsuite/gfortran.dg/pr84869.f90 b/gcc/testsuite/gfortran.dg/pr84869.f90 new file mode 100644 index 000000000000..fe40b6208047 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr84869.f90 @@ -0,0 +1,25 @@ +! { dg-do compile } +! +! Test the fix for PR84869, where line 19 segfaulted. +! +! Contributed by Gerhard Steinmetz <gs...@t-online.de> +! +program p + type t + integer :: i + end type + call s +contains + function f() + class(t), allocatable :: f(:) + f = [(t(i), i = 1, 10)] + end + subroutine s + class(*), allocatable :: z(:) + allocate (z, source = f ()) ! Segfault in gfc_class_len_get. + select type (z) + type is (t) + if (any (z%i /= [(i, i = 1,10)])) stop 1 + end select + end +end