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
diff --git a/libc/bionic/malloc_common.cpp b/libc/bionic/malloc_common.cpp
index 5a5ec76..8bf44a1 100644
--- a/libc/bionic/malloc_common.cpp
+++ b/libc/bionic/malloc_common.cpp
@@ -184,6 +184,15 @@
return Malloc(realloc)(old_mem, bytes);
}
+extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
+ size_t new_size;
+ if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
+ errno = ENOMEM;
+ return nullptr;
+ }
+ return realloc(old_mem, new_size);
+}
+
#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
extern "C" void* pvalloc(size_t bytes) {
auto _pvalloc = __libc_globals->malloc_dispatch.pvalloc;