[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 5/7] reallocarray: simplify
From: |
Paul Eggert |
Subject: |
[PATCH 5/7] reallocarray: simplify |
Date: |
Thu, 24 Oct 2024 21:57:39 -0700 |
* lib/reallocarray.c (reallocarray): Use simpler workaround
for realloc glitch, which does not involve malloc.
* modules/reallocarray (Depends-on): Remove malloc-posix.
---
ChangeLog | 7 +++++++
lib/reallocarray.c | 14 ++++----------
modules/reallocarray | 1 -
3 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 0a74a6483c..9226a54c18 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/lib/reallocarray.c b/lib/reallocarray.c
index e0377d0870..bae88c937d 100644
--- a/lib/reallocarray.c
+++ b/lib/reallocarray.c
@@ -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);
diff --git a/modules/reallocarray b/modules/reallocarray
index dace5e636b..41c80e454b 100644
--- a/modules/reallocarray
+++ b/modules/reallocarray
@@ -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
--
2.43.0
- [PATCH 1/7] realloc: still more improvements for realloc (p, 0), Paul Eggert, 2024/10/25
- [PATCH 2/7] group-member: fix malloc-gnu dependency, Paul Eggert, 2024/10/25
- [PATCH 4/7] ialloc: fix realloc-gnu dependency, Paul Eggert, 2024/10/25
- [PATCH 3/7] backupfile: fix irealloc dependency, Paul Eggert, 2024/10/25
- [PATCH 5/7] reallocarray: simplify,
Paul Eggert <=
- [PATCH 6/7] xalloc: port to Cheri, strict C23, realloc null, Paul Eggert, 2024/10/25
- [PATCH 7/7] realloc: minor style coalescing, Paul Eggert, 2024/10/25