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>
11 * Copyright 2014 Russ Allbery <eagle@eyrie.org>
13 * The Board of Trustees of the Leland Stanford Junior University
15 * Copying and distribution of this file, with or without modification, are
16 * permitted in any medium without royalty provided the copyright notice and
17 * this notice are preserved. This file is offered as-is, without any
20 * SPDX-License-Identifier: FSFAP
24 #include <portable/system.h>
28 #include <tests/tap/basic.h>
30 void *test_reallocarray(void *, size_t, size_t);
42 /* Test success cases and write to the memory for valgrind checks. */
43 p = test_reallocarray(NULL, 2, 5);
44 memcpy(p, "123456789", 10);
45 is_string("123456789", p, "reallocarray of NULL");
46 p = test_reallocarray(p, 4, 5);
47 is_string("123456789", p, "reallocarray after resize");
48 memcpy(p + 9, "0123456789", 11);
49 is_string("1234567890123456789", p, "write to larger memory segment");
53 * If nmemb or size are 0, we should either get NULL or a pointer we can
54 * free. Make sure we don't get something weird, like division by zero.
56 p = test_reallocarray(NULL, 0, 100);
59 p = test_reallocarray(NULL, 100, 0);
63 /* Test the range-checking error cases. */
64 p = test_reallocarray(NULL, 2, SIZE_MAX / 2);
66 ok(p == NULL, "reallocarray fails for 2, SIZE_MAX / 2");
67 is_int(ENOMEM, oerrno, "...with correct errno");
69 p = test_reallocarray(base, 3, SIZE_MAX / 3);
71 ok(p == NULL, "reallocarray fails for 3, SIZE_MAX / 3");
72 is_int(ENOMEM, oerrno, "...with correct errno");
73 sqrt_max = (1UL << (sizeof(size_t) * 4));
74 p = test_reallocarray(base, sqrt_max, sqrt_max);
76 ok(p == NULL, "reallocarray fails for sqrt(SIZE_MAX), sqrt(SIZE_MAX)");
77 is_int(ENOMEM, oerrno, "...with correct errno");
78 p = test_reallocarray(base, 1, SIZE_MAX);
80 ok(p == NULL, "reallocarray fails for 1, SIZE_MAX");
81 is_int(ENOMEM, oerrno, "...with correct errno");
82 p = test_reallocarray(base, SIZE_MAX, 1);
84 ok(p == NULL, "reallocarray fails for SIZE_MAX, 1");
85 is_int(ENOMEM, oerrno, "...with correct errno");
86 p = test_reallocarray(base, 2, SIZE_MAX);
88 ok(p == NULL, "reallocarray fails for 2, SIZE_MAX");
89 is_int(ENOMEM, oerrno, "...with correct errno");
91 /* Clean up and exit. */