[PATCH] D63668: [Support] Improve zero-size allocation with safe_malloc, etc.
Xing Xue via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 25 08:15:36 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rGece53d0ae50a: Improve zero-size allocation with safe_malloc, etc. (authored by xingxue).
Changed prior to commit:
https://reviews.llvm.org/D63668?vs=206050&id=206457#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D63668/new/
https://reviews.llvm.org/D63668
Files:
llvm/include/llvm/Support/MemAlloc.h
Index: llvm/include/llvm/Support/MemAlloc.h
===================================================================
--- llvm/include/llvm/Support/MemAlloc.h
+++ llvm/include/llvm/Support/MemAlloc.h
@@ -24,23 +24,41 @@
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;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63668.206457.patch
Type: text/x-patch
Size: 1778 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190625/01968642/attachment.bin>
More information about the llvm-commits
mailing list