]> eyrie.org Git - kerberos/krb5-strength.git/blob - portable/reallocarray.c
Change CrackLib tests for system CrackLib
[kerberos/krb5-strength.git] / portable / reallocarray.c
1 /*
2  * Replacement for a missing reallocarray.
3  *
4  * Provides the same functionality as the OpenBSD library function
5  * reallocarray for those systems that don't have it.  This function is the
6  * same as realloc, but takes the size arguments in the same form as calloc
7  * and checks for overflow so that the caller doesn't need to.
8  *
9  * The canonical version of this file is maintained in the rra-c-util package,
10  * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
11  *
12  * Written by Russ Allbery <eagle@eyrie.org>
13  *
14  * The authors hereby relinquish any claim to any copyright that they may have
15  * in this work, whether granted under contract or by operation of law or
16  * international treaty, and hereby commit to the public, at large, that they
17  * shall not, at any time in the future, seek to enforce any copyright in this
18  * work against any person or entity, or prevent any person or entity from
19  * copying, publishing, distributing or creating derivative works of this
20  * work.
21  */
22
23 #include <config.h>
24 #include <portable/system.h>
25
26 #include <errno.h>
27
28 /*
29  * If we're running the test suite, rename reallocarray to avoid conflicts
30  * with the system version.  #undef it first because some systems may define
31  * it to another name.
32  */
33 #if TESTING
34 # undef reallocarray
35 # define reallocarray test_reallocarray
36 void *test_reallocarray(void *, size_t, size_t);
37 #endif
38
39 /*
40  * nmemb * size cannot overflow if both are smaller than sqrt(SIZE_MAX).  We
41  * can calculate that value statically by using 2^(sizeof(size_t) * 8) as the
42  * value of SIZE_MAX and then taking the square root, which gives
43  * 2^(sizeof(size_t) * 4).  Compute the exponentiation with shift.
44  */
45 #define CHECK_THRESHOLD (1UL << (sizeof(size_t) * 4))
46
47 void *
48 reallocarray(void *ptr, size_t nmemb, size_t size)
49 {
50     if (nmemb >= CHECK_THRESHOLD || size >= CHECK_THRESHOLD)
51         if (nmemb > 0 && SIZE_MAX / nmemb <= size) {
52             errno = ENOMEM;
53             return NULL;
54         }
55     return realloc(ptr, nmemb * size);
56 }