Skip to content

Commit 5d1f635

Browse files
committed
[Reflection] Gracefully handle reflection sections with bad sizes.
When a reflection section has a size that only fits a partial record, we detect it, log an error, and abort. However, things like Remote Mirror need to be robust to bad data, so instead handle this by terminating iteration at the last full record. rdar://91954103
1 parent 6a08f14 commit 5d1f635

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 14 additions & 2 deletions
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)