[PATCH] D84293: Add an assertion in SmallVector::push_back()

Mehdi AMINI via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 13 16:56:38 PST 2020


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG2c196bbc6bd8: Add an assertion in SmallVector::push_back() (authored by mehdi_amini).

Changed prior to commit:
  https://reviews.llvm.org/D84293?vs=305279&id=305281#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D84293/new/

https://reviews.llvm.org/D84293

Files:
  llvm/include/llvm/ADT/SmallVector.h
  llvm/include/llvm/MC/MCInst.h


Index: llvm/include/llvm/MC/MCInst.h
===================================================================
--- llvm/include/llvm/MC/MCInst.h
+++ llvm/include/llvm/MC/MCInst.h
@@ -181,7 +181,7 @@
   MCOperand &getOperand(unsigned i) { return Operands[i]; }
   unsigned getNumOperands() const { return Operands.size(); }
 
-  void addOperand(const MCOperand &Op) { Operands.push_back(Op); }
+  void addOperand(const MCOperand Op) { Operands.push_back(Op); }
 
   using iterator = SmallVectorImpl<MCOperand>::iterator;
   using const_iterator = SmallVectorImpl<MCOperand>::const_iterator;
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -136,6 +136,13 @@
     this->Size = this->Capacity = 0; // FIXME: Setting Capacity to 0 is suspect.
   }
 
+  void assertSafeToPush(const void *Elt) {
+    assert(
+        (Elt < begin() || Elt >= end() || this->size() < this->capacity()) &&
+        "Attempting to push_back to the vector an element of the vector without"
+        " enough space reserved");
+  }
+
 public:
   using size_type = size_t;
   using difference_type = ptrdiff_t;
@@ -251,6 +258,7 @@
 
 public:
   void push_back(const T &Elt) {
+    this->assertSafeToPush(&Elt);
     if (LLVM_UNLIKELY(this->size() >= this->capacity()))
       this->grow();
     ::new ((void*) this->end()) T(Elt);
@@ -258,6 +266,7 @@
   }
 
   void push_back(T &&Elt) {
+    this->assertSafeToPush(&Elt);
     if (LLVM_UNLIKELY(this->size() >= this->capacity()))
       this->grow();
     ::new ((void*) this->end()) T(::std::move(Elt));
@@ -353,6 +362,7 @@
 
 public:
   void push_back(const T &Elt) {
+    this->assertSafeToPush(&Elt);
     if (LLVM_UNLIKELY(this->size() >= this->capacity()))
       this->grow();
     memcpy(reinterpret_cast<void *>(this->end()), &Elt, sizeof(T));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84293.305281.patch
Type: text/x-patch
Size: 1914 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201114/d3e7208b/attachment.bin>


More information about the cfe-commits mailing list