Skip to content

[llvm-profdata] Move error handling logic out of normal input's code path #67177

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

Merged
merged 1 commit into from
Sep 25, 2023
Merged
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
23 changes: 8 additions & 15 deletions llvm/lib/ProfileData/SampleProfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,14 @@ bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) {

template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
unsigned NumBytesRead = 0;
std::error_code EC;
uint64_t Val = decodeULEB128(Data, &NumBytesRead);

if (Val > std::numeric_limits<T>::max())
EC = sampleprof_error::malformed;
else if (Data + NumBytesRead > End)
EC = sampleprof_error::truncated;
else
EC = sampleprof_error::success;

if (EC) {
if (Val > std::numeric_limits<T>::max()) {
std::error_code EC = sampleprof_error::malformed;
reportError(0, EC.message());
return EC;
} else if (Data + NumBytesRead > End) {
std::error_code EC = sampleprof_error::truncated;
reportError(0, EC.message());
return EC;
}
Expand All @@ -485,10 +482,9 @@ template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
}

ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
std::error_code EC;
StringRef Str(reinterpret_cast<const char *>(Data));
if (Data + Str.size() + 1 > End) {
EC = sampleprof_error::truncated;
std::error_code EC = sampleprof_error::truncated;
reportError(0, EC.message());
return EC;
}
Expand All @@ -499,10 +495,8 @@ ErrorOr<StringRef> SampleProfileReaderBinary::readString() {

template <typename T>
ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {
std::error_code EC;

if (Data + sizeof(T) > End) {
EC = sampleprof_error::truncated;
std::error_code EC = sampleprof_error::truncated;
reportError(0, EC.message());
return EC;
}
Expand All @@ -514,7 +508,6 @@ ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {

template <typename T>
inline ErrorOr<size_t> SampleProfileReaderBinary::readStringIndex(T &Table) {
std::error_code EC;
auto Idx = readNumber<size_t>();
if (std::error_code EC = Idx.getError())
return EC;
Expand Down