]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/portable/reallocarray-t.c
Change CrackLib tests for system CrackLib
[kerberos/krb5-strength.git] / tests / portable / reallocarray-t.c
1 /*
2  * reallocarray test suite.
3  *
4  * This does some simple sanity checks and checks some of the overflow
5  * detection, but isn't particularly thorough.
6  *
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/>.
9  *
10  * Written by Russ Allbery <eagle@eyrie.org>
11  *
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
18  * work.
19  */
20
21 #include <config.h>
22 #include <portable/system.h>
23
24 #include <errno.h>
25
26 #include <tests/tap/basic.h>
27
28 void *test_reallocarray(void *, size_t, size_t);
29
30
31 int
32 main(void)
33 {
34     char *p, *base;
35     size_t sqrt_max;
36     int oerrno;
37
38     plan(15);
39
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");
48     free(p);
49
50     /*
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.
53      */
54     p = test_reallocarray(NULL, 0, 100);
55     if (p != NULL)
56         free(p);
57     p = test_reallocarray(NULL, 100, 0);
58     if (p != NULL)
59         free(p);
60
61     /* Test the range-checking error cases. */
62     p = test_reallocarray(NULL, 2, SIZE_MAX / 2);
63     oerrno = errno;
64     ok(p == NULL, "reallocarray fails for 2, SIZE_MAX / 2");
65     is_int(ENOMEM, oerrno, "...with correct errno");
66     base = malloc(10);
67     p = test_reallocarray(base, 3, SIZE_MAX / 3);
68     oerrno = errno;
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);
73     oerrno = errno;
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);
77     oerrno = errno;
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);
81     oerrno = errno;
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);
85     oerrno = errno;
86     ok(p == NULL, "reallocarray fails for 2, SIZE_MAX");
87     is_int(ENOMEM, oerrno, "...with correct errno");
88
89     /* Clean up and exit. */
90     free(base);
91     return 0;
92 }