Skip to content

Commit 0460669

Browse files
author
Davide Italiano
committed
[Reflection] Check that the offset is within the section.
<rdar://problem/49043621>
1 parent 470ce2f commit 0460669

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

include/swift/Reflection/Records.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ class FieldRecord {
8585
(const char *)((uintptr_t)MangledTypeName.get() + Offset));
8686
}
8787

88-
StringRef getFieldName(uintptr_t Offset) const {
89-
if (FieldName)
90-
return (const char *)((uintptr_t)FieldName.get() + Offset);
91-
return "";
88+
StringRef getFieldName(uintptr_t Offset, uintptr_t Low,
89+
uintptr_t High) const {
90+
if (Offset < Low || Offset > High)
91+
return "";
92+
return (const char *)((uintptr_t)FieldName.get() + Offset);
9293
}
9394

9495
bool isIndirectCase() const {

stdlib/public/Reflection/TypeRefBuilder.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ bool TypeRefBuilder::getFieldTypeRefs(
194194
- FD.second->TypeReference.SectionOffset;
195195
auto FieldOffset = FD.second->Field.SectionOffset
196196
- FD.second->ReflectionString.SectionOffset;
197-
auto FieldName = Field.getFieldName(FieldOffset);
197+
auto Low = FD.second->ReflectionString.SectionOffset;
198+
auto High = FD.second->ReflectionString.Metadata.size();
199+
auto FieldName = Field.getFieldName(FieldOffset, Low, High);
198200

199201
// Empty cases of enums do not have a type
200202
if (FD.first->isEnum() && !Field.hasMangledTypeName()) {
@@ -339,8 +341,10 @@ void TypeRefBuilder::dumpFieldSection(std::ostream &OS) {
339341
OS << '-';
340342
OS << '\n';
341343
for (auto &field : descriptor) {
342-
OS << std::string(field.getFieldName(NameOffset).begin(),
343-
field.getFieldName(NameOffset).end());
344+
auto Low = sections.ReflectionString.SectionOffset;
345+
auto High = sections.ReflectionString.Metadata.size();
346+
OS << std::string(field.getFieldName(NameOffset, Low, High).begin(),
347+
field.getFieldName(NameOffset, Low, High).end());
344348
if (field.hasMangledTypeName()) {
345349
OS << ": ";
346350
dumpTypeRef(field.getMangledTypeName(TypeRefOffset), OS);

stdlib/public/runtime/ReflectionMirror.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ static bool _shouldReportMissingReflectionMetadataWarnings() {
327327

328328
const FieldDescriptor &descriptor = *fields;
329329
auto &field = descriptor.getFields()[index];
330-
auto name = field.getFieldName(0);
330+
// Bounds are always valid as the offset is constant.
331+
auto name = field.getFieldName(0, 0, 1);
331332

332333
// Enum cases don't always have types.
333334
if (!field.hasMangledTypeName())

0 commit comments

Comments
 (0)