diff mbox

[Fortran] PR 51869 - fix realloc on assignment issue

Message ID 4F155A84.8090805@net-b.de
State New
Headers show

Commit Message

Tobias Burnus Jan. 17, 2012, 11:24 a.m. UTC
This patch nullifies (scalar) allocatables after malloc, if (and only 
if) they contain allocatable components to make sure that no 
uninitialized memory is accessed.

Build and regtested on x86-64-linux.
OK for the trunk?

Tobias

Comments

Janne Blomqvist Jan. 17, 2012, 11:45 a.m. UTC | #1
On Tue, Jan 17, 2012 at 13:24, Tobias Burnus <burnus@net-b.de> wrote:
> This patch nullifies (scalar) allocatables after malloc, if (and only if)
> they contain allocatable components to make sure that no uninitialized
> memory is accessed.
>
> Build and regtested on x86-64-linux.
> OK for the trunk?

calloc potentially has better performance than malloc+memset, and has
smaller generated code size too.
Paul Richard Thomas Jan. 17, 2012, 12:29 p.m. UTC | #2
Dear Tobias and Janne,

I had preparedand was about to submit the identical patch :-)

On Tue, Jan 17, 2012 at 12:45 PM, Janne Blomqvist
<blomqvist.janne@gmail.com> wrote:
> On Tue, Jan 17, 2012 at 13:24, Tobias Burnus <burnus@net-b.de> wrote:
>> This patch nullifies (scalar) allocatables after malloc, if (and only if)
>> they contain allocatable components to make sure that no uninitialized
>> memory is accessed.
>>
>> Build and regtested on x86-64-linux.
>> OK for the trunk?
>
> calloc potentially has better performance than malloc+memset, and has
> smaller generated code size too.

On a previous occasion (for class array allocation), I looked for and
was unable to find a __BUILTIN_CALLOC.  If it exists, use that.

Otherwise, OK for trunk.

Thanks to you both.

Paul
diff mbox

Patch

2012-01-17  Tobias Burnus  <burnus@net-b.de>

	PR fortran/51869
	* trans-expr.c (alloc_scalar_allocatable_for_assignment): Nullify
	LHS after allocation, if it has allocatable components.

2012-01-17  Tobias Burnus  <burnus@net-b.de>

	PR fortran/51869
	* gfortran.dg/realloc_on_assign_9.f90: New.

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 14411e0..c709265 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -6590,6 +6597,15 @@  alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
 			     1, size_in_bytes);
   tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
   gfc_add_modify (block, lse.expr, tmp);
+
+  if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
+    {
+      tmp = build_call_expr_loc (input_location,
+				 builtin_decl_explicit (BUILT_IN_MEMSET),
+				 3, lse.expr, integer_zero_node, size_in_bytes);
+      gfc_add_expr_to_block (block, tmp);
+    }
+
   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
     {
       /* Deferred characters need checking for lhs and rhs string
--- /dev/null	2012-01-17 08:41:28.951768065 +0100
+++ gcc/gcc/testsuite/gfortran.dg/realloc_on_assign_9.f90	2012-01-17 11:41:41.000000000 +0100
@@ -0,0 +1,34 @@ 
+! { dg-do run }
+!
+! PR fortran/51869
+!
+module soop_stars_class
+  implicit none
+  type soop_stars
+    real ,dimension(:,:) ,allocatable :: position
+  end type
+  type show
+    type(soop_stars) :: rocket
+  end type
+contains
+  function new_show(boom)
+    type(soop_stars) ,intent(in) :: boom
+    type(show) :: new_show
+    new_show%rocket = boom
+  end function
+end module
+
+program main
+  use soop_stars_class
+  implicit none
+
+  type(soop_stars) :: fireworks
+  type(show), allocatable :: july4
+
+  allocate (fireworks%position(2,2))
+  fireworks%position = 33.0
+
+  july4 = new_show(boom=fireworks)
+end program
+
+! { dg-final { cleanup-modules "soop_stars_class" } }