Mercurial > gnulib
changeset 38244:a0855e9f76b9
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.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Thu, 15 Dec 2016 10:00:22 -0800 |
parents | f9bc62578d4a |
children | 7475c57c0be7 |
files | ChangeLog lib/safe-alloc.c modules/safe-alloc |
diffstat | 3 files changed, 12 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Thu Dec 15 09:59:21 2016 -0800 +++ b/ChangeLog Thu Dec 15 10:00:22 2016 -0800 @@ -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
--- a/lib/safe-alloc.c Thu Dec 15 09:59:21 2016 -0800 +++ b/lib/safe-alloc.c Thu Dec 15 10:00:22 2016 -0800 @@ -22,30 +22,13 @@ /* 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 @@ return 0; } - if (safe_alloc_oversized (count, size)) + if (xalloc_oversized (count, size)) { errno = ENOMEM; return -1; @@ -108,7 +91,7 @@ *(void **) ptrptr = NULL; return 0; } - if (safe_alloc_oversized (count, size)) + if (xalloc_oversized (count, size)) { errno = ENOMEM; return -1;