From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kevin Buettner 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 Message-id: <1010306000635.ZM8056@ocotillo.lan> X-SW-Source: 2001-03/msg00094.html 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; }