Skip to content

Commit

Permalink
[Sample Profile Reader] Fix potential integer overflow/infinite loop …
Browse files Browse the repository at this point in the history
…bug in sample profile reader

Change loop induction variable type to match the type of "SIZE" where it's compared against, to prevent infinite loop caused by overflow wraparound if there are more than 2^32 samples

Reviewed By: wenlei

Differential Revision: https://reviews.llvm.org/D132493
  • Loading branch information
huangjd committed Aug 23, 2022
1 parent de54bcc commit b105656
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions llvm/lib/ProfileData/SampleProfReader.cpp
Expand Up @@ -809,7 +809,7 @@ std::error_code SampleProfileReaderExtBinaryBase::readFuncOffsetTable() {
OrderedFuncOffsets->reserve(*Size);
}

for (uint32_t I = 0; I < *Size; ++I) {
for (uint64_t I = 0; I < *Size; ++I) {
auto FContext(readSampleContextFromTable());
if (std::error_code EC = FContext.getError())
return EC;
Expand Down Expand Up @@ -1096,7 +1096,7 @@ std::error_code SampleProfileReaderExtBinaryBase::readMD5NameTable() {
return sampleprof_error::success;
}
NameTable.reserve(*Size);
for (uint32_t I = 0; I < *Size; ++I) {
for (uint64_t I = 0; I < *Size; ++I) {
auto FID = readNumber<uint64_t>();
if (std::error_code EC = FID.getError())
return EC;
Expand Down Expand Up @@ -1237,7 +1237,7 @@ std::error_code SampleProfileReaderCompactBinary::readNameTable() {
if (std::error_code EC = Size.getError())
return EC;
NameTable.reserve(*Size);
for (uint32_t I = 0; I < *Size; ++I) {
for (uint64_t I = 0; I < *Size; ++I) {
auto FID = readNumber<uint64_t>();
if (std::error_code EC = FID.getError())
return EC;
Expand Down Expand Up @@ -1279,7 +1279,7 @@ std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTable() {
if (std::error_code EC = EntryNum.getError())
return EC;

for (uint32_t i = 0; i < (*EntryNum); i++)
for (uint64_t i = 0; i < (*EntryNum); i++)
if (std::error_code EC = readSecHdrTableEntry(i))
return EC;

Expand Down Expand Up @@ -1448,7 +1448,7 @@ std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
return EC;

FuncOffsetTable.reserve(*Size);
for (uint32_t I = 0; I < *Size; ++I) {
for (uint64_t I = 0; I < *Size; ++I) {
auto FName(readStringFromTable());
if (std::error_code EC = FName.getError())
return EC;
Expand Down

0 comments on commit b105656

Please sign in to comment.