From 369e7d3a7d8d523e5c89b8d95d58603986d7380a Mon Sep 17 00:00:00 2001 From: Mike Ash Date: Fri, 9 Dec 2022 12:42:56 -0500 Subject: [PATCH] [Reflection] Make TypeRefVisitor robust to NULL typerefs and unknown kinds. These shouldn't happen, but we may encounter bad data which results in a NULL typeref pointer, or a bogus kind value. rdar://103111816 rdar://103105744 --- include/swift/Reflection/TypeRef.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/swift/Reflection/TypeRef.h b/include/swift/Reflection/TypeRef.h index 9e20f97525648..7f107f9adf7b2 100644 --- a/include/swift/Reflection/TypeRef.h +++ b/include/swift/Reflection/TypeRef.h @@ -1024,6 +1024,10 @@ class TypeRefVisitor { public: RetTy visit(const TypeRef *typeRef, Args... args) { + // We shouldn't get a NULL typeRef, but handle it gracefully if we do. + if (!typeRef) + return RetTy(); + switch (typeRef->getKind()) { #define TYPEREF(Id, Parent) \ case TypeRefKind::Id: \ @@ -1033,7 +1037,9 @@ class TypeRefVisitor { #include "swift/Reflection/TypeRefs.def" } - swift_unreachable("Unhandled TypeRefKind in switch."); + // We shouldn't get an unknown kind, but bad data might result in an unknown + // value showing up here. Handle it gracefully when that happens. + return RetTy(); } };