[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 07:13:10 PDT 2018


trixirt created this revision.
trixirt added reviewers: pete, sanjoy.
Herald added a subscriber: llvm-commits.

Check that memory allocation succeeds before use.
Rename variable DescInfo to DI for consistency.


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;
+  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*
+  User *Obj = nullptr;
   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;
+  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.148405.patch
Type: text/x-patch
Size: 2072 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180524/4af53aa1/attachment.bin>


More information about the llvm-commits mailing list