[PATCH] D47327: Check that memory allocation succeeds before use.
Tom Rix via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu May 24 12:17:14 PDT 2018
trixirt updated this revision to Diff 148462.
trixirt added a comment.
use nothrow version of new
Repository:
rL LLVM
https://reviews.llvm.org/D47327
Files:
lib/IR/User.cpp
Index: lib/IR/User.cpp
===================================================================
--- lib/IR/User.cpp
+++ lib/IR/User.cpp
@@ -121,21 +121,23 @@
assert(DescBytesToAllocate % sizeof(void *) == 0 &&
"We need this to satisfy alignment constraints for Uses");
+ User *Obj = nullptr;
uint8_t *Storage = static_cast<uint8_t *>(
- ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
- Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
- Use *End = Start + Us;
- User *Obj = reinterpret_cast<User*>(End);
- Obj->NumUserOperands = Us;
- Obj->HasHungOffUses = false;
- Obj->HasDescriptor = DescBytes != 0;
- Use::initTags(Start, End);
-
- if (DescBytes != 0) {
- auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
- DescInfo->SizeInBytes = DescBytes;
+ ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate, std::nothrow));
+ if (Storage != nullptr) {
+ Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
+ Use *End = Start + Us;
+ Obj = reinterpret_cast<User*>(End);
+ Obj->NumUserOperands = Us;
+ Obj->HasHungOffUses = false;
+ Obj->HasDescriptor = DescBytes != 0;
+ Use::initTags(Start, End);
+
+ if (DescBytes != 0) {
+ auto *DI = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
+ DI->SizeInBytes = DescBytes;
+ }
}
-
return Obj;
}
@@ -149,13 +151,16 @@
void *User::operator new(size_t Size) {
// Allocate space for a single Use*
- void *Storage = ::operator new(Size + sizeof(Use *));
- Use **HungOffOperandList = static_cast<Use **>(Storage);
- User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
- Obj->NumUserOperands = 0;
- Obj->HasHungOffUses = true;
- Obj->HasDescriptor = false;
- *HungOffOperandList = nullptr;
+ User *Obj = nullptr;
+ void *Storage = ::operator new(Size + sizeof(Use *), std::nothrow);
+ if (Storage != nullptr) {
+ Use **HungOffOperandList = static_cast<Use **>(Storage);
+ Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
+ Obj->NumUserOperands = 0;
+ Obj->HasHungOffUses = true;
+ Obj->HasDescriptor = false;
+ *HungOffOperandList = nullptr;
+ }
return Obj;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47327.148462.patch
Type: text/x-patch
Size: 2226 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180524/cd0c2d04/attachment.bin>
More information about the llvm-commits
mailing list