Skip to content

Fix: Security vulnerability in grow_pod #990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/llvm/Support/SmallVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ static_assert(sizeof(SmallVector<void *, 1>) ==
/// on POD-like datatypes and is out of line to reduce code duplication.
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");
constexpr size_t MinGrowth = 1;
size_t NewCapacity = 2 * capacity() + MinGrowth; // Always grow.
NewCapacity = static_cast<unsigned>(std::max(NewCapacity, MinCapacity));

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

void *NewElts;
if (BeginX == FirstEl) {
Expand Down