[COMMIT osv master] libc: add reallocarray

6 views
Skip to first unread message

Commit Bot

unread,
May 3, 2020, 6:27:16 PM5/3/20
to osv...@googlegroups.com, Waldemar Kozaczuk
From: Waldemar Kozaczuk <jwkoz...@gmail.com>
Committer: Waldemar Kozaczuk <jwkoz...@gmail.com>
Branch: master

libc: add reallocarray

Some apps like xclock from Fedora need reallocarray() which
this patch adds as simple delegation to realloc.

Signed-off-by: Waldemar Kozaczuk <jwkoz...@gmail.com>

---
diff --git a/core/mempool.cc b/core/mempool.cc
--- a/core/mempool.cc
+++ b/core/mempool.cc
@@ -2014,6 +2014,11 @@ void* realloc(void* obj, size_t size)
return buf;
}

+extern "C" void *reallocarray(void *ptr, size_t nmemb, size_t size)
+{
+ return realloc(ptr, nmemb * size);
+}
+
size_t malloc_usable_size(void* obj)
{
if ( obj == nullptr ) {

Waldek Kozaczuk

unread,
May 3, 2020, 6:29:14 PM5/3/20
to Waldemar Kozaczuk, OSv Development
Technically realloc should handle this:

" However, unlike that realloc() call, reallocarray() fails safely in
       the case where the multiplication would overflow.  If such an over‐
       flow occurs, reallocarray() returns NULL, sets errno to ENOMEM, and
       leaves the original block of memory unchanged."
Reply all
Reply to author
Forward
0 new messages