See attached patch. For a polymorphic array selector, trans_associate_var
gives the associate name its own class container and copies that
container back into the selector at the end of the block. A pointer
assignment, ALLOCATE or DEALLOCATE of the selector inside the block is
therefore undone; after a DEALLOCATE the freed array comes back and is
freed again. The copy-back is needed only for SELECT RANK, where the
associate name has the selector's POINTER or ALLOCATABLE attribute, so
the patch keeps it only for an assumed-rank selector. flang gives the
expected results for the new test.
This patch must go in after my PR54322 patch ("fortran: [PR54322]
Associate names are neither POINTER nor ALLOCATABLE"). Without that
patch gfortran accepts pointer assignment to an associate name, and such
code works only because of the copy-back removed here.
The fix is in trans_associate_var in trans-stmt.cc: the copyback
argument to gfc_conv_class_to_class, and the matching copy-back of _len
for an unlimited polymorphic selector. The rest is the new test.
Regression tested on x86_64.
OK for mainline?
Regards,
Jerry
---
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.
>From cb24f2ccfd8555c01dc4f02fcd49c74f8249b5b5 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 14 Nov 2026 17:52:05 -0800
Subject: [PATCH] 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.
---
gcc/fortran/trans-stmt.cc | 13 ++++-
gcc/testsuite/gfortran.dg/pr128735.f90 | 77 ++++++++++++++++++++++++++
2 files changed, 87 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/pr128735.f90
diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index 699b6187578..c4037a8e79e 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -2384,8 +2384,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);
@@ -2423,8 +2429,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 00000000000..521df9b49e6
--- /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
--
2.56.0