Skip to content

Commit 614f4be

Browse files
committed
[Runtime] Reject suffixes on ObjC mangled class names.
The demangler tolerates arbitrary suffixes on mangled names, and parses them as a Suffix node. When looking up a class by an ObjC mangled name, we don't want such demanglings to succeed, because this will result in false positives. It's expected that NSClassFromString(someClassName + "some suffix") will fail, unless something has actually created a class with that suffix. rdar://problem/60012296
1 parent 516b3e4 commit 614f4be

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ class Node {
213213
NodePointer getFirstChild() const {
214214
return getChild(0);
215215
}
216+
NodePointer getLastChild() const {
217+
return getChild(getNumChildren() - 1);
218+
}
216219
NodePointer getChild(size_t index) const {
217220
assert(getNumChildren() > index);
218221
return begin()[index];

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,13 @@ getObjCClassByMangledName(const char * _Nonnull typeName,
16871687
auto node = demangler.demangleSymbol(typeName);
16881688
if (!node)
16891689
return NO;
1690+
1691+
// If we successfully demangled but there is a suffix, then we did NOT use
1692+
// the entire name, and this is NOT a match. Reject it.
1693+
if (node->hasChildren() &&
1694+
node->getLastChild()->getKind() == Node::Kind::Suffix)
1695+
return NO;
1696+
16901697
metadata = swift_getTypeByMangledNode(
16911698
MetadataState::Complete, demangler, node,
16921699
nullptr,

test/Interpreter/SDK/objc_getClass.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ testSuite.test("NotPresent") {
261261
expectNil(NSClassFromString("\u{1}badnews"));
262262
expectNil(NSClassFromString("$s\u{1}badnews"));
263263
expectNil(NSClassFromString("_T\u{1}badnews"));
264+
265+
// Correct mangled names with additional text afterwards should not resolve.
266+
expectNil(NSClassFromString("_TtC4main20MangledSwiftSubclass_"))
267+
expectNil(NSClassFromString("_TtC4main22MangledSwiftSuperclassXYZ"))
268+
expectNil(NSClassFromString("_TtC4main19MangledObjCSubclass123"))
269+
expectNil(NSClassFromString("_TtC4main21MangledObjCSuperclasswhee"))
264270
}
265271

266272
runAllTests()

0 commit comments

Comments
 (0)