diff mbox

[Fortran] Realloc on assignment: Allocate at least a byte

Message ID 51C46DD8.2010206@net-b.de
State New
Headers show

Commit Message

Tobias Burnus June 21, 2013, 3:14 p.m. UTC
In order to ensure that "ALLOCATED(var)" works, we have to allocate at 
least one byte. While malloc(0) is permitted, it is system depended 
whether it returns NULL or a unique non-NULL pointer.

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

Tobias

Comments

Mikael Morin June 21, 2013, 7:12 p.m. UTC | #1
Le 21/06/2013 17:14, Tobias Burnus a écrit :
> In order to ensure that "ALLOCATED(var)" works, we have to allocate at
> least one byte. While malloc(0) is permitted, it is system depended
> whether it returns NULL or a unique non-NULL pointer.
> 
> Build and regtested on x86-64-gnu-linux.
> OK for the trunk?
> 
Sure.

Mikael
diff mbox

Patch

2013-06-21  Tobias Burnus  <burnus@net-b.de>

	* trans-array.c (gfc_alloc_allocatable_for_assignment): Allocate
	at least one byte.
	* trans-expr.c (alloc_scalar_allocatable_for_assignment): Ditto.

2013-06-21  Tobias Burnus  <burnus@net-b.de>

	* gfortran.dg/realloc_on_assign_18.f90: New.

diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c
index a4321cc..24e5aa3 100644
--- a/gcc/fortran/trans-array.c
+++ b/gcc/fortran/trans-array.c
@@ -8209,6 +8209,8 @@  gfc_alloc_allocatable_for_assignment (gfc_loopinfo *loop,
 			   gfc_array_index_type,
 			   tmp, size2);
   size2 = fold_convert (size_type_node, size2);
+  size2 = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
+			   size2, size_one_node);
   size2 = gfc_evaluate_now (size2, &fblock);
 
   /* Realloc expression.  Note that the scalarizer uses desc.data
diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index bd8886c..56dc766 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -7574,6 +7574,9 @@  alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
       size_in_bytes = size;
     }
 
+  size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
+				   size_in_bytes, size_one_node);
+
   if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
     {
       tmp = build_call_expr_loc (input_location,
--- /dev/null	2013-06-21 09:21:05.672079164 +0200
+++ gcc/gcc/testsuite/gfortran.dg/realloc_on_assign_18.f90	2013-06-21 15:55:44.729537597 +0200
@@ -0,0 +1,20 @@ 
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+!
+! Ensure that for zero-sized array, nonzero memory is allocated
+!
+type t
+end type t
+
+type(t), allocatable :: x, y(:)
+
+x = t()
+y = [ t :: ]
+
+if (.not. allocated (x)) call abort ()
+if (.not. allocated (y)) call abort ()
+end
+
+! { dg-final { scan-tree-dump "x = \\(struct t .\\) __builtin_malloc \\(1\\);" "original" } }
+! { dg-final { scan-tree-dump "y.data = \\(void . restrict\\) __builtin_malloc \\(1\\);" "original" } }
+! { dg-final { cleanup-tree-dump "original" } }