Skip to content

[Reflection] Gracefully handle reflection sections with bad sizes. #58770

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
May 12, 2022
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
16 changes: 14 additions & 2 deletions include/swift/Reflection/TypeRefBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ class ReflectionSectionIteratorBase
: OriginalSize(Size), Cur(Cur), Size(Size), Name(Name) {
if (Size != 0) {
auto NextRecord = this->operator*();
if (!NextRecord) {
// NULL record pointer, don't attempt to proceed. Setting size to 0 will
// make this iterator compare equal to the end iterator.
this->Size = 0;
return;
}
auto NextSize = Self::getCurrentRecordSize(NextRecord);
if (NextSize > Size) {
std::cerr << "!!! Reflection section too small to contain first record\n" << std::endl;
Expand All @@ -109,7 +115,9 @@ class ReflectionSectionIteratorBase
<< ", size of first record: "
<< NextSize
<< std::endl;
abort();
// Set this iterator equal to the end. This section is effectively
// empty.
this->Size = 0;
}
}
}
Expand Down Expand Up @@ -149,14 +157,18 @@ class ReflectionSectionIteratorBase
std::cerr << std::hex << std::setw(2) << (int)p[i] << " ";
}
std::cerr << std::endl;
abort();
Size = 0; // Set this iterator equal to the end.
}
}

return asImpl();
}

bool operator==(const Self &other) const {
// Size = 0 means we're at the end even if Cur doesn't match. This allows
// iterators that encounter an incorrect size to safely end iteration.
if (Size == 0 && other.Size == 0)
return true;
return Cur == other.Cur && Size == other.Size;
}

Expand Down