summaryrefslogtreecommitdiff
path: root/tests/malloc_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/malloc_test.cpp')
-rw-r--r--tests/malloc_test.cpp34
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
+}