From 0f344846a62c8863375909d8d6b435b4b5fd35a0 Mon Sep 17 00:00:00 2001 From: Andre Vehreschild Date: Thu, 20 Mar 2025 13:37:21 +0100 Subject: Fortran: Fix double free on polymorphic array dummy argument [PR119349] Calling elemental routines with polymorphic formals leads to generation of a temporary polymorphic variable and code for its deallocation. Sourcing this element from an array constructor the latter now is prevented from generating a second deallocation. PR fortran/119349 gcc/fortran/ChangeLog: * trans-expr.cc (gfc_conv_procedure_call): Prevent deallocation of array temporary for polymorphic temporary argument. gcc/testsuite/ChangeLog: * gfortran.dg/class_79.f90: New test. --- gcc/fortran/trans-expr.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'gcc/fortran/trans-expr.cc') diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index d965539..923d46c 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -7994,7 +7994,11 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, gfc_add_expr_to_block (&se->post, local_tmp); } - if (!finalized && !e->must_finalize) + /* Items of array expressions passed to a polymorphic formal arguments + create their own clean up, so prevent double free. */ + if (!finalized && !e->must_finalize + && !(e->expr_type == EXPR_ARRAY && fsym + && fsym->ts.type == BT_CLASS)) { bool scalar_res_outside_loop; scalar_res_outside_loop = e->expr_type == EXPR_FUNCTION -- cgit v1.1