diff --git a/src/util-inl.h b/src/util-inl.h index caadba9dae2caa..d6b7bd5e1b4cd1 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -361,13 +361,11 @@ T* UncheckedRealloc(T* pointer, size_t n) { // As per spec realloc behaves like malloc if passed nullptr. template inline T* UncheckedMalloc(size_t n) { - if (n == 0) n = 1; return UncheckedRealloc(nullptr, n); } template inline T* UncheckedCalloc(size_t n) { - if (n == 0) n = 1; MultiplyWithOverflowCheck(sizeof(T), n); return static_cast(calloc(n, sizeof(T))); } diff --git a/test/cctest/test_util.cc b/test/cctest/test_util.cc index a64942a8a35009..c355fbca58e8a0 100644 --- a/test/cctest/test_util.cc +++ b/test/cctest/test_util.cc @@ -97,39 +97,39 @@ TEST(UtilTest, ToLower) { EXPECT_EQ('a', ToLower('A')); } -#define TEST_AND_FREE(expression) \ - do { \ - auto pointer = expression; \ - EXPECT_NE(nullptr, pointer); \ - free(pointer); \ +#define TEST_AND_FREE(expression, size) \ + do { \ + auto pointer = expression(size); \ + if (size != 0) EXPECT_NE(pointer, nullptr); \ + free(pointer); \ } while (0) TEST(UtilTest, Malloc) { - TEST_AND_FREE(Malloc(0)); - TEST_AND_FREE(Malloc(1)); - TEST_AND_FREE(Malloc(0)); - TEST_AND_FREE(Malloc(1)); + TEST_AND_FREE(Malloc, 0); + TEST_AND_FREE(Malloc, 1); + TEST_AND_FREE(Malloc, 0); + TEST_AND_FREE(Malloc, 1); } TEST(UtilTest, Calloc) { - TEST_AND_FREE(Calloc(0)); - TEST_AND_FREE(Calloc(1)); - TEST_AND_FREE(Calloc(0)); - TEST_AND_FREE(Calloc(1)); + TEST_AND_FREE(Calloc, 0); + TEST_AND_FREE(Calloc, 1); + TEST_AND_FREE(Calloc, 0); + TEST_AND_FREE(Calloc, 1); } TEST(UtilTest, UncheckedMalloc) { - TEST_AND_FREE(UncheckedMalloc(0)); - TEST_AND_FREE(UncheckedMalloc(1)); - TEST_AND_FREE(UncheckedMalloc(0)); - TEST_AND_FREE(UncheckedMalloc(1)); + TEST_AND_FREE(UncheckedMalloc, 0); + TEST_AND_FREE(UncheckedMalloc, 1); + TEST_AND_FREE(UncheckedMalloc, 0); + TEST_AND_FREE(UncheckedMalloc, 1); } TEST(UtilTest, UncheckedCalloc) { - TEST_AND_FREE(UncheckedCalloc(0)); - TEST_AND_FREE(UncheckedCalloc(1)); - TEST_AND_FREE(UncheckedCalloc(0)); - TEST_AND_FREE(UncheckedCalloc(1)); + TEST_AND_FREE(UncheckedCalloc, 0); + TEST_AND_FREE(UncheckedCalloc, 1); + TEST_AND_FREE(UncheckedCalloc, 0); + TEST_AND_FREE(UncheckedCalloc, 1); } template