[Bug fortran/125535] Wrong-code for implied-do with allocatable-component derived type and transformational intrinsic
cvs-commit at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Mon Jun 15 16:31:31 GMT 2026
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125535
--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-16 branch has been updated by Jerry DeLisle
<jvdelisle@gcc.gnu.org>:
https://gcc.gnu.org/g:1da2423372115bf33c9885ccd681ca800d848e49
commit r16-9111-g1da2423372115bf33c9885ccd681ca800d848e49
Author: Jerry DeLisle <jvdelisle@gcc.gnu.org>
Date: Thu Jun 11 18:50:37 2026 -0700
fortran: [PR125535] Plug remaining leak in implied-do array constructor
with allocatable components
Follow-up to the PR fortran/125535 wrong-code fix (commit 1b8421e9d5b,
already pushed): that fix corrected the *values* produced by an
implied-do array constructor of derived-type function results with
allocatable components, but left a separate memory leak in the same
code path.
An array constructor whose implied-do produces function results of a
derived type with allocatable components moves each result into the
constructor temporary, so the temporary owns those components. The
per-element finalization in gfc_trans_array_ctor_element only freed
the single slot referenced by the final loop offset, leaking the
allocatable components of every other element the loop produced.
A whole-array sweep is the only way to free every slot written by an
implied-do, but it may be used only when every element is an owned
function result: a variable element is shallow-copied into the
temporary and its components are aliased rather than owned, so freeing
them would double free. Add gfc_constructor_is_owned_alloc_comp to
detect the all-owned case and, when it holds, suppress the per-element
finalization and emit a single gfc_deallocate_alloc_comp_no_caf over
the whole temporary.
Assisted-by: Claude Opus 4.8
PR fortran/125535
gcc/fortran/ChangeLog:
* trans-array.cc (gfc_constructor_is_owned_alloc_comp): New
function.
(gfc_trans_array_constructor_value): Add OWNED_SWEEP parameter and,
when set, suppress the per-element finalization. Pass it through
the
recursive call.
(trans_array_constructor): Compute OWNED_SWEEP and, when set,
deallocate the allocatable components of the whole temporary in one
sweep.
gcc/testsuite/ChangeLog:
* gfortran.dg/asan/implied_do_alloc_comp_leak_1.f90: New test.
(cherry picked from commit b2ec4ebcf914f32f51c35dbd9a2c98dab5841097)
More information about the Gcc-bugs
mailing list