2024-10-24 Paul Eggert <eggert@cs.ucla.edu>
+ realloc: minor style coalescing
+ * lib/alignalloc.h (alignalloc):
+ * lib/eealloc.h (eerealloc):
+ * lib/ialloc.h (irealloc, ireallocarray):
+ * lib/safe-alloc.h (safe_alloc_realloc_n):
+ Adjust commentary and code to better match what’s used elsewhere.
+ This doesn’t change behavior.
+
xalloc: port to Cheri, strict C23, realloc null
* lib/xmalloc.c [__CHERI_PURE_CAPABILITY__]: Include <cheri.h>.
(xrealloc, xreallocarray): Support Cheri. Avoid undefined
void *ptr = NULL;
if (alignment < sizeof (void *))
alignment = sizeof (void *);
- errno = posix_memalign (&ptr, alignment, size | !size);
+ /* Work around posix_memalign glitch by treating a 0 size as if it were 1,
+ so that returning NULL is equivalent to failing. */
+ errno = posix_memalign (&ptr, alignment, size ? size : 1);
# if defined __CHERI_PURE_CAPABILITY__
if (ptr != NULL)
ptr = cheri_bounds_set (ptr, size);
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)
- nx = 1;
- void *ptr = realloc (p, nx);
+ /* 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. */
+ void *ptr = realloc (p, n ? n : 1);
# if defined __CHERI_PURE_CAPABILITY__
if (ptr != NULL)
ptr = cheri_bounds_set (ptr, n);
{
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);
+ /* 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);
{
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. */
+ /* 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)
SAFE_ALLOC_INLINE void *
safe_alloc_realloc_n (void *ptr, size_t count, size_t size)
{
+ /* Work around reallocarray glitch by treating a 0 size as if it were 1,
+ so that returning NULL is equivalent to failing. */
size_t countx = count;
size_t sizex = size;
if (count == 0 || size == 0)