Skip to content

Commit 0855829

Browse files
authored
Demangling variadic tuples should not crash (#31988)
The TypeDecoder logic had a bug that caused crashes when it saw a tuple type with a variadic marker. Since variadic tuples aren't supported, this changes the logic to cleanly reject a tuple with a variadic marker.
1 parent 6527a21 commit 0855829

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

include/swift/Demangling/TypeDecoder.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -716,19 +716,16 @@ class TypeDecoder {
716716

717717
// If the tuple element is labeled, add its label to 'labels'.
718718
unsigned typeChildIndex = 0;
719-
unsigned nameIdx = 0;
720-
if (element->getChild(nameIdx)->getKind() == NodeKind::VariadicMarker) {
721-
variadic = true;
722-
nameIdx = 1;
723-
typeChildIndex = 1;
719+
if (element->getChild(typeChildIndex)->getKind() == NodeKind::VariadicMarker) {
720+
return BuiltType();
724721
}
725-
if (element->getChild(nameIdx)->getKind() == NodeKind::TupleElementName) {
722+
if (element->getChild(typeChildIndex)->getKind() == NodeKind::TupleElementName) {
726723
// Add spaces to terminate all the previous labels if this
727724
// is the first we've seen.
728725
if (labels.empty()) labels.append(elements.size(), ' ');
729726

730727
// Add the label and its terminator.
731-
labels += element->getChild(0)->getText();
728+
labels += element->getChild(typeChildIndex)->getText();
732729
labels += ' ';
733730
typeChildIndex++;
734731

test/Demangle/Inputs/manglings.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,13 @@ $sSD5IndexVy__GD ---> $sSD5IndexVy__GD
351351
$s4test3StrCACycfC ---> {T:$s4test3StrCACycfc} test.Str.__allocating_init() -> test.Str
352352
$s18keypaths_inlinable13KeypathStructV8computedSSvpACTKq ---> key path getter for keypaths_inlinable.KeypathStruct.computed : Swift.String : keypaths_inlinable.KeypathStruct, serialized
353353
$s18resilient_protocol24ResilientDerivedProtocolPxAA0c4BaseE0Tn --> associated conformance descriptor for resilient_protocol.ResilientDerivedProtocol.A: resilient_protocol.ResilientBaseProtocol
354-
$s3red4testyAA3ResOyxSayq_GAEs5ErrorAAq_sAFHD1__HCg_GADyxq_GsAFR_r0_lF --> red.test<A, B where B: Swift.Error>(red.Res<A, B>) -> red.Res<A, [B]>
354+
$s3red4testyAA3ResOyxSayq_GAEs5ErrorAAq_sAFHD1__HCg_GADyxq_GsAFR_r0_lF ---> red.test<A, B where B: Swift.Error>(red.Res<A, B>) -> red.Res<A, [B]>
355355
$s3red4testyAA7OurTypeOy4them05TheirD0Vy5AssocQzGAjE0F8ProtocolAAxAA0c7DerivedH0HD1_AA0c4BaseH0HI1_AieKHA2__HCg_GxmAaLRzlF ---> red.test<A where A: red.OurDerivedProtocol>(A.Type) -> red.OurType<them.TheirType<A.Assoc>>
356-
$s17property_wrappers10WithTuplesV9fractionsSd_S2dtvpfP --> property wrapper backing initializer of property_wrappers.WithTuples.fractions : (Swift.Double, Swift.Double, Swift.Double)
356+
$s17property_wrappers10WithTuplesV9fractionsSd_S2dtvpfP ---> property wrapper backing initializer of property_wrappers.WithTuples.fractions : (Swift.Double, Swift.Double, Swift.Double)
357357
$sSo17OS_dispatch_queueC4sync7executeyyyXE_tFTOTA ---> {T:$sSo17OS_dispatch_queueC4sync7executeyyyXE_tFTO} partial apply forwarder for @nonobjc __C.OS_dispatch_queue.sync(execute: () -> ()) -> ()
358358
$sxq_Idgnr_D ---> @differentiable @callee_guaranteed (@in_guaranteed A) -> (@out B)
359359
$sxq_Ilgnr_D ---> @differentiable(linear) @callee_guaranteed (@in_guaranteed A) -> (@out B)
360360
$sS3fIedgyywd_D ---> @escaping @differentiable @callee_guaranteed (@unowned Swift.Float, @unowned @noDerivative Swift.Float) -> (@unowned Swift.Float)
361361
$sS5fIedtyyywddw_D ---> @escaping @differentiable @convention(thin) (@unowned Swift.Float, @unowned Swift.Float, @unowned @noDerivative Swift.Float) -> (@unowned Swift.Float, @unowned @noDerivative Swift.Float)
362362
$syQo ---> $syQo
363+
$sx1td_t ---> (t: A...)

test/TypeDecoder/invalid_types.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-build-swift -emit-executable %s -g -o %t/structural_types -emit-module
4+
// RUN: sed -ne '/\/\/ *DEMANGLE: /s/\/\/ *DEMANGLE: *//p' < %s > %t/input
5+
// RUN: %lldb-moduleimport-test %t/structural_types -type-from-mangled=%t/input | %FileCheck %s
6+
7+
8+
// If this were supported, it would be `(t: τ_0_0...)`
9+
// But tuples with a variadic 'd' marker are not actually valid Swift types, so
10+
// the type decoder rejects them.
11+
// DEMANGLE: $sx1td_t
12+
// CHECK: Can't resolve type of $sx1td_t

0 commit comments

Comments
 (0)