From 9b6da3c014d0866efc608e132356d7a44f9c5537 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 25 Apr 2020 00:59:56 +0000 Subject: clean up mallocProduct, reallocProduct code git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3794 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- lib/util/mallocvar.h | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/util/mallocvar.h b/lib/util/mallocvar.h index a2fb9eb7..23b28c40 100644 --- a/lib/util/mallocvar.h +++ b/lib/util/mallocvar.h @@ -24,7 +24,7 @@ extern "C" { static __inline__ void mallocProduct(void ** const resultP, - unsigned int const factor1, + size_t const factor1, unsigned int const factor2) { /*---------------------------------------------------------------------------- malloc a space whose size in bytes is the product of 'factor1' and @@ -35,15 +35,14 @@ mallocProduct(void ** const resultP, -----------------------------------------------------------------------------*/ /* C99 introduces SIZE_MAX, the maximum size_t value. - Pre-C99, we just use UINT_MAX, because in practice size_t is always at - least as wide as unsigned int and nobody with such an old compiler - could possibly need memory sizes larger than UINT_MAX. + Pre-C99, we do the best we can, assuming conventional encoding of + numbers and that size_t is unsigned. */ size_t const sizeMax = #if defined(SIZE_MAX) SIZE_MAX #else - UINT_MAX + ~((size_t)0) #endif ; @@ -51,13 +50,13 @@ mallocProduct(void ** const resultP, *resultP = malloc(1); else { /* N.B. The type of malloc's argument is size_t */ - if ((size_t)factor1 != factor1 || (size_t)factor2 != factor2) + if ((size_t)factor2 != factor2) *resultP = NULL; else { if (sizeMax / factor2 < factor1) *resultP = NULL; else - *resultP = malloc((size_t)factor1 * factor2); + *resultP = malloc(factor1 * factor2); } } } @@ -66,18 +65,30 @@ mallocProduct(void ** const resultP, static __inline__ void reallocProduct(void ** const blockP, - unsigned int const factor1, + size_t const factor1, unsigned int const factor2) { + size_t const sizeMax = +#if defined(SIZE_MAX) + SIZE_MAX +#else + ~((size_t)0) +#endif + ; + void * const oldBlockP = *blockP; void * newBlockP; - if (UINT_MAX / factor2 < factor1) + /* N.B. The type of realloc's argument is size_t */ + if ((size_t)factor2 != factor2) newBlockP = NULL; - else - newBlockP = realloc(oldBlockP, factor1 * factor2); - + else { + if (sizeMax / factor2 < factor1) + newBlockP = NULL; + else + newBlockP = realloc(oldBlockP, factor1 * factor2); + } if (newBlockP) *blockP = newBlockP; else { -- cgit 1.4.1