From 2c196bbc6bd897b3dcc1d87a3baac28e1e88df41 Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Fri, 13 Nov 2020 22:37:25 +0000 Subject: [PATCH] Add an assertion in SmallVector::push_back() This assertion ensures the input value isn't part of the vector when growing is required. In such cases the vector will grow and the input value is invalidated before being read from. This found 14 failed Tests. Reviewed By: bkramer Differential Revision: https://reviews.llvm.org/D84293 --- llvm/include/llvm/ADT/SmallVector.h | 10 ++++++++++ llvm/include/llvm/MC/MCInst.h | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h index e042497..fbc8ede 100644 --- a/llvm/include/llvm/ADT/SmallVector.h +++ b/llvm/include/llvm/ADT/SmallVector.h @@ -136,6 +136,13 @@ protected: 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 @@ protected: 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 @@ public: } 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 @@ protected: public: void push_back(const T &Elt) { + this->assertSafeToPush(&Elt); if (LLVM_UNLIKELY(this->size() >= this->capacity())) this->grow(); memcpy(reinterpret_cast(this->end()), &Elt, sizeof(T)); diff --git a/llvm/include/llvm/MC/MCInst.h b/llvm/include/llvm/MC/MCInst.h index 360dbda..2ce2ee0 100644 --- a/llvm/include/llvm/MC/MCInst.h +++ b/llvm/include/llvm/MC/MCInst.h @@ -181,7 +181,7 @@ public: 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::iterator; using const_iterator = SmallVectorImpl::const_iterator; -- 2.7.4