+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.
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
+#if defined __CHERI__
+# include <cheri.h>
+#endif
_GL_INLINE_HEADER_BEGIN
#ifndef 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).
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