1 /* Test for reallocarray.
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
22 #include <support/check.h>
32 const size_t max
= ~(size_t)0;
35 /* Test overflow detection. */
37 ptr
= reallocarray (NULL
, max
, 2);
39 TEST_VERIFY (errno
== ENOMEM
);
42 ptr
= reallocarray (NULL
, 2, max
);
44 TEST_VERIFY (errno
== ENOMEM
);
49 ptr
= reallocarray (NULL
, a
, b
);
51 TEST_VERIFY (errno
== ENOMEM
);
54 ptr
= reallocarray (NULL
, b
, a
);
56 TEST_VERIFY (errno
== ENOMEM
);
58 /* Test realloc-like behavior. */
59 /* Allocate memory like malloc. */
60 ptr
= reallocarray (NULL
, 10, 2);
61 TEST_VERIFY_EXIT (ptr
);
62 TEST_VERIFY_EXIT (malloc_usable_size (ptr
) >= 10*2);
64 memset (ptr
, 0xAF, 10*2);
67 ptr2
= reallocarray (ptr
, 20, 2);
71 TEST_VERIFY (malloc_usable_size (ptr
) >= 20*2);
75 for (i
= 0; i
< 10*2; ++i
)
82 /* Decrease buffer size. */
83 ptr2
= reallocarray (ptr
, 5, 3);
87 TEST_VERIFY_EXIT (malloc_usable_size (ptr
) >= 5*3);
91 for (i
= 0; i
< 5*3; ++i
)
98 /* Overflow should leave buffer untouched. */
100 ptr2
= reallocarray (ptr
, 2, ~(size_t)0);
102 TEST_VERIFY (errno
== ENOMEM
);
106 for (i
= 0; i
< 5*3; ++i
)
118 #include <support/test-driver.c>