https://gcc.gnu.org/g:1f03545576b68402efda98a963a0195d9467ca63

commit r17-6329-g1f03545576b68402efda98a963a0195d9467ca63
Author: Jerry DeLisle <[email protected]>
Date:   Sat Nov 14 19:40:04 2026 -0800

    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.

Diff:
---
 gcc/fortran/trans-expr.cc                  | 37 ++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/asan/pr52774.f90 | 49 ++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index bea545dd67db..61130209f49b 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -13807,6 +13807,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
@@ -14041,6 +14072,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 000000000000..53ed99fcada6
--- /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

Reply via email to