public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch, fortran v2] PR fortran/92621 Problems with memory handling with allocatable intent(out) arrays with bind(c)
@ 2021-04-26 11:21 José Rui Faustino de Sousa
  2021-06-14 15:32 ` PING: " José Rui Faustino de Sousa
  0 siblings, 1 reply; 2+ messages in thread
From: José Rui Faustino de Sousa @ 2021-04-26 11:21 UTC (permalink / raw)
  To: fortran, gcc-patches

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.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* PING: [Patch, fortran v2] PR fortran/92621 Problems with memory handling with allocatable intent(out) arrays with bind(c)
  2021-04-26 11:21 [Patch, fortran v2] PR fortran/92621 Problems with memory handling with allocatable intent(out) arrays with bind(c) José Rui Faustino de Sousa
@ 2021-06-14 15:32 ` José Rui Faustino de Sousa
  0 siblings, 0 replies; 2+ messages in thread
From: José Rui Faustino de Sousa @ 2021-06-14 15:32 UTC (permalink / raw)
  To: fortran, gcc-patches

*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 <jrfsousa@gmail.com>
To: fortran@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.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-06-14 15:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-26 11:21 [Patch, fortran v2] PR fortran/92621 Problems with memory handling with allocatable intent(out) arrays with bind(c) José Rui Faustino de Sousa
2021-06-14 15:32 ` PING: " José Rui Faustino de Sousa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).