2024-11-04 Paul Eggert <eggert@cs.ucla.edu>
+ realloc-posix: set CHERI bounds
+ * lib/ialloc.h, lib/xmalloc.c [__CHERI_PURE_CAPABILITY__]:
+ Do nothing special, as realloc and reallocarray
+ should do this for us now.
+ * lib/realloc.c [__CHERI_PURE_CAPABILITY__]:
+ Include cheri.h, and arrange for rpl_realloc to set bounds.
+
realloc-posix: realloc (..., 0) now returns nonnull
* lib/realloc.c (rpl_realloc): Simplify and tune by using
HAVE_REALLOC_0_NONNULL and HAVE_MALLOC_PTRDIFF, and
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
-#if defined __CHERI_PURE_CAPABILITY__
-# include <cheri.h>
-#endif
_GL_INLINE_HEADER_BEGIN
#ifndef IALLOC_INLINE
void *
irealloc (void *p, idx_t s)
{
- if (s <= SIZE_MAX)
- {
- /* 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);
-#endif
- return p;
- }
- else
- return _gl_alloc_nomem ();
+ return s <= SIZE_MAX ? realloc (p, s) : _gl_alloc_nomem ();
}
/* icalloc (num, size) is like calloc (num, size).
IALLOC_INLINE void *
ireallocarray (void *p, idx_t n, idx_t s)
{
- if (n <= SIZE_MAX && s <= SIZE_MAX)
- {
- /* 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)
- nx = sx = 1;
- p = reallocarray (p, nx, sx);
-#if defined __CHERI_PURE_CAPABILITY__
- if (p != NULL && (n == 0 || s == 0))
- p = cheri_bounds_set (p, 0);
-#endif
- return p;
- }
- else
- return _gl_alloc_nomem ();
+ return (n <= SIZE_MAX && s <= SIZE_MAX
+ ? reallocarray (p, n, s)
+ : _gl_alloc_nomem ());
}
#ifdef __cplusplus
#include <errno.h>
#include <stdckdint.h>
+#ifdef __CHERI_PURE_CAPABILITY__
+# include <cheri.h>
+#endif
+
/* Call the system's realloc below. This file does not define
_GL_USE_STDLIB_ALLOC because it needs Gnulib's malloc if present. */
#undef realloc
void *
rpl_realloc (void *p, size_t n)
{
+ size_t n1 = n;
+
if (n == 0)
{
#if NEED_SANITIZED_REALLOC
caller sites. */
#if !HAVE_REALLOC_0_NONNULL
- n = 1;
+ n1 = 1;
#endif
}
}
#endif
- void *result = realloc (p, n);
+ void *result = realloc (p, n1);
#if !HAVE_MALLOC_POSIX
if (result == NULL)
errno = ENOMEM;
#endif
+#ifdef __CHERI_PURE_CAPABILITY__
+ if (result != NULL)
+ result = cheri_bounds_set (result, n);
+#endif
+
return result;
}
#include <stdint.h>
#include <string.h>
-#ifdef __CHERI_PURE_CAPABILITY__
-# include <cheri.h>
-#endif
-
static void * _GL_ATTRIBUTE_PURE
check_nonnull (void *p)
{
void *
xrealloc (void *p, size_t s)
{
- /* Work around realloc glitch by treating a 0 size as if it were 1,
- to avoid undefined behavior in strict C23 platforms,
- so that returning NULL is equivalent to failing. */
- void *r = realloc (p, s ? s : 1);
+ void *r = realloc (p, s);
if (!r)
xalloc_die ();
-#ifdef __CHERI_PURE_CAPABILITY__
- r = cheri_bounds_set (r, s);
-#endif
return r;
}
void *
xreallocarray (void *p, size_t n, size_t s)
{
- /* 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 || !s)
- nx = sx = 1;
- void *r = reallocarray (p, nx, sx);
+ void *r = reallocarray (p, n, s);
if (!r)
xalloc_die ();
-#ifdef __CHERI_PURE_CAPABILITY__
- if (!n || !s)
- r = cheri_bounds_set (r, 0);
-#endif
return r;
}