1
0
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:
Paul Eggert
2024-10-24 21:34:12 -07:00
parent 59d98e49ba
commit b69be0f7ae
3 changed files with 11 additions and 11 deletions

View File

@@ -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> 2024-10-23 Paul Eggert <eggert@cs.ucla.edu>
ialloc: fix realloc-gnu dependency ialloc: fix realloc-gnu dependency

View File

@@ -33,17 +33,11 @@ reallocarray (void *ptr, size_t nmemb, size_t size)
return NULL; return NULL;
} }
/* Avoid calling realloc (ptr, 0), since that is undefined behaviour in /* Work around realloc glitch by treating a 0 size as if it were 1,
ISO C 23 and since the GNU libc behaviour may possibly change. */ to avoid undefined behavior in strict C23 platforms,
and so that returning NULL is equivalent to failing. */
if (nbytes == 0) if (nbytes == 0)
{ nbytes = 1;
void *new_ptr = malloc (1);
if (new_ptr == NULL)
/* errno is set here. */
return NULL;
free (ptr);
return new_ptr;
}
/* Call realloc, setting errno to ENOMEM on failure. */ /* Call realloc, setting errno to ENOMEM on failure. */
return realloc (ptr, nbytes); return realloc (ptr, nbytes);

View File

@@ -8,7 +8,6 @@ m4/reallocarray.m4
Depends-on: Depends-on:
extensions extensions
malloc-posix [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
realloc-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] stdckdint [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
stdlib stdlib