Hi Jerry!
Am 22.06.26 um 8:37 PM schrieb Jerry D:
See attached patch.
Regression tested on x86_64.
A new test case is added to asan to catch this in the future.
I'd rather leave the review to Paul, but I have one question:
As is, the new helper has_class_alloc_comp might return true for
pointer components, but I think it should not.
Can you please check?
Cheers,
Harald
OK for mainline.
Regards,
Jerry
---
fortran: Fix memory leak for array structure-constructor element [PR121972]
Fix the remaining leak in pdt_86.f03. Added a new test in asan to avoid it.
PR fortran/121972
gcc/fortran/ChangeLog:
* trans-array.cc (has_class_alloc_comp): New helper; returns true if
derived type DER has any CLASS component.
(gfc_trans_array_ctor_element): Also free allocatable components when
the element expression is EXPR_STRUCTURE, skipping types that contain
CLASS components to avoid freeing stack-allocated _data pointers.
(gfc_constructor_is_owned_alloc_comp): Likewise treat EXPR_STRUCTURE
elements as owned only when the derived type has no CLASS components.
* trans-expr.cc (gfc_trans_alloc_subarray_assign): Free allocatable
components of the component's temporary descriptor before nulling its
data pointer, for non-variable source expressions.
gcc/testsuite/ChangeLog:
* gfortran.dg/class_array_15.f03: Adjust expected free count.
* gfortran.dg/derived_constructor_comps_6.f90: Adjust expected free
count.
* gfortran.dg/asan/structure_constructor_alloc_comp_leak_1.f90: New
test.
---