From b8e5f9c8ae81cd3a4e632480fe58676b57de07d4 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 15 Dec 2016 10:00:22 -0800 Subject: [PATCH] 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. --- ChangeLog | 5 +++++ lib/safe-alloc.c | 25 ++++--------------------- modules/safe-alloc | 3 +++ 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index bee9a1728e..5e9e80197d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2016-12-15 Paul Eggert + 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 diff --git a/lib/safe-alloc.c b/lib/safe-alloc.c index 7b2acf2815..0f9e5a6a6e 100644 --- a/lib/safe-alloc.c +++ b/lib/safe-alloc.c @@ -22,30 +22,13 @@ /* Specification. */ #include "safe-alloc.h" +#include "xalloc-oversized.h" + #include #include #include -/* 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; diff --git a/modules/safe-alloc b/modules/safe-alloc index 94f799fe82..13e78dec7b 100644 --- a/modules/safe-alloc +++ b/modules/safe-alloc @@ -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 -- 2.39.5