From 3e5ef465d294d26829b95e59e945ff4255f96abd Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Thu, 13 Jul 2017 15:26:36 -0400 Subject: Use PartitionAlloc with PartitionAllocReturnNull MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Tom Sepez --- core/fxcrt/fx_memory.cpp | 5 +++-- core/fxcrt/fx_memory.h | 32 ++++++++++++++++---------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/core/fxcrt/fx_memory.cpp b/core/fxcrt/fx_memory.cpp index 589a4cf508..6a592a12d2 100644 --- a/core/fxcrt/fx_memory.cpp +++ b/core/fxcrt/fx_memory.cpp @@ -26,8 +26,9 @@ void FXMEM_InitializePartitionAlloc() { // 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 fdf64db190..65cf19c980 100644 --- a/core/fxcrt/fx_memory.h +++ b/core/fxcrt/fx_memory.h @@ -40,22 +40,23 @@ NEVER_INLINE void FX_OutOfMemoryTerminate(); 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_SafeRealloc(void* ptr, size_t num_members, size_t member_size) { 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::max() / h) { + if (w < std::numeric_limits::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_AllocOrDie2D(size_t w, size_t h, size_t member_size) { 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 @@ inline void FX_Free(void* ptr) { // // 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. -- cgit v1.2.3