2024-11-04 Paul Eggert <eggert@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).
@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,
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;