1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
/*
* reallocarray test suite.
*
* This does some simple sanity checks and checks some of the overflow
* detection, but isn't particularly thorough.
*
* The canonical version of this file is maintained in the rra-c-util package,
* which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
*
* Written by Russ Allbery <eagle@eyrie.org>
* Copyright 2014 Russ Allbery <eagle@eyrie.org>
* Copyright 2014
* The Board of Trustees of the Leland Stanford Junior University
*
* Copying and distribution of this file, with or without modification, are
* permitted in any medium without royalty provided the copyright notice and
* this notice are preserved. This file is offered as-is, without any
* warranty.
*
* SPDX-License-Identifier: FSFAP
*/
#include <config.h>
#include <portable/system.h>
#include <errno.h>
#include <tests/tap/basic.h>
void *test_reallocarray(void *, size_t, size_t);
int
main(void)
{
char *p, *base;
size_t sqrt_max;
int oerrno;
plan(15);
/* Test success cases and write to the memory for valgrind checks. */
p = test_reallocarray(NULL, 2, 5);
memcpy(p, "123456789", 10);
is_string("123456789", p, "reallocarray of NULL");
p = test_reallocarray(p, 4, 5);
is_string("123456789", p, "reallocarray after resize");
memcpy(p + 9, "0123456789", 11);
is_string("1234567890123456789", p, "write to larger memory segment");
free(p);
/*
* If nmemb or size are 0, we should either get NULL or a pointer we can
* free. Make sure we don't get something weird, like division by zero.
*/
p = test_reallocarray(NULL, 0, 100);
if (p != NULL)
free(p);
p = test_reallocarray(NULL, 100, 0);
if (p != NULL)
free(p);
/* Test the range-checking error cases. */
p = test_reallocarray(NULL, 2, SIZE_MAX / 2);
oerrno = errno;
ok(p == NULL, "reallocarray fails for 2, SIZE_MAX / 2");
is_int(ENOMEM, oerrno, "...with correct errno");
base = malloc(10);
p = test_reallocarray(base, 3, SIZE_MAX / 3);
oerrno = errno;
ok(p == NULL, "reallocarray fails for 3, SIZE_MAX / 3");
is_int(ENOMEM, oerrno, "...with correct errno");
sqrt_max = (1UL << (sizeof(size_t) * 4));
p = test_reallocarray(base, sqrt_max, sqrt_max);
oerrno = errno;
ok(p == NULL, "reallocarray fails for sqrt(SIZE_MAX), sqrt(SIZE_MAX)");
is_int(ENOMEM, oerrno, "...with correct errno");
p = test_reallocarray(base, 1, SIZE_MAX);
oerrno = errno;
ok(p == NULL, "reallocarray fails for 1, SIZE_MAX");
is_int(ENOMEM, oerrno, "...with correct errno");
p = test_reallocarray(base, SIZE_MAX, 1);
oerrno = errno;
ok(p == NULL, "reallocarray fails for SIZE_MAX, 1");
is_int(ENOMEM, oerrno, "...with correct errno");
p = test_reallocarray(base, 2, SIZE_MAX);
oerrno = errno;
ok(p == NULL, "reallocarray fails for 2, SIZE_MAX");
is_int(ENOMEM, oerrno, "...with correct errno");
/* Clean up and exit. */
free(base);
return 0;
}
|