This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


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

[PATCH RFA #2] utils.c: Fix xcalloc (0, 0) behavior


The patch below supercedes the patch that I submitted in

    http://sources.redhat.com/ml/gdb-patches/2001-03/msg00052.html

The patch below is much more aggressive in that it will cause NULL to
be returned for *all* platforms when a zero-sized request is made to
one of GDB's memory allocation functions.  The patch in the message
referenced above only returned NULL when the underlying allocation
functions (upon which GDB's are implemented) chose to return NULL.  I
think the consensus opinion is that it would be better to make the
behavior consistent across platforms so that bugs will be caught
sooner.

I have tested this patch on i386-unknown-freebsd4.2 and
i686-pc-linux-gnu and do not see any regressions.

	* utils.c (xmrealloc, xcalloc): Return NULL for zero-sized requests.

Index: utils.c
===================================================================
RCS file: /cvs/src/src/gdb/utils.c,v
retrieving revision 1.31
diff -u -p -r1.31 utils.c
--- utils.c	2001/02/25 04:45:11	1.31
+++ utils.c	2001/03/05 23:24:05
@@ -1042,18 +1042,27 @@ xmrealloc (PTR md, PTR ptr, long size)
 {
   register PTR val;
 
-  if (ptr != NULL)
+  if (size == 0)
     {
-      val = mrealloc (md, ptr, size);
+      if (ptr != NULL)
+	mfree (md, ptr);
+      val = NULL;
     }
   else
     {
-      val = mmalloc (md, size);
+      if (ptr != NULL)
+	{
+	  val = mrealloc (md, ptr, size);
+	}
+      else
+	{
+	  val = mmalloc (md, size);
+	}
+      if (val == NULL)
+	{
+	  nomem (size);
+	}
     }
-  if (val == NULL)
-    {
-      nomem (size);
-    }
   return (val);
 }
 
@@ -1071,9 +1080,16 @@ xmalloc (size_t size)
 PTR
 xcalloc (size_t number, size_t size)
 {
-  void *mem = mcalloc (NULL, number, size);
-  if (mem == NULL)
-    nomem (number * size);
+  void *mem;
+
+  if (number == 0 || size == 0)
+    mem = NULL;
+  else
+    {
+      mem = mcalloc (NULL, number, size);
+      if (mem == NULL)
+	nomem (number * size);
+    }
   return mem;
 }
 


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