]> Savannah Git Hosting - gnulib.git/commitdiff
safe-alloc: use xalloc-oversized
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 15 Dec 2016 18:00:22 +0000 (10:00 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 15 Dec 2016 18:19:48 +0000 (10:19 -0800)
* 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.

ChangeLog
lib/safe-alloc.c
modules/safe-alloc

index bee9a1728e381eb7b076baa035ab8146c343ebd0..5e9e80197dcc699922d310e03c3e734dfe1737ae 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 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
index 7b2acf2815f26ff0d6b38234966d7002e9f23752..0f9e5a6a6e4427e8667e6abc0a0d5d64006f1299 100644 (file)
 /* 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
@@ -68,7 +51,7 @@ safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
       return 0;
     }
 
-  if (safe_alloc_oversized (count, size))
+  if (xalloc_oversized (count, size))
     {
       errno = ENOMEM;
       return -1;
@@ -108,7 +91,7 @@ safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
       *(void **) ptrptr = NULL;
       return 0;
     }
-  if (safe_alloc_oversized (count, size))
+  if (xalloc_oversized (count, size))
     {
       errno = ENOMEM;
       return -1;
index 94f799fe8294345decf52956728c59fcfc883b81..13e78dec7b247d4854d9c005744a25220a4cfa26 100644 (file)
@@ -6,6 +6,9 @@ lib/safe-alloc.h
 lib/safe-alloc.c
 m4/safe-alloc.m4
 
+Depends-on:
+xalloc-oversized
+
 configure.ac:
 gl_SAFE_ALLOC