bug4-dealloc-scope-exit.f90 fails in runtime with: Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error: #0 0x7f2b83809803 in ??? #1 0x7f2b838083b5 in ??? #2 0x7f2b834dcfaf in ??? #3 0x40066f in n1 at /home/pault/prs/pdt_conformance/reducers/bug4-dealloc-scope-exit.f90:14 #4 0x4006c9 in main at /home/pault/prs/pdt_conformance/reducers/bug4-dealloc-scope-exit.f90:14 Segmentation fault (core dumped) ./a.out The patch is straightforward enough as not to need explanation. Passes regtesting on FC44/x86_64. OK for mainline. Cheers Paul
From b308d107d4ac32e348e2d728764e3910af9c3a92 Mon Sep 17 00:00:00 2001 From: Paul Thomas <[email protected]> Date: Thu, 8 Oct 2026 09:58:52 +0100 Subject: [PATCH] Fortran: [meta-bug] [PDT] Parameterized derived type errors [PR82173] 2026-10-06 Paul Thomas <[email protected]> gcc/fortran PR fortran/82173 * trans-decl.cc (gfc_trans_deferred_vars): Small formatting adjustment followed by a guarding of the pdt component dealloc for allocatable variables. gcc/testsuite/ PR fortran/82173 * gfortran.dg/pdt_bug4.f90: New test. --- gcc/fortran/trans-decl.cc | 16 ++++++++++++++++ gcc/testsuite/gfortran.dg/pdt_bug4.f90 | 14 ++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/pdt_bug4.f90 diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc index a614e71eced..0229cc7ab82 100644 --- a/gcc/fortran/trans-decl.cc +++ b/gcc/fortran/trans-decl.cc @@ -5179,6 +5179,22 @@ gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block) else tmp = NULL_TREE; + if (sym->attr.allocatable && !sym->attr.result + && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (sym->backend_decl))) + { + /* Only deallocate the PDT comonents if the variable has not + been deallocated. Arrays are guarded elsewhere. */ + tree null_cond, zero; + null_cond = sym->backend_decl; + zero = build_int_cst (TREE_TYPE (null_cond), 0); + null_cond = fold_build2_loc (input_location, NE_EXPR, + logical_type_node, + null_cond, zero); + tmp = build3_loc (input_location, COND_EXPR, void_type_node, + null_cond, tmp, + build_empty_stmt (input_location)); + } + gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), tmp); } else if (sym->attr.dummy) diff --git a/gcc/testsuite/gfortran.dg/pdt_bug4.f90 b/gcc/testsuite/gfortran.dg/pdt_bug4.f90 new file mode 100644 index 00000000000..32bd4b1607a --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pdt_bug4.f90 @@ -0,0 +1,14 @@ +! { dg-do run } +! Used to seg fault on exit due to attempted double free of pdt_array components. +! See attachment to PR82173 comment 10 - reproducers/bug4-dealloc-scope-exit.f90 +program n1 + type :: t(n) + integer, len :: n = 1 + integer :: v(n) + end type + type(t(3)), allocatable :: b ! NON-deferred len, allocatable + allocate (b) + b%v = 1 + deallocate (b) + print *, 'reached end' +end program ! Segmentation fault - invalid memory reference -- 2.55.0
