From: Waldemar Kozaczuk <[email protected]>
Committer: Waldemar Kozaczuk <[email protected]>
Branch: master

mempool: fix reallocarray to properly handle overflow

Signed-off-by: Waldemar Kozaczuk <[email protected]>

---
diff --git a/core/mempool.cc b/core/mempool.cc
--- a/core/mempool.cc
+++ b/core/mempool.cc
@@ -2014,9 +2014,14 @@ void* realloc(void* obj, size_t size)
     return buf;
 }
 
-extern "C" void *reallocarray(void *ptr, size_t nmemb, size_t size)
+extern "C" void *reallocarray(void *ptr, size_t nmemb, size_t elem_size)
 {
-    return realloc(ptr, nmemb * size);
+    size_t bytes;
+    if (__builtin_mul_overflow(nmemb, elem_size, &bytes)) {
+        errno = ENOMEM;
+        return 0;
+    }
+    return realloc(ptr, nmemb * elem_size);
 }
 
 size_t malloc_usable_size(void* obj)

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/0000000000004d131105a4e73938%40google.com.

Reply via email to