copy_wrapper);
gfc_add_expr_to_block (&fnblock, call);
}
+ /* For allocatable arrays with nested allocatable components,
+ add_when_allocated already includes gfc_duplicate_allocatable
+ (from the recursive structure_alloc_comps call at line 10290-10293),
+ so we must not call it again here. PR121628 added an
+ add_when_allocated != NULL clause that was redundant for scalars
+ (already handled by !c->as) and wrong for arrays (double alloc). */
else if (c->attr.allocatable && !c->attr.proc_pointer
- && (add_when_allocated != NULL_TREE
- || !cmp_has_alloc_comps
+ && (!cmp_has_alloc_comps
|| !c->as
|| c->attr.codimension
|| caf_in_coarray (caf_mode)))
! This checks that the "z = y" assignment is not considered copyable, as the
! array is of a derived type containing allocatable components. Hence, we
-! we should expand the scalarized loop, which contains *two* memcpy calls
+! should expand the scalarized loop, which contains *two* memcpy calls
! for the assignment itself, plus one for initialization.
! { dg-do compile }
! { dg-options "-O2 -fdump-tree-original" }
!
! PR 121628
+! PR 123868 - fixed double allocation that caused 4 memcpy instead of 3
!
type :: a
integer, allocatable :: i(:)
z = y
end
-! { dg-final { scan-tree-dump-times "__builtin_memcpy" 4 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_memcpy" 3 "original" } }
--- /dev/null
+! { dg-do run }
+! PR fortran/123868 - Memory leak on assignment with nested allocatable
+! components. Regression introduced by PR121628 commit which caused
+! gfc_duplicate_allocatable to be called twice for allocatable array
+! components with nested allocatable components.
+
+module bugMod
+
+ type :: vs
+ character(len=1), allocatable :: s
+ end type vs
+
+ type :: ih
+ type(vs), allocatable, dimension(:) :: hk
+ end type ih
+
+end module bugMod
+
+program bugProg
+ use bugMod
+
+ block
+ type(ih) :: c, d
+
+ allocate(d%hk(1))
+ allocate(d%hk(1)%s)
+ d%hk(1)%s='z'
+ c=d
+ if (c%hk(1)%s /= 'z') stop 1
+ if (d%hk(1)%s /= 'z') stop 2
+
+ end block
+
+end program bugProg