[llvm] r364322 - Improve zero-size allocation with safe_malloc, etc.

Xing Xue via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 25 08:08:29 PDT 2019


Author: xingxue
Date: Tue Jun 25 08:08:28 2019
New Revision: 364322

URL: http://llvm.org/viewvc/llvm-project?rev=364322&view=rev
Log:
Improve zero-size allocation with safe_malloc, etc.

Summary:
The current implementations of the memory allocation functions mistake a nullptr returned from std::malloc, std::calloc, or std::realloc as a failure. The behaviour for each of std::malloc, std::calloc, and std::realloc when the size is 0 is implementation defined (ISO/IEC 9899:2018 7.22.3), and may return a nullptr.

This patch checks if space requested is zero when a nullptr is returned, retry requesting non-zero if it is.

Authored By: andusy

Reviewers: hubert.reinterpretcast, xingxue, jasonliu

Reviewed By: hubert.reinterpretcast, xingxue, abrachet

Subscribers: abrachet, jsji, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D63668

Modified:
    llvm/trunk/include/llvm/Support/MemAlloc.h

Modified: llvm/trunk/include/llvm/Support/MemAlloc.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemAlloc.h?rev=364322&r1=364321&r2=364322&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MemAlloc.h (original)
+++ llvm/trunk/include/llvm/Support/MemAlloc.h Tue Jun 25 08:08:28 2019
@@ -24,23 +24,41 @@ namespace llvm {
 
 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
   void *Result = std::malloc(Sz);
-  if (Result == nullptr)
+  if (Result == nullptr) {
+    // It is implementation-defined whether allocation occurs if the space
+    // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
+    // non-zero, if the space requested was zero.
+    if (Sz == 0)
+      return safe_malloc(1);
     report_bad_alloc_error("Allocation failed");
+  }
   return Result;
 }
 
 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
                                                         size_t Sz) {
   void *Result = std::calloc(Count, Sz);
-  if (Result == nullptr)
+  if (Result == nullptr) {
+    // It is implementation-defined whether allocation occurs if the space
+    // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
+    // non-zero, if the space requested was zero.
+    if (Count == 0 || Sz == 0)
+      return safe_malloc(1);
     report_bad_alloc_error("Allocation failed");
+  }
   return Result;
 }
 
 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
   void *Result = std::realloc(Ptr, Sz);
-  if (Result == nullptr)
+  if (Result == nullptr) {
+    // It is implementation-defined whether allocation occurs if the space
+    // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
+    // non-zero, if the space requested was zero.
+    if (Sz == 0)
+      return safe_malloc(1);
     report_bad_alloc_error("Allocation failed");
+  }
   return Result;
 }
 




More information about the llvm-commits mailing list