]> Savannah Git Hosting - gnulib.git/commitdiff
safe-alloc: simplify, given new reallocarray behavior
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 5 Nov 2024 06:24:29 +0000 (22:24 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 5 Nov 2024 06:25:08 +0000 (22:25 -0800)
* 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.

ChangeLog
lib/safe-alloc.h

index e42514c1f7dceea404ef70bc6a6fcba57a501695..89cc5a95a73c2639fb205d94bcdbb8819566f6bc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2024-11-04  Paul Eggert  <eggert@cs.ucla.edu>
 
+       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.
index 428d70840b4c75494010174c0d617331d67203b3..72cad991719504ed52535ad694b0af5b4b8ca1dd 100644 (file)
@@ -27,9 +27,6 @@
 #endif
 
 #include <stdlib.h>
-#if defined __CHERI_PURE_CAPABILITY__
-# include <cheri.h>
-#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: