bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainenstatic void test_malloc_overflow_multiply(void)
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen test_assert_idx(MALLOC_MULTIPLY(tests[i].a, tests[i].b) == tests[i].a * tests[i].b, i);
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen test_assert_idx(MALLOC_MULTIPLY(tests[i].b, tests[i].a) == tests[i].b * tests[i].a, i);
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen unsigned short n = 2;
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen /* check that no compiler warning is given */
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen test_assert_idx(MALLOC_ADD(tests[i].a, tests[i].b) == tests[i].a + tests[i].b, i);
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen test_assert_idx(MALLOC_ADD(tests[i].b, tests[i].a) == tests[i].b + tests[i].a, i);
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainenstatic enum fatal_test_state fatal_malloc_overflow_multiply(unsigned int *stage)
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen const struct {
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen unsigned int i;
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen test_expect_fatal_string("memory allocation overflow");
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen i_error("%"PRIuSIZE_T, MALLOC_MULTIPLY((size_t)SIZE_MAX/2, (uint8_t)3));
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen i_error("%"PRIuSIZE_T, MALLOC_MULTIPLY((uint8_t)3, (size_t)SIZE_MAX/2));
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen i_error("%"PRIuSIZE_T, MALLOC_MULTIPLY(mul_tests[i].a, mul_tests[i].b));
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen i_error("%"PRIuSIZE_T, MALLOC_MULTIPLY(mul_tests[i].b, mul_tests[i].a));
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainenstatic enum fatal_test_state fatal_malloc_overflow_add(unsigned int *stage)
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen const struct {
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen unsigned int i;
ba1a5db879b08d2fefcb42160af67853cdfe7687Timo Sirainen test_expect_fatal_string("memory allocation overflow");
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen i_error("%"PRIuSIZE_T, MALLOC_ADD((size_t)SIZE_MAX, (uint8_t)1));
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen i_error("%"PRIuSIZE_T, MALLOC_ADD((uint8_t)1, (size_t)SIZE_MAX));
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen i_error("%"PRIuSIZE_T, MALLOC_ADD(add_tests[i].a, add_tests[i].b));
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainen i_error("%"PRIuSIZE_T, MALLOC_ADD(add_tests[i].b, add_tests[i].a));
b716136fc47efd434d60be5db262b4013e375fa9Timo Sirainenenum fatal_test_state fatal_malloc_overflow(unsigned int stage)