On 11/16/26 12:41 PM, Mikael Morin wrote:
Le 15/11/2026 à 04:52, Jerry DeLisle a écrit :
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.
OK.
Thanks for review.
The master branch has been updated by Jerry DeLisle <[email protected]>:
https://gcc.gnu.org/g:1f03545576b68402efda98a963a0195d9467ca63
commit r17-6329-g1f03545576b68402efda98a963a0195d9467ca63
- Jerry