Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Kevin Buettner <kevinb@cygnus.com>
To: gdb-patches@sources.redhat.com
Subject: [PATCH RFA #2] utils.c: Fix xcalloc (0, 0) behavior
Date: Mon, 05 Mar 2001 16:06:00 -0000	[thread overview]
Message-ID: <1010306000635.ZM8056@ocotillo.lan> (raw)

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;
 }
 


             reply	other threads:[~2001-03-05 16:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-05 16:06 Kevin Buettner [this message]
2001-03-05 17:11 ` Kevin Buettner
2001-03-06 14:27 ` Andrew Cagney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1010306000635.ZM8056@ocotillo.lan \
    --to=kevinb@cygnus.com \
    --cc=gdb-patches@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox