Fix calloc() overflow
authorXi Wang <xi.wang@gmail.com>
Wed, 14 Mar 2012 20:46:49 +0000 (04:46 +0800)
committerIvan Maidanski <ivmai@mail.ru>
Thu, 15 Mar 2012 15:46:02 +0000 (19:46 +0400)
* malloc.c (calloc): Check multiplication overflow in calloc(),
assuming REDIRECT_MALLOC.

malloc.c

index aeda69362c9ce397d72126a42e26fac44d6c29c5..5c3374da31f67ee57cbd1cef0230c9f0f6f6d8fa 100644 (file)
--- a/malloc.c
+++ b/malloc.c
@@ -369,8 +369,13 @@ void * malloc(size_t lb)
   }
 #endif /* GC_LINUX_THREADS */
 
+#ifndef SIZE_MAX
+#define SIZE_MAX (~(size_t)0)
+#endif
 void * calloc(size_t n, size_t lb)
 {
+    if (lb && n > SIZE_MAX / lb)
+      return NULL;
 #   if defined(GC_LINUX_THREADS) /* && !defined(USE_PROC_FOR_LIBRARIES) */
         /* libpthread allocated some memory that is only pointed to by  */
         /* mmapped thread stacks.  Make sure it's not collectable.      */