https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125535

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-16 branch has been updated by Jerry DeLisle
<[email protected]>:

https://gcc.gnu.org/g:d1750d8df75f292a0111e94dda82664c988bcc60

commit r16-9101-gd1750d8df75f292a0111e94dda82664c988bcc60
Author: Jerry DeLisle <[email protected]>
Date:   Fri Jun 5 13:20:05 2026 -0700

    fortran: wrong-code for implied-do with allocatable-component derived type

    When a nested implied-do array constructor called a transformational
    intrinsic (e.g. RESHAPE) whose result type has allocatable components, the
    argument temporaries were freed before the result's allocatable components
    were deep-copied, causing a heap-use-after-free and wrong runtime values.

    The root cause: in gfc_conv_procedure_call, the call to
    gfc_add_block_to_block (&se->pre, &post) â which frees the allocatable
    components of argument temporaries â was issued before the deep-copy loop
    for the result's allocatable components.  Transformational library
functions
    such as RESHAPE do a shallow byte-copy of the source array into the result,
    so the result's component pointers alias those of the argument temporaries.
    Freeing the temporaries first and then copying yielded use-after-free.

    Fix: move gfc_add_block_to_block (&se->pre, &post) to after the deep-copy
    loop, so that the result's allocatable components are copied while the
    source storage is still live.

    PR fortran/125535

    Assisted-by: Claude Sonnet 4.6

    gcc/fortran/ChangeLog:

            PR fortran/125535
            * trans-expr.cc (gfc_conv_procedure_call): Move post block append
            to after the deep copy of allocatable components for
transformational
            intrinsics, so that argument temporaries are not freed before the
            result components are copied.

    gcc/testsuite/ChangeLog:

            PR fortran/125535
            * gfortran.dg/implied_do_alloc_comp_1.f90: New test.

    (cherry picked from commit a0d7e1c3639a6e19f4413bf53bb1e994494ec77f)

Reply via email to