diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h --- a/llvm/include/llvm/ADT/SmallVector.h +++ b/llvm/include/llvm/ADT/SmallVector.h @@ -46,6 +46,7 @@ /// This is an implementation of the grow() method which only works /// on POD-like data types and is out of line to reduce code duplication. + /// This function will report a fatal error if it cannot increase capacity. void grow_pod(void *FirstEl, size_t MinCapacity, size_t TSize); public: diff --git a/llvm/lib/Support/SmallVector.cpp b/llvm/lib/Support/SmallVector.cpp --- a/llvm/lib/Support/SmallVector.cpp +++ b/llvm/lib/Support/SmallVector.cpp @@ -39,12 +39,19 @@ /// grow_pod - This is an implementation of the grow() method which only works /// on POD-like datatypes and is out of line to reduce code duplication. +/// This function will report a fatal error if it cannot increase capacity. void SmallVectorBase::grow_pod(void *FirstEl, size_t MinCapacity, size_t TSize) { // Ensure we can fit the new capacity in 32 bits. if (MinCapacity > UINT32_MAX) report_bad_alloc_error("SmallVector capacity overflow during allocation"); + // Ensure we can meet the guarantee of space for at least one more element. + // The above check alone will not catch the case where grow is called with a + // default MinCapacity of 0, but the current capacity cannot be increased. + if (capacity() == size_t(UINT32_MAX)) + report_bad_alloc_error("SmallVector capacity unable to grow"); + size_t NewCapacity = 2 * capacity() + 1; // Always grow. NewCapacity = std::min(std::max(NewCapacity, MinCapacity), size_t(UINT32_MAX));