From: Paul Eggert Date: Tue, 5 Nov 2024 06:24:29 +0000 (-0800) Subject: safe-alloc: simplify, given new reallocarray behavior X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=064110be61987a8ec32f53e705b3a3562571bff4;p=gnulib.git safe-alloc: simplify, given new reallocarray behavior * lib/safe-alloc.h [__CHERI_PURE_CAPABILITY__]: Remove special cases for this, as reallocarray now does it for us. (safe_alloc_realloc_n): Remove. All uses replaced by reallocarray. --- diff --git a/ChangeLog b/ChangeLog index e42514c1f7..89cc5a95a7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2024-11-04 Paul Eggert + safe-alloc: simplify, given new reallocarray behavior + * lib/safe-alloc.h [__CHERI_PURE_CAPABILITY__]: + Remove special cases for this, as reallocarray + now does it for us. + (safe_alloc_realloc_n): Remove. All uses replaced by reallocarray. + argz: defend against realloc (0, 0) * modules/argz (Depends-on): Add realloc-posix so that argz_append doesn’t wrongly fail with ENOMEM on AIX. diff --git a/lib/safe-alloc.h b/lib/safe-alloc.h index 428d70840b..72cad99171 100644 --- a/lib/safe-alloc.h +++ b/lib/safe-alloc.h @@ -27,9 +27,6 @@ #endif #include -#if defined __CHERI_PURE_CAPABILITY__ -# include -#endif _GL_INLINE_HEADER_BEGIN #ifndef SAFE_ALLOC_INLINE @@ -41,23 +38,7 @@ extern "C" { #endif -/* Don't call these directly - use the macros below. */ -SAFE_ALLOC_INLINE void * -safe_alloc_realloc_n (void *ptr, size_t count, size_t size) -{ - /* Work around reallocarray glitch by treating a 0 size as if it were 1, - so that returning NULL is equivalent to failing. */ - size_t countx = count; - size_t sizex = size; - if (count == 0 || size == 0) - countx = sizex = 1; - ptr = reallocarray (ptr, countx, sizex); -#if defined __CHERI_PURE_CAPABILITY__ - if (ptr != NULL && (count == 0 || size == 0)) - ptr = cheri_bounds_set (ptr, 0); -#endif - return ptr; -} +/* Don't call this directly - use the macros below. */ _GL_ATTRIBUTE_NODISCARD SAFE_ALLOC_INLINE int safe_alloc_check (void *ptr) { @@ -103,7 +84,7 @@ safe_alloc_check (void *ptr) * Return -1 on failure to allocate, zero on success. */ #define ALLOC_N_UNINITIALIZED(ptr, count) \ - safe_alloc_check ((ptr) = safe_alloc_realloc_n (NULL, count, sizeof *(ptr))) + safe_alloc_check ((ptr) = reallocarray (NULL, count, sizeof *(ptr))) /** * REALLOC_N: @@ -117,7 +98,7 @@ safe_alloc_check (void *ptr) * Return -1 on failure to reallocate, zero on success. */ #define REALLOC_N(ptr, count) \ - safe_alloc_check ((ptr) = safe_alloc_realloc_n (ptr, count, sizeof *(ptr))) + safe_alloc_check ((ptr) = reallocarray (ptr, count, sizeof *(ptr))) /** * FREE: