[PATCH] D77601: Make SmallVector assert if it cannot grow.

Andrew via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 6 18:34:07 PDT 2020


browneee updated this revision to Diff 255557.
browneee marked 2 inline comments as done.
browneee added a comment.
Herald added a subscriber: dexonsmith.

Added function comment on grow_pod.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D77601

Files:
  llvm/include/llvm/ADT/SmallVector.h
  llvm/lib/Support/SmallVector.cpp


Index: llvm/lib/Support/SmallVector.cpp
===================================================================
--- llvm/lib/Support/SmallVector.cpp
+++ 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 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));
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ 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 fatal error if it cannot increase capacity.
   void grow_pod(void *FirstEl, size_t MinCapacity, size_t TSize);
 
 public:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77601.255557.patch
Type: text/x-patch
Size: 1714 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200407/2bd9f76f/attachment.bin>


More information about the llvm-commits mailing list