* 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. --- ChangeLog | 8 ++++++++ lib/alignalloc.h | 4 +++- lib/eealloc.h | 9 ++++----- lib/ialloc.h | 11 ++++++----- lib/safe-alloc.h | 2 ++ 5 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/ChangeLog b/ChangeLog index dad59adfe9..edd01c8291 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2024-10-24 Paul Eggert <egg...@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 diff --git a/lib/alignalloc.h b/lib/alignalloc.h index 0c73bfc4b2..229ccf5dd9 100644 --- a/lib/alignalloc.h +++ b/lib/alignalloc.h @@ -100,7 +100,9 @@ alignalloc (idx_t alignment, idx_t size) 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); diff --git a/lib/eealloc.h b/lib/eealloc.h index ff31da49cd..8bfcf599bd 100644 --- a/lib/eealloc.h +++ b/lib/eealloc.h @@ -80,11 +80,10 @@ EEALLOC_INLINE void *eerealloc (void *p, size_t n) 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); diff --git a/lib/ialloc.h b/lib/ialloc.h index 2aa94ae7ca..7e296bfdad 100644 --- a/lib/ialloc.h +++ b/lib/ialloc.h @@ -70,9 +70,10 @@ irealloc (void *p, idx_t s) { 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); @@ -114,8 +115,8 @@ ireallocarray (void *p, idx_t n, idx_t 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) diff --git a/lib/safe-alloc.h b/lib/safe-alloc.h index bee973ed4d..428d70840b 100644 --- a/lib/safe-alloc.h +++ b/lib/safe-alloc.h @@ -45,6 +45,8 @@ extern "C" { 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) -- 2.43.0