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

ChangeLog
lib/eealloc.h

index b0f6f9f40422d9f11c77689974175bbbf6478321..60aea347a344ff3690da61cfa60c3dc80830b79d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-11-11  Bruno Haible  <bruno@clisp.org>
+
+       eealloc: Take advantage of CHERI bounds-checking.
+       * lib/eealloc.h: Include <cheri.h>.
+       (eemalloc): When n is 0, return a pointer whose bounds are of size 0,
+       not 1.
+       (eerealloc): Likewise.
+
 2023-11-11  Bruno Haible  <bruno@clisp.org>
 
        alignalloc: Take advantage of CHERI bounds-checking.
index 6666f172c6d59ac510934d5875272403f3e69ea5..bae39151468006d204ca5360b56a7e2d37502883 100644 (file)
@@ -36,6 +36,9 @@
 #endif
 
 #include <stdlib.h>
+#if defined __CHERI__
+# include <cheri.h>
+#endif
 
 _GL_INLINE_HEADER_BEGIN
 #ifndef EEALLOC_INLINE
@@ -52,9 +55,15 @@ EEALLOC_INLINE void *
 eemalloc (size_t n)
 {
   /* If n is zero, allocate a 1-byte block.  */
+  size_t nx = n;
   if (n == 0)
-    n = 1;
-  return malloc (n);
+    nx = 1;
+  void *ptr = malloc (nx);
+# if defined __CHERI__
+  if (ptr != NULL)
+    ptr = cheri_bounds_set (ptr, n);
+# endif
+  return ptr;
 }
 #endif
 
@@ -67,9 +76,15 @@ 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)
-    n = 1;
-  return realloc (p, n);
+    nx = 1;
+  void *ptr = realloc (p, nx);
+# if defined __CHERI__
+  if (ptr != NULL)
+    ptr = cheri_bounds_set (ptr, n);
+# endif
+  return ptr;
 }
 #endif