summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2021-04-25 18:24:42 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2021-04-25 18:25:31 -0700
commit974ddbe54c37dd1f8f0f0f11f6f967d6fecd864d (patch)
tree7b106c0d4f17a403adefeaee5fd8f50494731030 /tests
parentffdf094f83481d04c58fc36fc7a32ba440f365c7 (diff)
downloadgnulib-974ddbe54c37dd1f8f0f0f11f6f967d6fecd864d.tar.gz
reallocarray: don’t crash if item size is 0
This problem affects only platforms where xalloc_oversized divides a number by the size arg. Fix this by defining xalloc_oversized (n, s) to work even if s == 0. * lib/malloca.h, lib/xalloc-oversized.h: Document new behavior. * lib/xalloc-oversized.h (__xalloc_oversized): Do not crash if S==0. * tests/test-reallocarray.c (main): Test for the bug.
Diffstat (limited to 'tests')
-rw-r--r--tests/test-reallocarray.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/tests/test-reallocarray.c b/tests/test-reallocarray.c
index 973f22ba66..6de355e6bd 100644
--- a/tests/test-reallocarray.c
+++ b/tests/test-reallocarray.c
@@ -26,12 +26,12 @@ SIGNATURE_CHECK (reallocarray, void *, (void *, size_t, size_t));
int
main ()
{
- void *volatile p = NULL;
-
/* Check that reallocarray fails when requested to allocate a block
of memory larger than PTRDIFF_MAX or SIZE_MAX bytes. */
for (size_t n = 2; n != 0; n <<= 1)
{
+ void *volatile p = NULL;
+
p = reallocarray (p, PTRDIFF_MAX / n + 1, n);
if (p)
return 1;
@@ -43,6 +43,11 @@ main ()
return 3;
if (errno != ENOMEM)
return 4;
+
+ /* Reallocarray should not crash with zero sizes. */
+ p = reallocarray (p, 0, n);
+ p = reallocarray (p, n, 0);
+ free (p);
}
return 0;