Use PartitionAlloc with PartitionAllocReturnNull

This CL changes usage of PartitionAlloc in fx_memory to allow null
return value for methods used by external C libraries.

Change-Id: I8e2b5dcfb37e30370606afb9a71a7a1d3a28c097
Reviewed-on: https://pdfium-review.googlesource.com/7770
Commit-Queue: Nicolás Peña <npm@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/fx_memory.cpp b/core/fxcrt/fx_memory.cpp
index 589a4cf..6a592a1 100644
--- a/core/fxcrt/fx_memory.cpp
+++ b/core/fxcrt/fx_memory.cpp
@@ -26,8 +26,9 @@
 
 // TODO(palmer): Remove the |flags| argument.
 void* FXMEM_DefaultAlloc(size_t byte_size, int flags) {
-  return pdfium::base::PartitionAllocGeneric(gGeneralPartitionAllocator.root(),
-                                             byte_size, "GeneralPartition");
+  return pdfium::base::PartitionAllocGenericFlags(
+      gGeneralPartitionAllocator.root(), pdfium::base::PartitionAllocReturnNull,
+      byte_size, "GeneralPartition");
 }
 
 void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size) {
diff --git a/core/fxcrt/fx_memory.h b/core/fxcrt/fx_memory.h
index fdf64db..65cf19c 100644
--- a/core/fxcrt/fx_memory.h
+++ b/core/fxcrt/fx_memory.h
@@ -40,22 +40,23 @@
 inline void* FX_SafeAlloc(size_t num_members, size_t member_size) {
   FX_SAFE_SIZE_T total = member_size;
   total *= num_members;
-  if (!total.IsValid()) {
+  if (!total.IsValid())
     return nullptr;
-  }
-  void* result = pdfium::base::PartitionAllocGeneric(
-      gGeneralPartitionAllocator.root(), total.ValueOrDie(),
-      "GeneralPartition");
-  memset(result, 0, total.ValueOrDie());
+
+  void* result = pdfium::base::PartitionAllocGenericFlags(
+      gGeneralPartitionAllocator.root(), pdfium::base::PartitionAllocReturnNull,
+      total.ValueOrDie(), "GeneralPartition");
+  if (result)
+    memset(result, 0, total.ValueOrDie());
   return result;
 }
 
 inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) {
   FX_SAFE_SIZE_T size = num_members;
   size *= member_size;
-  if (!size.IsValid()) {
+  if (!size.IsValid())
     return nullptr;
-  }
+
   return pdfium::base::PartitionReallocGeneric(
       gGeneralPartitionAllocator.root(), ptr, size.ValueOrDie(),
       "GeneralPartition");
@@ -63,17 +64,17 @@
 
 inline void* FX_AllocOrDie(size_t num_members, size_t member_size) {
   // TODO(tsepez): See if we can avoid the implicit memset(0).
-  if (void* result = FX_SafeAlloc(num_members, member_size)) {
+  if (void* result = FX_SafeAlloc(num_members, member_size))
     return result;
-  }
+
   FX_OutOfMemoryTerminate();  // Never returns.
   return nullptr;             // Suppress compiler warning.
 }
 
 inline void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) {
-  if (w < std::numeric_limits<size_t>::max() / h) {
+  if (w < std::numeric_limits<size_t>::max() / h)
     return FX_AllocOrDie(w * h, member_size);
-  }
+
   FX_OutOfMemoryTerminate();  // Never returns.
   return nullptr;             // Suppress compiler warning.
 }
@@ -81,9 +82,9 @@
 inline void* FX_ReallocOrDie(void* ptr,
                              size_t num_members,
                              size_t member_size) {
-  if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) {
+  if (void* result = FX_SafeRealloc(ptr, num_members, member_size))
     return result;
-  }
+
   FX_OutOfMemoryTerminate();  // Never returns.
   return nullptr;             // Suppress compiler warning.
 }
@@ -111,9 +112,8 @@
   //
   // So this check is hiding (what I consider to be) bugs, and we should try to
   // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690
-  if (ptr) {
+  if (ptr)
     pdfium::base::PartitionFree(ptr);
-  }
 }
 
 // The FX_ArraySize(arr) macro returns the # of elements in an array arr.