On 10/17/26 3:30 AM, Mikael Morin wrote:
Le 17/10/2026 à 00:41, Jerry DeLisle a écrit :
See attached patch. The finalization wrapper of a CLASS object
deallocates the allocatable components of a non-allocatable array
component with expressions like ptr2%entries(:)%children.
gfc_trans_deallocate already has a branch for that form, but the
generic allocatable-component branch before it also ran, using the type
of children for the elements of entries, so it freed whatever lay at
the offset of a component of the wrong type. The fix computes the
test for that form once and skips the generic branch for it; the
special branch already frees all allocatable components of the
elements.
The fix is in gfc_trans_deallocate in trans-stmt.cc.
allocate_with_source_14.f03 counted the bogus free, so its expected
__builtin_free count drops from 29 to 28; finalize_64.f90 is the new
test.
Regression tested on x86_64.
OK for mainline?
Regards,
Jerry
---
fortran: [PR127941] Fix invalid free in CLASS finalization wrapper
Deallocating a CLASS object whose type has a non-allocatable derived
type array component with nested allocatable components freed an
invalid pointer. For such a component the finalization wrapper
deallocates an expression like ptr%entries(:)%children, and
gfc_trans_deallocate already handles that form by deallocating the
allocatable components of the array elements. It also ran the generic
allocatable-component cleanup with the type of the last component,
which applied that type's components to the array elements. The patch
skips the generic cleanup for this form.
Assisted-by: Claude Opus 5.5
PR fortran/127941
gcc/fortran/ChangeLog:
* trans-stmt.cc (gfc_trans_deallocate): Do not deallocate the
allocatable components of the last component's type when the
expression is a non-allocatable array component generated by
finalize_component.
gcc/testsuite/ChangeLog:
* gfortran.dg/allocate_with_source_14.f03: Expect one fewer call
to __builtin_free.
* gfortran.dg/finalize_64.f90: New test.
OK, thanks.
Thanks for review.
The master branch has been updated by Jerry DeLisle <[email protected]>:
https://gcc.gnu.org/g:1fe80b2edf859d7bc66207d32d7ba1213f406799
commit r17-5275-g1fe80b2edf859d7bc66207d32d7ba1213f406799