https://gcc.gnu.org/g:de431c751cef6ef0d7e9e54f67ef64afc6d05b09
commit r17-6642-gde431c751cef6ef0d7e9e54f67ef64afc6d05b09 Author: Jerry DeLisle <[email protected]> Date: Sat Nov 14 17:52:05 2026 -0800 fortran: [PR128735] END ASSOCIATE restores old CLASS array selector When the selector of ASSOCIATE or SELECT TYPE was a polymorphic array pointer or allocatable, the end of the block copied the descriptor the selector had on entry back into it. This undid a pointer assignment to the selector inside the block, and after a DEALLOCATE of the selector it restored the freed array, giving a double free. The copy-back is needed only for SELECT RANK, whose associate name can be allocated or pointer-assigned; other associate names are neither POINTER nor ALLOCATABLE. Keep it only for an assumed-rank selector. Assisted-by: Claude Opus 5.5 PR fortran/128735 gcc/fortran/ChangeLog: * trans-stmt.cc (trans_associate_var): Copy the class container of a class array associate name back to the selector only for SELECT RANK. gcc/testsuite/ChangeLog: * gfortran.dg/pr128735.f90: New test. Diff: --- gcc/fortran/trans-stmt.cc | 13 ++++-- gcc/testsuite/gfortran.dg/pr128735.f90 | 77 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc index 79c3d1e1fe09..9f5e408b3e03 100644 --- a/gcc/fortran/trans-stmt.cc +++ b/gcc/fortran/trans-stmt.cc @@ -2257,8 +2257,14 @@ trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block) /* Needed to get/set the _len component below. */ target_expr = se.expr; + /* Only a SELECT RANK name, whose selector is assumed rank, can be + allocated or pointer-assigned (F2023:11.1.3.3, 11.1.10.3), so + only its container is copied back to the selector. */ + bool copyback = e->rank == -1; + /* Obtain a temporary class container for the result. */ - gfc_conv_class_to_class (&se, e, sym->ts, false, true, false, false); + gfc_conv_class_to_class (&se, e, sym->ts, false, copyback, false, + false); se.expr = build_fold_indirect_ref_loc (input_location, se.expr); desc = gfc_class_data_get (se.expr); @@ -2289,8 +2295,9 @@ trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block) fold_convert (TREE_TYPE (charlen), tmp)); /* and the opposite way at the end of the block, to hand changes on the string length back. */ - gfc_add_modify (&se.post, tmp, - fold_convert (TREE_TYPE (tmp), charlen)); + if (copyback) + gfc_add_modify (&se.post, tmp, + fold_convert (TREE_TYPE (tmp), charlen)); /* Length assignment done, prevent adding it again below. */ need_len_assign = false; } diff --git a/gcc/testsuite/gfortran.dg/pr128735.f90 b/gcc/testsuite/gfortran.dg/pr128735.f90 new file mode 100644 index 000000000000..521df9b49e61 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr128735.f90 @@ -0,0 +1,77 @@ +! { dg-do run } +! +! PR 128735 +! END ASSOCIATE and END SELECT must not restore the descriptor that a +! polymorphic array selector had on entry to the block. + +module m + type t + integer :: i + end type + type holder + class(*), pointer :: p(:) => null() + end type +end module + +program p + use m + implicit none + class(*), pointer :: q(:) + class(t), pointer :: r(:) + type(holder) :: obj + integer, target :: a(2) = [1, 2], b(3) = [3, 4, 5] + character(3), target :: c1(2) = ['abc', 'def'] + character(5), target :: c2(3) = ['ghijk', 'lmnop', 'qrstu'] + type(t), target :: ta(2) = t(1), tb(3) = t(2) + + q => a + associate (x => q) + q => b + end associate + if (size (q) /= 3) stop 1 + + obj%p => a + select type (z => obj%p) + type is (integer) + obj%p => b + if (any (z /= [1, 2])) stop 2 + end select + if (size (obj%p) /= 3) stop 3 + + q => c1 + associate (x => q) + q => c2 + end associate + select type (q) + type is (character(*)) + if (len (q) /= 5 .or. size (q) /= 3) stop 4 + end select + + r => ta + associate (x => r) + x(1)%i = 99 + r => tb + end associate + if (size (r) /= 3 .or. ta(1)%i /= 99) stop 5 + + call realloc + call dealloc +contains + subroutine realloc + class(t), allocatable :: s(:) + allocate (s(2), source=t(1)) + associate (x => s) + deallocate (s) + allocate (s(4), source=t(2)) + end associate + if (size (s) /= 4 .or. any (s%i /= 2)) stop 6 + end subroutine + subroutine dealloc + class(*), allocatable :: s(:) + allocate (s, source=[1, 2]) + associate (x => s) + deallocate (s) + end associate + if (allocated (s)) stop 7 + end subroutine +end program
