On 11/24/26 8:53 AM, Mikael Morin wrote:
Le 15/11/2026 à 02:54, Jerry DeLisle a écrit :
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?
OK.
Thanks for review.
The master branch has been updated by Jerry DeLisle <[email protected]>:
https://gcc.gnu.org/g:de431c751cef6ef0d7e9e54f67ef64afc6d05b09
commit r17-6642-gde431c751cef6ef0d7e9e54f67ef64afc6d05b09
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.