]> Savannah Git Hosting - gnulib.git/commitdiff
ialloc: Take advantage of CHERI bounds-checking.
authorBruno Haible <bruno@clisp.org>
Sat, 11 Nov 2023 18:34:26 +0000 (19:34 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 11 Nov 2023 18:34:26 +0000 (19:34 +0100)
* lib/ialloc.h: Include <cheri.h>.
(irealloc): When s is 0, return a pointer whose bounds are of size 0,
not 1.
(ireallocarray): When n or s is 0, return a pointer whose bounds are of
size 0, not 1.

ChangeLog
lib/ialloc.h

index 60aea347a344ff3690da61cfa60c3dc80830b79d..c166fae7153be479c381e03f82668bb619520b1e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2023-11-11  Bruno Haible  <bruno@clisp.org>
+
+       ialloc: Take advantage of CHERI bounds-checking.
+       * lib/ialloc.h: Include <cheri.h>.
+       (irealloc): When s is 0, return a pointer whose bounds are of size 0,
+       not 1.
+       (ireallocarray): When n or s is 0, return a pointer whose bounds are of
+       size 0, not 1.
+
 2023-11-11  Bruno Haible  <bruno@clisp.org>
 
        eealloc: Take advantage of CHERI bounds-checking.
index 22f57a47d84c668896b42c715da6e1b91fcf8889..527b1f48beb15962a185e40d5332fb2cbf9d3252 100644 (file)
@@ -29,6 +29,9 @@
 #include <errno.h>
 #include <stdint.h>
 #include <stdlib.h>
+#if defined __CHERI__
+# include <cheri.h>
+#endif
 
 _GL_INLINE_HEADER_BEGIN
 #ifndef IALLOC_INLINE
@@ -65,9 +68,19 @@ IALLOC_INLINE
 void *
 irealloc (void *p, idx_t s)
 {
-  /* Work around GNU realloc glitch by treating a zero size as if it
-     were 1, so that returning NULL is equivalent to failing.  */
-  return s <= SIZE_MAX ? realloc (p, s | !s) : _gl_alloc_nomem ();
+  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);
+#if defined __CHERI__
+      if (p != NULL)
+        p = cheri_bounds_set (p, s);
+#endif
+      return p;
+    }
+  else
+    return _gl_alloc_nomem ();
 }
 
 /* icalloc (num, size) is like calloc (num, size).
@@ -99,13 +112,23 @@ icalloc (idx_t n, idx_t s)
 IALLOC_INLINE void *
 ireallocarray (void *p, idx_t n, idx_t s)
 {
-  /* Work around GNU reallocarray glitch by treating a zero size as if
-     it were 1, so that returning NULL is equivalent to failing.  */
-  if (n == 0 || s == 0)
-    n = s = 1;
-  return (n <= SIZE_MAX && s <= SIZE_MAX
-          ? reallocarray (p, n, s)
-          : _gl_alloc_nomem ());
+  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.  */
+      size_t nx = n;
+      size_t sx = s;
+      if (n == 0 || s == 0)
+        nx = sx = 1;
+      p = reallocarray (p, nx, sx);
+#if defined __CHERI__
+      if (p != NULL && (n == 0 || s == 0))
+        p = cheri_bounds_set (p, 0);
+#endif
+      return p;
+    }
+  else
+    return _gl_alloc_nomem ();
 }
 
 #ifdef __cplusplus