Bug 1423461 - Report the right size to the OOM handler for moz_xcalloc. r?njn draft
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 06 Dec 2017 16:10:30 +0900
changeset 708196 853c5c695612b6aa007a936ff2adf1e3a5d3eb30
parent 708195 653db78c48078f009526866c2fecb4eb9e7be084
child 743130 82a178e2d74f9c99ad3ecd27ca504b55f018792c
push id92320
push userbmo:mh+mozilla@glandium.org
push dateWed, 06 Dec 2017 10:46:02 +0000
reviewersnjn
bugs1423461
milestone59.0a1
Bug 1423461 - Report the right size to the OOM handler for moz_xcalloc. r?njn
memory/mozalloc/mozalloc.cpp
--- a/memory/mozalloc/mozalloc.cpp
+++ b/memory/mozalloc/mozalloc.cpp
@@ -54,16 +54,17 @@ MOZ_MEMORY_API char *strndup_impl(const 
 
 #include <errno.h>
 #include <new>                  // for std::bad_alloc
 #include <string.h>
 
 #include <sys/types.h>
 
 #include "mozilla/Assertions.h"
+#include "mozilla/CheckedInt.h"
 #include "mozilla/Likely.h"
 #include "mozilla/mozalloc.h"
 #include "mozilla/mozalloc_oom.h"  // for mozalloc_handle_oom
 
 void*
 moz_xmalloc(size_t size)
 {
     void* ptr = malloc_impl(size);
@@ -74,17 +75,18 @@ moz_xmalloc(size_t size)
     return ptr;
 }
 
 void*
 moz_xcalloc(size_t nmemb, size_t size)
 {
     void* ptr = calloc_impl(nmemb, size);
     if (MOZ_UNLIKELY(!ptr && nmemb && size)) {
-        mozalloc_handle_oom(size);
+        mozilla::CheckedInt<size_t> totalSize = mozilla::CheckedInt<size_t>(nmemb) * size;
+        mozalloc_handle_oom(totalSize.isValid() ? totalSize.value() : SIZE_MAX);
         return moz_xcalloc(nmemb, size);
     }
     return ptr;
 }
 
 void*
 moz_xrealloc(void* ptr, size_t size)
 {