]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/portable/reallocarray-t.c
Declare fast forward from 3.1-2
[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  * Copyright 2014 Russ Allbery <eagle@eyrie.org>
12  * Copyright 2014
13  *     The Board of Trustees of the Leland Stanford Junior University
14  *
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
18  * warranty.
19  *
20  * SPDX-License-Identifier: FSFAP
21  */
22
23 #include <config.h>
24 #include <portable/system.h>
25
26 #include <errno.h>
27
28 #include <tests/tap/basic.h>
29
30 void *test_reallocarray(void *, size_t, size_t);
31
32
33 int
34 main(void)
35 {
36     char *p, *base;
37     size_t sqrt_max;
38     int oerrno;
39
40     plan(15);
41
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");
50     free(p);
51
52     /*
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.
55      */
56     p = test_reallocarray(NULL, 0, 100);
57     if (p != NULL)
58         free(p);
59     p = test_reallocarray(NULL, 100, 0);
60     if (p != NULL)
61         free(p);
62
63     /* Test the range-checking error cases. */
64     p = test_reallocarray(NULL, 2, SIZE_MAX / 2);
65     oerrno = errno;
66     ok(p == NULL, "reallocarray fails for 2, SIZE_MAX / 2");
67     is_int(ENOMEM, oerrno, "...with correct errno");
68     base = malloc(10);
69     p = test_reallocarray(base, 3, SIZE_MAX / 3);
70     oerrno = errno;
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);
75     oerrno = errno;
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);
79     oerrno = errno;
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);
83     oerrno = errno;
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);
87     oerrno = errno;
88     ok(p == NULL, "reallocarray fails for 2, SIZE_MAX");
89     is_int(ENOMEM, oerrno, "...with correct errno");
90
91     /* Clean up and exit. */
92     free(base);
93     return 0;
94 }