+2024-10-24 Paul Eggert <eggert@cs.ucla.edu>
+
+ reallocarray: simplify
+ * lib/reallocarray.c (reallocarray): Use simpler workaround
+ for realloc glitch, which does not involve malloc.
+ * modules/reallocarray (Depends-on): Remove malloc-posix.
+
2024-10-23 Paul Eggert <eggert@cs.ucla.edu>
ialloc: fix realloc-gnu dependency
return NULL;
}
- /* Avoid calling realloc (ptr, 0), since that is undefined behaviour in
- ISO C 23 and since the GNU libc behaviour may possibly change. */
+ /* 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. */
if (nbytes == 0)
- {
- void *new_ptr = malloc (1);
- if (new_ptr == NULL)
- /* errno is set here. */
- return NULL;
- free (ptr);
- return new_ptr;
- }
+ nbytes = 1;
/* Call realloc, setting errno to ENOMEM on failure. */
return realloc (ptr, nbytes);
Depends-on:
extensions
-malloc-posix [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
realloc-posix [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
stdckdint [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
stdlib