Skip to content

Commit 4a5dc2a

Browse files
committed
[Demangler] Accept overly short type names if they are NUL terminated
swift_getTypeByMangledNameInContext takes a pointer and a length, but some programs pass a pointer to a NUL-terminated C string and an excessive length, implicitly relying on the terminator to end the string early. This worked previously, but commit 7fe2bef made the demangler more strict about bad data. This changes the demangler to successfully parse a name by terminating the name string at a 0 byte encountered where it expects to find an operator. All other cases of bad data continue to be rejected. rdar://137430048
1 parent 56ec625 commit 4a5dc2a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/Demangling/Demangler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,12 @@ NodePointer Demangler::demangleType(StringRef MangledName,
804804
bool Demangler::parseAndPushNodes() {
805805
const auto textSize = Text.size();
806806
while (Pos < textSize) {
807+
// Programs may look up a type by NUL-terminated name with an excessive
808+
// length. Keep them working by returning success if we encounter a NUL in
809+
// the middle of the string where an operator is expected.
810+
if (peekChar() == '\0')
811+
return true;
812+
807813
NodePointer Node = demangleOperator();
808814
if (!Node)
809815
return false;

test/Runtime/demangleToMetadata.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,5 +566,17 @@ if #available(SwiftStdlib 6.0, *) {
566566
}
567567
}
568568

569+
if #available(SwiftStdlib 6.1, *) {
570+
DemangleToMetadataTests.test("NUL-terminated name, excessive length value") {
571+
let t = _getTypeByMangledNameInContext("4main1SV", 256,
572+
genericContext: nil,
573+
genericArguments: nil)
574+
expectNotNil(t)
575+
if let t {
576+
expectEqual(type(of: S()), t)
577+
}
578+
}
579+
}
580+
569581
runAllTests()
570582

0 commit comments

Comments
 (0)