* lib/realloc.c (rpl_realloc): Do not require realloc (p, 0) to succeed, as apparently glibc realloc (p, n) can sometimes fail even when the region would not grow. --- ChangeLog | 5 +++++ doc/posix-functions/realloc.texi | 4 ++-- lib/realloc.c | 10 +++------- 3 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/ChangeLog b/ChangeLog index 5edadc4ea8..ccad8e84f6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2024-11-04 Paul Eggert <egg...@cs.ucla.edu> + realloc: don’t require success for nongrowth + * lib/realloc.c (rpl_realloc): Do not require realloc (p, 0) to + succeed, as apparently glibc realloc (p, n) can sometimes fail + even when the region would not grow. + calloc, malloc: tune a bit This applies mostly to non-glibc platforms, or to 32-bit glibc before glibc 2.30 (2019). diff --git a/doc/posix-functions/realloc.texi b/doc/posix-functions/realloc.texi index 4be63c767a..3d8cd1705b 100644 --- a/doc/posix-functions/realloc.texi +++ b/doc/posix-functions/realloc.texi @@ -89,8 +89,8 @@ Portability problems not fixed by Gnulib: @item When not growing an already-allocated region, i.e., when @code{p} points to a region of size @code{psize} and @code{n <= psize}, -the standards allow @code{realloc (p, n)} to fail and return a null pointer. -It is not known which, if any, implementations actually fail in this situation. +@code{realloc (p, n)} can fail and return a null pointer: +glibc 2.40 and probably other platforms. @item If @code{realloc (p, 0)} frees @code{p} and returns a null pointer, diff --git a/lib/realloc.c b/lib/realloc.c index a94e230232..d0ed2169bc 100644 --- a/lib/realloc.c +++ b/lib/realloc.c @@ -65,18 +65,14 @@ rpl_realloc (void *p, size_t n) due either to C23 or to (a)'s semantics being messy. Act like (b), as that's easy, matches GNU, BSD and V7 malloc, matches BSD and V7 realloc, and requires no extra code at - caller sites. - Do not fail if P is nonnull, though, as it's natural for callers - to assume that realloc (P, 0) can fail only when P is null. */ + caller sites. */ void *result = realloc (p, 1); - if (result != NULL) - return result; #if !HAVE_MALLOC_POSIX - if (p == NULL) + if (result == NULL) errno = ENOMEM; #endif - return p; + return result; } ptrdiff_t signed_n; -- 2.43.0