diff options
author | Alexander Dahl <ada@thorsis.com> | 2022-12-23 10:21:27 +0100 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2024-02-02 11:26:52 +0100 |
commit | e738d6eada34361fb8497acd808278410946022f (patch) | |
tree | 4563e2932b0f5bf81b281f4492801d23a9a0ba51 | |
parent | a01cc69ee9536f90e57c61a198f2d1944d3d4313 (diff) | |
download | cmocka-e738d6eada34361fb8497acd808278410946022f.zip cmocka-e738d6eada34361fb8497acd808278410946022f.tar.gz cmocka-e738d6eada34361fb8497acd808278410946022f.tar.bz2 |
tests: Add test for test_calloc()
calloc() is prone to integer overflow on multiplication of its arguments.
glibc, musl, and uclibc test for that in its implementations and return NULL in
that case with errno set to ENOMEM. cmocka lacks such a check and passes all
kinds of overflown values to test_malloc() with different ways to fail.
Signed-off-by: Alexander Dahl <ada@thorsis.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
(cherry picked from commit d87c470017e095a01d9e9233f35c0b5254431648)
-rw-r--r-- | tests/test_alloc.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/tests/test_alloc.c b/tests/test_alloc.c index 966814a..95f2b37 100644 --- a/tests/test_alloc.c +++ b/tests/test_alloc.c @@ -10,6 +10,25 @@ #include <stdio.h> #include <string.h> +static void torture_test_calloc(void **state) +{ + void *ptr; + + (void)state; /* unsused */ + + ptr = test_calloc(2, SIZE_MAX); + assert_null(ptr); + ptr = test_calloc(SIZE_MAX, 2); + assert_null(ptr); + + /* overflows to 0 */ + ptr = test_calloc(2, (SIZE_MAX/2)+1); + assert_null(ptr); + + ptr = test_calloc(3, (SIZE_MAX/2)+42); + assert_null(ptr); +} + static void torture_test_malloc(void **state) { char *str; @@ -82,6 +101,7 @@ static void torture_test_realloc_set0(void **state) int main(void) { const struct CMUnitTest alloc_tests[] = { + cmocka_unit_test(torture_test_calloc), cmocka_unit_test(torture_test_malloc), cmocka_unit_test(torture_test_realloc), cmocka_unit_test(torture_test_realloc_set0), |