Skip to content

Commit 5862e02

Browse files
tabudzfacebook-github-bot
authored andcommitted
Re-sync with internal repository (#772)
Co-authored-by: Facebook Community Bot <[email protected]>
1 parent f3b9649 commit 5862e02

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/llvm/Support/SmallVector.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ static_assert(sizeof(SmallVector<void *, 1>) ==
4242
/// on POD-like datatypes and is out of line to reduce code duplication.
4343
void SmallVectorBase::grow_pod(void *FirstEl, size_t MinCapacity,
4444
size_t TSize) {
45-
// Ensure we can fit the new capacity in 32 bits.
46-
if (MinCapacity > UINT32_MAX)
47-
report_bad_alloc_error("SmallVector capacity overflow during allocation");
45+
constexpr size_t MinGrowth = 1;
46+
size_t NewCapacity = 2 * capacity() + MinGrowth; // Always grow.
47+
NewCapacity = static_cast<unsigned>(std::max(NewCapacity, MinCapacity));
4848

49-
size_t NewCapacity = 2 * capacity() + 1; // Always grow.
50-
NewCapacity =
51-
std::min(std::max(NewCapacity, MinCapacity), size_t(UINT32_MAX));
49+
// Ensure that NewCapacity did not overflow an unsigned int,
50+
// and that the capacity in bytes will not overflow a size_t.
51+
if (NewCapacity <= this->capacity() ||
52+
NewCapacity < MinCapacity ||
53+
NewCapacity > size_t(-1) / TSize)
54+
report_bad_alloc_error("SmallVector capacity overflow during allocation");
5255

5356
void *NewElts;
5457
if (BeginX == FirstEl) {

0 commit comments

Comments
 (0)