2 * reallocarray test suite.
4 * This does some simple sanity checks and checks some of the overflow
5 * detection, but isn't particularly thorough.
7 * The canonical version of this file is maintained in the rra-c-util package,
8 * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
10 * Written by Russ Allbery <eagle@eyrie.org>
12 * The authors hereby relinquish any claim to any copyright that they may have
13 * in this work, whether granted under contract or by operation of law or
14 * international treaty, and hereby commit to the public, at large, that they
15 * shall not, at any time in the future, seek to enforce any copyright in this
16 * work against any person or entity, or prevent any person or entity from
17 * copying, publishing, distributing or creating derivative works of this
22 #include <portable/system.h>
26 #include <tests/tap/basic.h>
28 void *test_reallocarray(void *, size_t, size_t);
40 /* Test success cases and write to the memory for valgrind checks. */
41 p = test_reallocarray(NULL, 2, 5);
42 memcpy(p, "123456789", 10);
43 is_string("123456789", p, "reallocarray of NULL");
44 p = test_reallocarray(p, 4, 5);
45 is_string("123456789", p, "reallocarray after resize");
46 memcpy(p + 9, "0123456789", 11);
47 is_string("1234567890123456789", p, "write to larger memory segment");
51 * If nmemb or size are 0, we should either get NULL or a pointer we can
52 * free. Make sure we don't get something weird, like division by zero.
54 p = test_reallocarray(NULL, 0, 100);
57 p = test_reallocarray(NULL, 100, 0);
61 /* Test the range-checking error cases. */
62 p = test_reallocarray(NULL, 2, SIZE_MAX / 2);
64 ok(p == NULL, "reallocarray fails for 2, SIZE_MAX / 2");
65 is_int(ENOMEM, oerrno, "...with correct errno");
67 p = test_reallocarray(base, 3, SIZE_MAX / 3);
69 ok(p == NULL, "reallocarray fails for 3, SIZE_MAX / 3");
70 is_int(ENOMEM, oerrno, "...with correct errno");
71 sqrt_max = (1UL << (sizeof(size_t) * 4));
72 p = test_reallocarray(base, sqrt_max, sqrt_max);
74 ok(p == NULL, "reallocarray fails for sqrt(SIZE_MAX), sqrt(SIZE_MAX)");
75 is_int(ENOMEM, oerrno, "...with correct errno");
76 p = test_reallocarray(base, 1, SIZE_MAX);
78 ok(p == NULL, "reallocarray fails for 1, SIZE_MAX");
79 is_int(ENOMEM, oerrno, "...with correct errno");
80 p = test_reallocarray(base, SIZE_MAX, 1);
82 ok(p == NULL, "reallocarray fails for SIZE_MAX, 1");
83 is_int(ENOMEM, oerrno, "...with correct errno");
84 p = test_reallocarray(base, 2, SIZE_MAX);
86 ok(p == NULL, "reallocarray fails for 2, SIZE_MAX");
87 is_int(ENOMEM, oerrno, "...with correct errno");
89 /* Clean up and exit. */