mirror of
https://git.savannah.gnu.org/git/gnulib.git
synced 2025-08-08 17:22:05 +03:00
reallocarray: simplify
* lib/reallocarray.c (reallocarray): Use simpler workaround for realloc glitch, which does not involve malloc. * modules/reallocarray (Depends-on): Remove malloc-posix.
This commit is contained in:
@@ -1,3 +1,10 @@
|
||||
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
|
||||
|
@@ -33,17 +33,11 @@ reallocarray (void *ptr, size_t nmemb, size_t size)
|
||||
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);
|
||||
|
@@ -8,7 +8,6 @@ m4/reallocarray.m4
|
||||
|
||||
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
|
||||
|
Reference in New Issue
Block a user