]> git.ipfire.org Git - thirdparty/gcc.git/commit
Fortran: Fix PR123868
authorJerry DeLisle <jvdelisle@gcc.gnu.org>
Fri, 30 Jan 2026 21:25:23 +0000 (13:25 -0800)
committerJerry DeLisle <jvdelisle@gcc.gnu.org>
Fri, 30 Jan 2026 21:38:43 +0000 (13:38 -0800)
commitca448bc5e435a2076cb3683a9be823c08a14e69e
tree412820ad542d91fc42738cc01a1863a3699b436c
parente4c57e146a224d0aaa71ace78f96fca1156add24
Fortran: Fix PR123868

When copying derived types with allocatable array components where the
array element type also has allocatable components, the condition at
line 11071 was incorrectly triggering a call to gfc_duplicate_allocatable.
However, for allocatable arrays with nested allocatables (where
cmp_has_alloc_comps && c->as is true), the add_when_allocated code
already includes a gfc_duplicate_allocatable call (generated by the
recursive structure_alloc_comps call at lines 10290-10293).

This caused the outer array to be allocated twice: first by the explicit
gfc_duplicate_allocatable call at line 11099, and then again by the
gfc_duplicate_allocatable embedded in add_when_allocated.  The first
allocation was leaked when the second allocation overwrote the data
pointer.

PR121628 added "add_when_allocated != NULL_TREE ||" to the condition,
which was redundant for scalars (already handled by !c->as) and wrong
for arrays (caused double allocation).  Simply removing this clause
restores the correct pre-PR121628 behavior.

PR fortran/123868

gcc/fortran/ChangeLog:

* trans-array.cc (structure_alloc_comps): For COPY_ALLOC_COMP,
remove the add_when_allocated != NULL_TREE clause that PR121628
added.  This clause was redundant for scalars and caused double
allocation for arrays with nested allocatable components.

gcc/testsuite/ChangeLog:

* gfortran.dg/array_memcpy_2.f90: Update expected memcpy count
from 4 to 3, as the double allocation bug is now fixed.
* gfortran.dg/pr123868.f90: New test.

Signed-off-by: Christopher Albert <albert@alumni.tugraz.at>
gcc/fortran/trans-array.cc
gcc/testsuite/gfortran.dg/array_memcpy_2.f90
gcc/testsuite/gfortran.dg/pr123868.f90 [new file with mode: 0644]