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

Fix double free in assignment with redefining function calls

Prevents double frees and memory leaks in Fortran assignments involving function calls that redefine the left-hand side.

This commit addresses a double-free bug in GCC’s Fortran compiler (gfortran) that occurred during assignments where the right-hand side was a function call that redefined the left-hand side (LHS). If the LHS had allocatable components, gfortran would save and then free them after the assignment. If the function call modified the LHS, it could free already-freed components, leading to a double free, and leak the components remaining in the LHS. The fix evaluates the function call before saving the LHS components, resolving the issue.

In Details

In gfc_trans_assignment_1 within gcc/fortran/trans-expr.cc, when handling an assignment lhs = f(...) where lhs has allocatable components, the existing code first saves lhs components for deallocation and then calls f. If f redefines lhs (e.g., via an actual argument that overlaps lhs), and f also deallocates components of lhs, a double free occurs. This patch introduces rhs_may_redefine_lhs to check for such scenarios and evaluates the RHS call *before* saving lhs components for deallocation, preventing the double free. The assignment violates F2023 10.1.4 p2 but is…

For Context
allocatable
A Fortran attribute that allows a variable (typically an array or derived type component) to be dynamically allocated and deallocated during program execution.
LHS
Left-Hand Side, the variable or expression on the left side of an assignment operator.
RHS
Right-Hand Side, the expression or value on the right side of an assignment operator.
Fortran 2023
The latest standard for the Fortran programming language, defining its syntax, semantics, and intrinsic procedures.
Filed Under: fortranmemory-managementbugfix