File tree Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -42,13 +42,16 @@ static_assert(sizeof(SmallVector<void *, 1>) ==
42
42
// / on POD-like datatypes and is out of line to reduce code duplication.
43
43
void SmallVectorBase::grow_pod (void *FirstEl, size_t MinCapacity,
44
44
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) );
48
48
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" );
52
55
53
56
void *NewElts;
54
57
if (BeginX == FirstEl) {
You can’t perform that action at this time.
0 commit comments