* lib/safe-alloc.h: Include <cheri.h>.
(safe_alloc_realloc_n): When count or size is 0, return a pointer whose
bounds are of size 0, not 1.
+2023-11-11 Bruno Haible <bruno@clisp.org>
+
+ safe-alloc: Take advantage of CHERI bounds-checking.
+ * lib/safe-alloc.h: Include <cheri.h>.
+ (safe_alloc_realloc_n): When count or size is 0, return a pointer whose
+ bounds are of size 0, not 1.
+
2023-11-11 Bruno Haible <bruno@clisp.org>
ialloc: Take advantage of CHERI bounds-checking.
#endif
#include <stdlib.h>
+#if defined __CHERI__
+# include <cheri.h>
+#endif
_GL_INLINE_HEADER_BEGIN
#ifndef SAFE_ALLOC_INLINE
SAFE_ALLOC_INLINE void *
safe_alloc_realloc_n (void *ptr, size_t count, size_t size)
{
+ size_t countx = count;
+ size_t sizex = size;
if (count == 0 || size == 0)
- count = size = 1;
- return reallocarray (ptr, count, size);
+ countx = sizex = 1;
+ ptr = reallocarray (ptr, countx, sizex);
+#if defined __CHERI__
+ if (ptr != NULL && (count == 0 || size == 0))
+ ptr = cheri_bounds_set (ptr, 0);
+#endif
+ return ptr;
}
_GL_ATTRIBUTE_NODISCARD SAFE_ALLOC_INLINE int
safe_alloc_check (void *ptr)