diff options
author | Elliott Hughes <enh@google.com> | 2018-09-18 12:52:42 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2018-09-26 14:24:18 -0700 |
commit | b177085ce7219562eecf77f2e8de49f8f2605005 (patch) | |
tree | 573947a27714273f7d78e9c6f85c9fd30e63dba9 /tests/malloc_test.cpp | |
parent | e4e3de819d05481422f8bb9925486118924bf4a1 (diff) |
Add reallocarray(3).
Originally a BSD extension, now in glibc too. We've used it internally
for a while.
(cherry-pick of e4b13f7e3ca68edfcc5faedc5e7d4e13c4e8edb9.)
Bug: http://b/112163459
Test: ran tests
Change-Id: I813c3a62b13ddb91ba41e32a5a853d09207ea6bc
Merged-In: I813c3a62b13ddb91ba41e32a5a853d09207ea6bc
Diffstat (limited to 'tests/malloc_test.cpp')
-rw-r--r-- | tests/malloc_test.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp index 4161c9094..c4f13f6e7 100644 --- a/tests/malloc_test.cpp +++ b/tests/malloc_test.cpp @@ -26,6 +26,12 @@ #include "private/bionic_config.h" +#if defined(__BIONIC__) +#define HAVE_REALLOCARRAY 1 +#else +#define HAVE_REALLOCARRAY __GLIBC_PREREQ(2, 26) +#endif + TEST(malloc, malloc_std) { // Simple malloc test. void *ptr = malloc(100); @@ -497,3 +503,31 @@ TEST(malloc, mallopt_smoke) { // mallopt doesn't set errno. ASSERT_EQ(0, errno); } + +TEST(malloc, reallocarray_overflow) { +#if HAVE_REALLOCARRAY + // Values that cause overflow to a result small enough (8 on LP64) that malloc would "succeed". + size_t a = static_cast<size_t>(INTPTR_MIN + 4); + size_t b = 2; + + errno = 0; + ASSERT_TRUE(reallocarray(nullptr, a, b) == nullptr); + ASSERT_EQ(ENOMEM, errno); + + errno = 0; + ASSERT_TRUE(reallocarray(nullptr, b, a) == nullptr); + ASSERT_EQ(ENOMEM, errno); +#else + GTEST_LOG_(INFO) << "This test requires a C library with reallocarray.\n"; +#endif +} + +TEST(malloc, reallocarray) { +#if HAVE_REALLOCARRAY + void* p = reallocarray(nullptr, 2, 32); + ASSERT_TRUE(p != nullptr); + ASSERT_GE(malloc_usable_size(p), 64U); +#else + GTEST_LOG_(INFO) << "This test requires a C library with reallocarray.\n"; +#endif +} |