public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [fortran,committed] Fix faulty logic in gfc_call_realloc(), PR 33592
@ 2007-11-10 23:40 FX Coudert
  0 siblings, 0 replies; only message in thread
From: FX Coudert @ 2007-11-10 23:40 UTC (permalink / raw)
  To: fortran@gcc.gnu.org List; +Cc: gcc-patches list

[-- Attachment #1: Type: text/plain, Size: 620 bytes --]

Attached patch was committed as obvious after bootstrap and  
regtesting on x86_64-linux. It fixes the logic in gfc_call_realloc(),  
where the code we emitted didn't match the expected code logic  
(indicated in a comment): we checked for (size == 0) instead of  
(size != 0). This only showed up on platforms where realloc(NULL, 0)  
returns NULL, such as hppa-hpux, where it would lead to a runtime  
failure of gfortran.dg/ array_constructor_11.f90.


2007-11-10  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>

        PR fortran/33592
        * trans.c (gfc_call_realloc): Fix the logic and rename  
variables.


[-- Attachment #2: pr33593.diff --]
[-- Type: application/octet-stream, Size: 2016 bytes --]

Index: trans.c
===================================================================
--- trans.c     (revision 129869)
+++ trans.c     (working copy)
@@ -829,19 +829,19 @@ internal_realloc (void *mem, size_t size
 {
   if (size < 0)
     runtime_error ("Attempt to allocate a negative amount of memory.");
-  mem = realloc (mem, size);
-  if (!mem && size != 0)
+  res = realloc (mem, size);
+  if (!res && size != 0)
     _gfortran_os_error ("Out of memory");
 
   if (size == 0)
     return NULL;
 
-  return mem;
+  return res;
 }  */
 tree
 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
 {
-  tree msg, res, negative, zero, null_result, tmp;
+  tree msg, res, negative, nonzero, zero, null_result, tmp;
   tree type = TREE_TYPE (mem);
 
   size = gfc_evaluate_now (size, block);
@@ -868,10 +868,10 @@ gfc_call_realloc (stmtblock_t * block, t
   gfc_add_modify_expr (block, res, fold_convert (type, tmp));
   null_result = fold_build2 (EQ_EXPR, boolean_type_node, res,
                             build_int_cst (pvoid_type_node, 0));
-  zero = fold_build2 (EQ_EXPR, boolean_type_node, size,
-                     build_int_cst (size_type_node, 0));
+  nonzero = fold_build2 (NE_EXPR, boolean_type_node, size,
+                        build_int_cst (size_type_node, 0));
   null_result = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, null_result,
-                            zero);
+                            nonzero);
   msg = gfc_build_addr_expr (pchar_type_node,
                             gfc_build_cstring_const ("Out of memory"));
   tmp = fold_build3 (COND_EXPR, void_type_node, null_result,
@@ -881,6 +881,7 @@ gfc_call_realloc (stmtblock_t * block, t
 
   /* if (size == 0) then the result is NULL.  */
   tmp = fold_build2 (MODIFY_EXPR, type, res, build_int_cst (type, 0));
+  zero = fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, nonzero);
   tmp = fold_build3 (COND_EXPR, void_type_node, zero, tmp,
                     build_empty_stmt ());
   gfc_add_expr_to_block (block, tmp);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2007-11-10 18:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-10 23:40 [fortran,committed] Fix faulty logic in gfc_call_realloc(), PR 33592 FX Coudert

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).