This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch,Fortran] RFC - PR42958 - cleanup temporary var malloc


Hi all,

currently, for temporary arrays the following happens:

(1)  bounds = (ubound - lbound + 1)
(2)  size = bounds < 0 ? 0 : bounds * 8
(2) gfc_call_malloc (...., size)

where "8" in this example is the byte size.

gfc_call_malloc does:

(a) if (size < 0) error "Negative Memory"
(b) ptr =  malloc ( MAX(1, size))
(c) if (ptr == NULL) error "Could not allocate"

The overflow check at (a) does not work reliable - while for 32bit there
is the chance that it can find problems (i.e. for arrays which are
slightly too large), for 64bit it is rather unlikely to be useful. The
patch removes this overflow check.

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

 * * *

Note 1: One should consider to make the error check (c) optional;
though, the question is on which option it should depend -- on -fcheck=
or -O<n> or ...?
Maybe   if (flag.O == 0 || (flag.fcheck & <something>)), though the
question is what "something" should be.

Note 2: The "MAX (1, size)" is needed to make "allocated
(zero_sized_array)" possible; it will be obsoleted by the array
descriptor patch; the allocation would be then:

if (size > 0) {
  desc.ptr = malloc (size)
  if (desc.ptr == NULL) error "could not allocate"
}
desc.allocated = true
-- or with stat= --
desc.allocated = true
if (size > 0) {
  desc.ptr = malloc (size)
  if (desc.ptr == NULL) { desc.stat = error_status; desc.allocated = false }
}

Tobias
2010-02-17  Tobias Burnus  <burnus@net-b.de>

	PR fortran/42958
	* trans.c (gfc_call_malloc): Remove negative-size run-time error.

Index: gcc/fortran/trans.c
===================================================================
--- gcc/fortran/trans.c	(Revision 156815)
+++ gcc/fortran/trans.c	(Arbeitskopie)
@@ -497,13 +497,12 @@ gfc_trans_runtime_check (bool error, boo
 
 
 /* Call malloc to allocate size bytes of memory, with special conditions:
-      + if size < 0, generate a runtime error,
-      + if size == 0, return a malloced area of size 1,
+      + if size <= 0, return a malloced area of size 1,
       + if malloc returns NULL, issue a runtime error.  */
 tree
 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
 {
-  tree tmp, msg, negative, malloc_result, null_result, res;
+  tree tmp, msg, malloc_result, null_result, res;
   stmtblock_t block2;
 
   size = gfc_evaluate_now (size, block);
@@ -514,18 +513,7 @@ gfc_call_malloc (stmtblock_t * block, tr
   /* Create a variable to hold the result.  */
   res = gfc_create_var (prvoid_type_node, NULL);
 
-  /* size < 0 ?  */
-  negative = fold_build2 (LT_EXPR, boolean_type_node, size,
-			  build_int_cst (size_type_node, 0));
-  msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
-      ("Attempt to allocate a negative amount of memory."));
-  tmp = fold_build3 (COND_EXPR, void_type_node, negative,
-		     build_call_expr_loc (input_location,
-				      gfor_fndecl_runtime_error, 1, msg),
-		     build_empty_stmt (input_location));
-  gfc_add_expr_to_block (block, tmp);
-
-  /* Call malloc and check the result.  */
+  /* Call malloc.  */
   gfc_start_block (&block2);
 
   size = fold_build2 (MAX_EXPR, size_type_node, size,
@@ -535,6 +523,8 @@ gfc_call_malloc (stmtblock_t * block, tr
 		  fold_convert (prvoid_type_node,
 				build_call_expr_loc (input_location,
 				   built_in_decls[BUILT_IN_MALLOC], 1, size)));
+
+  /* Check whether malloc was successful.  */
   null_result = fold_build2 (EQ_EXPR, boolean_type_node, res,
 			     build_int_cst (pvoid_type_node, 0));
   msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
@@ -553,6 +543,7 @@ gfc_call_malloc (stmtblock_t * block, tr
   return res;
 }
 
+
 /* Allocate memory, using an optional status argument.
  
    This function follows the following pseudo-code:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]