Skip to content

Commit 213efbb

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 f70c41b commit 213efbb

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
@@ -1274,7 +1274,11 @@ id swift_dynamicCastObjCProtocolConditional(id object,
12741274
// Used when we have class metadata and we want to ensure a class has been
12751275
// initialized by the Objective-C runtime. We need to do this because the
12761276
// class "c" might be valid metadata, but it hasn't been initialized yet.
1277-
return [c class];
1277+
// Send a message that's likely not to be overridden to minimize potential
1278+
// side effects. Ignore the return value in case it is overridden to
1279+
// return something different. See SR-10463 for an example.
1280+
[c self];
1281+
return c;
12781282
}
12791283

12801284
static const ClassMetadata *

0 commit comments

Comments
 (0)