Skip to content

Commit bbdc9e0

Browse files
authored
Merge pull request #24574 from dcci/boundaries-reflection-5.1
2 parents 7c09198 + df6fb25 commit bbdc9e0

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

include/swift/Reflection/Records.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ 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+
uintptr_t nameAddr = (uintptr_t)FieldName.get() + Offset;
91+
if (nameAddr < Low || nameAddr > High)
92+
return "";
93+
return (const char *)nameAddr;
9294
}
9395

9496
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 = (uintptr_t)(FD.second->ReflectionString.Metadata.startAddress());
198+
auto High = (uintptr_t)(FD.second->ReflectionString.Metadata.endAddress());
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 = (uintptr_t)sections.ReflectionString.Metadata.startAddress();
345+
auto High = (uintptr_t)sections.ReflectionString.Metadata.endAddress();
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, std::numeric_limits<uint64_t>::max());
331332

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

0 commit comments

Comments
 (0)