2016-12-15 Paul Eggert <eggert@cs.ucla.edu>
+ safe-alloc: use xalloc-oversized
+ * lib/safe-alloc.c: Include xalloc-oversized.h.
+ (safe_alloc_oversized): Remove. All uses changed to xalloc_oversized.
+ * modules/safe-alloc (Depends-on): Add xalloc-oversized.
+
xalloc: do not exceed PTRDIFF_MAX
* lib/xmalloc.c (xcalloc) [HAVE_GNU_CALLOC]: Do not omit
xalloc_oversized check, since objects larger than PTRDIFF_MAX
/* Specification. */
#include "safe-alloc.h"
+#include "xalloc-oversized.h"
+
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>
-/* Return 1 if an array of N objects, each of size S, cannot exist due
- to size arithmetic overflow. S must be positive and N must be
- nonnegative. This is a macro, not a function, so that it
- works correctly even when SIZE_MAX < N.
-
- By gnulib convention, SIZE_MAX represents overflow in size
- calculations, so the conservative dividend to use here is
- SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
- However, malloc (SIZE_MAX) fails on all known hosts where
- sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
- exactly-SIZE_MAX allocations on such hosts; this avoids a test and
- branch when S is known to be 1.
-
- This is the same as xalloc_oversized from xalloc.h
-*/
-#define safe_alloc_oversized(n, s) \
- ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
-
-
/**
* safe_alloc_alloc_n:
* @ptrptr: pointer to pointer for address of allocated memory
return 0;
}
- if (safe_alloc_oversized (count, size))
+ if (xalloc_oversized (count, size))
{
errno = ENOMEM;
return -1;
*(void **) ptrptr = NULL;
return 0;
}
- if (safe_alloc_oversized (count, size))
+ if (xalloc_oversized (count, size))
{
errno = ENOMEM;
return -1;