GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc/fortran

Fortran PDT LEN storage freed only on deallocation.

Fortran compiler now correctly manages memory for CHARACTER TARGET dummy arguments to prevent use-after-free errors.

A change in GCC’s Fortran compiler (since r17-6306) caused the finalization wrapper of a Procedure-Derived Type (PDT) to free its LEN-dependent components prematurely. This led to use-after-free errors when these components were still in use, such as with INTENT(OUT) actual arguments. The fix ensures these components are freed only during actual deallocation, mirroring the handling of coarray components.

In Details

This commit addresses a memory management issue in gcc/fortran/class.cc and gcc/fortran/trans.cc related to Procedure-Derived Types (PDTs) with LEN dependent components. Previously, the finalization wrapper unconditionally freed LEN-dependent storage. The change introduces a fini_coarray flag to gfc_add_finalizer_call and related functions. This flag, when true, triggers the deallocation of LEN-dependent components, ensuring they are only freed when the object is truly deallocated, thus preventing use-after-free bugs for arguments like INTENT(OUT) dummies.

For Context
PDT
Procedure-Derived Type, a derived type in Fortran whose components are initialized or finalized by procedures.
LEN
In Fortran, a keyword used to specify the length of character variables or to define deferred-length components in derived types.
INTENT(OUT)
A Fortran procedure argument attribute indicating that the argument is an output parameter, whose value will be assigned within the procedure.
use-after-free
A type of memory corruption bug where a program attempts to access memory after it has been freed, leading to unpredictable behavior or crashes.
coarray
A Fortran language feature that provides built-in support for parallel programming, allowing programs to communicate between images (processes).
Filed Under: fortranmemory-managementcompiler-bug