Skip to content

Commit 49148e6

Browse files
authored
Merge pull request #58770 from apple/weird-reflection-section-size-handling
[Reflection] Gracefully handle reflection sections with bad sizes.
2 parents eec29fa + b0c2fd0 commit 49148e6

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Diff for: include/swift/Reflection/TypeRefBuilder.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ class ReflectionSectionIteratorBase
100100
: OriginalSize(Size), Cur(Cur), Size(Size), Name(Name) {
101101
if (Size != 0) {
102102
auto NextRecord = this->operator*();
103+
if (!NextRecord) {
104+
// NULL record pointer, don't attempt to proceed. Setting size to 0 will
105+
// make this iterator compare equal to the end iterator.
106+
this->Size = 0;
107+
return;
108+
}
103109
auto NextSize = Self::getCurrentRecordSize(NextRecord);
104110
if (NextSize > Size) {
105111
std::cerr << "!!! Reflection section too small to contain first record\n" << std::endl;
@@ -109,7 +115,9 @@ class ReflectionSectionIteratorBase
109115
<< ", size of first record: "
110116
<< NextSize
111117
<< std::endl;
112-
abort();
118+
// Set this iterator equal to the end. This section is effectively
119+
// empty.
120+
this->Size = 0;
113121
}
114122
}
115123
}
@@ -149,14 +157,18 @@ class ReflectionSectionIteratorBase
149157
std::cerr << std::hex << std::setw(2) << (int)p[i] << " ";
150158
}
151159
std::cerr << std::endl;
152-
abort();
160+
Size = 0; // Set this iterator equal to the end.
153161
}
154162
}
155163

156164
return asImpl();
157165
}
158166

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

0 commit comments

Comments
 (0)