]> Savannah Git Hosting - gnulib.git/commitdiff
realloc: minor style coalescing
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 25 Oct 2024 04:55:46 +0000 (21:55 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 25 Oct 2024 04:57:18 +0000 (21:57 -0700)
* lib/alignalloc.h (alignalloc):
* lib/eealloc.h (eerealloc):
* lib/ialloc.h (irealloc, ireallocarray):
* lib/safe-alloc.h (safe_alloc_realloc_n):
Adjust commentary and code to better match what’s used elsewhere.
This doesn’t change behavior.

ChangeLog
lib/alignalloc.h
lib/eealloc.h
lib/ialloc.h
lib/safe-alloc.h

index dad59adfe967cdcc46c98a9ee88e2ac70cdabe2e..edd01c829114efef9a06f86160323b400186a9da 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2024-10-24  Paul Eggert  <eggert@cs.ucla.edu>
 
+       realloc: minor style coalescing
+       * lib/alignalloc.h (alignalloc):
+       * lib/eealloc.h (eerealloc):
+       * lib/ialloc.h (irealloc, ireallocarray):
+       * lib/safe-alloc.h (safe_alloc_realloc_n):
+       Adjust commentary and code to better match what’s used elsewhere.
+       This doesn’t change behavior.
+
        xalloc: port to Cheri, strict C23, realloc null
        * lib/xmalloc.c [__CHERI_PURE_CAPABILITY__]: Include <cheri.h>.
        (xrealloc, xreallocarray): Support Cheri.  Avoid undefined
index 0c73bfc4b235c732dfee8ba8b470d91caf830873..229ccf5dd95b29e098f219cea9964a5a2745cb9c 100644 (file)
@@ -100,7 +100,9 @@ alignalloc (idx_t alignment, idx_t size)
   void *ptr = NULL;
   if (alignment < sizeof (void *))
     alignment = sizeof (void *);
-  errno = posix_memalign (&ptr, alignment, size | !size);
+  /* Work around posix_memalign glitch by treating a 0 size as if it were 1,
+     so that returning NULL is equivalent to failing.  */
+  errno = posix_memalign (&ptr, alignment, size ? size : 1);
 #  if defined __CHERI_PURE_CAPABILITY__
   if (ptr != NULL)
     ptr = cheri_bounds_set (ptr, size);
index ff31da49cdc4ce37357d6a568793311d98cb3153..8bfcf599bd317681ee690ef561ba1139ca808c70 100644 (file)
@@ -80,11 +80,10 @@ EEALLOC_INLINE void *eerealloc (void *p, size_t n)
 EEALLOC_INLINE void *
 eerealloc (void *p, size_t n)
 {
-  /* If n is zero, allocate or keep a 1-byte block.  */
-  size_t nx = n;
-  if (n == 0)
-    nx = 1;
-  void *ptr = realloc (p, nx);
+  /* Work around realloc glitch by treating a 0 size as if it were 1,
+     to avoid undefined behavior in strict C23 platforms,
+     and so that returning NULL is equivalent to failing.  */
+  void *ptr = realloc (p, n ? n : 1);
 # if defined __CHERI_PURE_CAPABILITY__
   if (ptr != NULL)
     ptr = cheri_bounds_set (ptr, n);
index 2aa94ae7caf93f7df3cc21d432b248afad3e3f4f..7e296bfdad932c7bb8eff9ad4be8520bfa84e1ea 100644 (file)
@@ -70,9 +70,10 @@ irealloc (void *p, idx_t s)
 {
   if (s <= SIZE_MAX)
     {
-      /* Work around GNU realloc glitch by treating a zero size as if it
-         were 1, so that returning NULL is equivalent to failing.  */
-      p = realloc (p, s | !s);
+      /* Work around realloc glitch by treating a 0 size as if it were 1,
+         to avoid undefined behavior in strict C23 platforms,
+         and so that returning NULL is equivalent to failing.  */
+      p = realloc (p, s ? s : 1);
 #if defined __CHERI_PURE_CAPABILITY__
       if (p != NULL)
         p = cheri_bounds_set (p, s);
@@ -114,8 +115,8 @@ ireallocarray (void *p, idx_t n, idx_t s)
 {
   if (n <= SIZE_MAX && s <= SIZE_MAX)
     {
-      /* Work around GNU reallocarray glitch by treating a zero size as if
-         it were 1, so that returning NULL is equivalent to failing.  */
+      /* Work around reallocarray glitch by treating a 0 size as if it were 1,
+         so that returning NULL is equivalent to failing.  */
       size_t nx = n;
       size_t sx = s;
       if (n == 0 || s == 0)
index bee973ed4d0e452f67ccbc44d5f38500bdbb75c6..428d70840b4c75494010174c0d617331d67203b3 100644 (file)
@@ -45,6 +45,8 @@ extern "C" {
 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)