Skip to content

Commit 0c1fa5f

Browse files
committed
[Runtime] Avoid +class overrides when initializing an ObjC class.
swift_getInitializedObjCClass called [c class] to trigger class initialization, and returned the value. This wreaked havoc when the class in question overrides +class. Instead, ignore the return value and return c. Switch from +class to +self, which is much less likely to be overridden. Calling an overridden method could have performance downsides or even cause unwanted side effects. rdar://problem/49853091
1 parent 6d7f3f6 commit 0c1fa5f

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

stdlib/public/runtime/SwiftObject.mm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,11 @@ id swift_dynamicCastObjCProtocolConditional(id object,
12691269
// Used when we have class metadata and we want to ensure a class has been
12701270
// initialized by the Objective-C runtime. We need to do this because the
12711271
// class "c" might be valid metadata, but it hasn't been initialized yet.
1272-
return [c class];
1272+
// Send a message that's likely not to be overridden to minimize potential
1273+
// side effects. Ignore the return value in case it is overridden to
1274+
// return something different. See SR-10463 for an example.
1275+
[c self];
1276+
return c;
12731277
}
12741278

12751279
static const ClassMetadata *

0 commit comments

Comments
 (0)