See attached patch. For p1 = toto(p1), where toto deallocates and
reallocates a component of its dummy, gfortran saves the components of
p1 before the call and frees them after the assignment. The call has
already freed them, so they are freed twice, and the components toto
stored in p1 leak. The patch evaluates the call before the components
are saved when an actual argument overlaps the lhs, as decided by
gfc_check_dependency so that w%c = f(w) and w = f(w%c) are caught too,
and its dummy is not INTENT(IN) or VALUE. -O2 code for other calls is
unchanged.
Such an assignment is not standard conforming (F2023 10.1.4 p2), but it
should not corrupt the heap. The test is in gfortran.dg/asan because
glibc usually reuses the freed block, so without -fsanitize=address it
passes on trunk.
The fix is rhs_may_redefine_lhs in trans-expr.cc and its use in
gfc_trans_assignment_1; the rest is the new test.
Regression tested on x86_64.
OK for mainline?
Regards,
Jerry
---
fortran: [PR52774] Double free when a function call redefines the lhs
For lhs = f(...) with an lhs that has allocatable components, gfortran
saved the lhs components before calling f and freed them after the
assignment. When f redefined the lhs through an actual argument that
overlaps it, f had already freed the saved components, so they were
freed twice, and the ones f left in the lhs leaked. Evaluate such a
call before the lhs components are saved. The assignment violates
F2023 10.1.4 p2, which is not a constraint, so no diagnostic is added.
Assisted-by: Claude Opus 5.5
PR fortran/52774
gcc/fortran/ChangeLog:
* trans-expr.cc (rhs_may_redefine_lhs): New function.
(gfc_trans_assignment_1): Evaluate an rhs that may redefine the lhs
before the lhs components are saved for deallocation.
gcc/testsuite/ChangeLog:
* gfortran.dg/asan/pr52774.f90: New test.
>From 6845af173487beb53d68899305b3fa7369a93c78 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 14 Nov 2026 19:40:04 -0800
Subject: [PATCH] fortran: [PR52774] Double free when a function call redefines
the lhs
For lhs = f(...) with an lhs that has allocatable components, gfortran
saved the lhs components before calling f and freed them after the
assignment. When f redefined the lhs through an actual argument that
overlaps it, f had already freed the saved components, so they were
freed twice, and the ones f left in the lhs leaked. Evaluate such a
call before the lhs components are saved. The assignment violates
F2023 10.1.4 p2, which is not a constraint, so no diagnostic is added.
Assisted-by: Claude Opus 5.5
PR fortran/52774
gcc/fortran/ChangeLog:
* trans-expr.cc (rhs_may_redefine_lhs): New function.
(gfc_trans_assignment_1): Evaluate an rhs that may redefine the lhs
before the lhs components are saved for deallocation.
gcc/testsuite/ChangeLog:
* gfortran.dg/asan/pr52774.f90: New test.
---
gcc/fortran/trans-expr.cc | 37 ++++++++++++++++
gcc/testsuite/gfortran.dg/asan/pr52774.f90 | 49 ++++++++++++++++++++++
2 files changed, 86 insertions(+)
create mode 100644 gcc/testsuite/gfortran.dg/asan/pr52774.f90
diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 08428820fea..144b188ed3a 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -13801,6 +13801,37 @@ is_assoc_assign (gfc_expr *lhs, gfc_expr *rhs)
&& lhs->symtree->n.sym->assoc->target == rhs;
}
+/* Return true if RHS is a function reference with an actual argument that
+ overlaps LHS and whose dummy may be redefined. */
+
+static bool
+rhs_may_redefine_lhs (gfc_expr *lhs, gfc_expr *rhs)
+{
+ if (rhs->expr_type != EXPR_FUNCTION || rhs->value.function.isym)
+ return false;
+
+ gfc_symbol *esym = rhs->value.function.esym;
+ gfc_formal_arglist *f = esym ? esym->formal : NULL;
+
+ for (gfc_actual_arglist *a = rhs->value.function.actual; a; a = a->next)
+ {
+ gfc_symbol *dummy = f ? f->sym : NULL;
+
+ /* Without a dummy to inspect, assume the worst. */
+ if ((!dummy
+ || (dummy->attr.intent != INTENT_IN && !dummy->attr.value))
+ && a->expr && a->expr->expr_type == EXPR_VARIABLE
+ && gfc_check_dependency (lhs, a->expr, false))
+ return true;
+
+ if (f)
+ f = f->next;
+ }
+
+ return false;
+}
+
+
/* Subroutine of gfc_trans_assignment that actually scalarizes the
assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
init_flag indicates initialization expressions and dealloc that no
@@ -14035,6 +14066,12 @@ gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
}
+ /* The lhs components to free are read after the rhs call. */
+ if (gfc_bt_struct (expr1->ts.type)
+ && rhs_may_redefine_lhs (expr1, expr2)
+ && !TREE_CONSTANT (rse.expr) && !VAR_P (rse.expr))
+ rse.expr = gfc_evaluate_now (rse.expr, &rse.pre);
+
/* Stabilize a string length for temporaries. */
if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
&& !(VAR_P (rse.string_length)
diff --git a/gcc/testsuite/gfortran.dg/asan/pr52774.f90 b/gcc/testsuite/gfortran.dg/asan/pr52774.f90
new file mode 100644
index 00000000000..53ed99fcada
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/asan/pr52774.f90
@@ -0,0 +1,49 @@
+! { dg-do run }
+!
+! PR 52774
+
+module m
+ type po
+ real(8), allocatable :: p(:)
+ end type
+ type wrap
+ type(po) :: c
+ integer :: k = 0
+ end type
+contains
+ function toto (dd) result (y)
+ type(po) :: dd, y
+ deallocate (dd%p)
+ dd%p = [1d0, 2d0]
+ y = dd
+ end function
+ function parent (w) result (y)
+ type(wrap) :: w
+ type(po) :: y
+ deallocate (w%c%p)
+ w%c%p = [3d0]
+ y = w%c
+ end function
+ function child (c) result (y)
+ type(po) :: c
+ type(wrap) :: y
+ deallocate (c%p)
+ c%p = [4d0, 5d0]
+ y%c = c
+ y%k = 6
+ end function
+end module
+
+program p
+ use m
+ type(po) :: p1
+ type(wrap) :: w
+ allocate (p1%p(1))
+ p1 = toto (p1)
+ if (any (p1%p /= [1d0, 2d0])) stop 1
+ allocate (w%c%p(2))
+ w%c = parent (w)
+ if (any (w%c%p /= [3d0])) stop 2
+ w = child (w%c)
+ if (any (w%c%p /= [4d0, 5d0]) .or. w%k /= 6) stop 3
+end program
--
2.56.0