*PING*

-------- Forwarded Message --------
Subject: [Patch, fortran v2] PR fortran/92621 Problems with memory handling with allocatable intent(out) arrays with bind(c)
Date: Mon, 26 Apr 2021 11:21:25 +0000
From: José Rui Faustino de Sousa <jrfso...@gmail.com>
To: fort...@gcc.gnu.org, gcc-patches@gcc.gnu.org

Hi all!

Proposed patch to:

PR92621 - Problems with memory handling with allocatable intent(out) arrays with bind(c)

Patch tested only on x86_64-pc-linux-gnu.

The code currently generated tries to deallocate the undefined artificial cfi.n pointer before it is associated with the allocatable array.

Since the cfi.n pointer is undefined attempting to free it is really a bad idea and it will frequently segfault.

Consequently, since the deallocation is done before the cfi.n pointer is associated with anything, the allocatable array is never freed, like it should, and it will be passed still allocated causing subsequent attempts to allocate it to fail.

Version 2 is basically a ping, fixes a typo, replaces an if block with a flag to make reviewing easier and replaces a call to malloc with calloc to make Valgrind happy.

Thank you very much.

Best regards,
José Rui

Fortran: Fix segfaults due to freeing undefined pointer [PR92621]

gcc/fortran/ChangeLog:

        PR fortran/92621
        * trans-expr.c (gfc_conv_gfc_desc_to_cfi_desc): Add code to
        deallocate allocatable intent(out) dummy array arguments and
        slightly rearrange code.
        (gfc_conv_procedure_call): Add a flag to avoid double frees,
        removes unnecessary checks for bind(c) objects and obsolete
        comments.

libgfortran/ChangeLog:

        PR fortran/92621
        * runtime/ISO_Fortran_binding.c (gfc_desc_to_cfi_desc): replaces
        a call to malloc with calloc to make Valgrind happy.

gcc/testsuite/ChangeLog:

        PR fortran/92621
        * gfortran.dg/bind-c-intent-out.f90: Changes regex to match the
        changes in code generation.
        * gfortran.dg/PR92621.f90: Improved new test.

Reply via email to