[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: xmalloc.c's xcalloc performs unnecessary test for N*S overflow
From: |
Paul Eggert |
Subject: |
Re: xmalloc.c's xcalloc performs unnecessary test for N*S overflow |
Date: |
Thu, 16 Jun 2005 17:15:35 -0700 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux) |
Jim Meyering <address@hidden> writes:
> As it stands, if I use both of the xalloc and calloc modules,
> calling xcalloc ends up performing the overflow check twice,
> first in xcalloc itself (above), and then again in calloc.
Also, xcalloc contains a test n != 0 that isn't needed when
rpl_calloc is being called.
How about the following (untested) change instead? It omits the tests
when they're unnecessary, but it doesn't establish a dependency of
xalloc on calloc. I'd rather leave out dependencies like that if it's
easy.
2005-06-16 Paul Eggert <address@hidden>
* xmalloc (HAVE_CALLOC): Define to 1 if not defined.
(xcalloc): Omit needless tests if ! HAVE_CALLOC.
--- xmalloc.c 2005-05-13 23:03:58 -0700
+++ /tmp/xmalloc.c 2005-06-16 17:14:13 -0700
@@ -30,6 +30,10 @@
# define SIZE_MAX ((size_t) -1)
#endif
+#ifndef HAVE_CALLOC
+# define HAVE_CALLOC 1
+#endif
+
/* Allocate an array of N objects, each with S bytes of memory,
dynamically, with error checking. S must be nonzero. */
@@ -204,8 +208,11 @@ xcalloc (size_t n, size_t s)
{
void *p;
/* Test for overflow, since some calloc implementations don't have
- proper overflow checks. */
- if (xalloc_oversized (n, s) || (! (p = calloc (n, s)) && n != 0))
+ proper overflow checks. Omit some tests if !HAVE_CALLOC, since
+ rpl_calloc catches overflow and never returns NULL if
+ successful. */
+ if ((HAVE_CALLOC && xalloc_oversized (n, s))
+ || (! (p = calloc (n, s)) && ! (HAVE_CALLOC && n == 0)))
xalloc_die ();
return p;
}