From 7f3cc256b5bcbf2e64540ca69401f62ec6869f0e Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Tue, 26 Apr 2022 16:30:29 -0700 Subject: [PATCH] Prevent Nullptr segfault in TurboModule init path Summary: During the TurboModule init path, the TurboModuleManager asks the application to create the TurboModule object, given its class. If the application is unable to create the TurboModule object, what should we do? 0. **What we do now:** Continue executing TurboModule init path. 1. Silently return nil early. 2. Silently return nil early, and RCTLogError. If we Continue executing the TurobModule init path, we'll run into a segfault, because we'll call objc_setAssociatedObject(nil, ...). This diff prevents that segfault, by doing a silent return of nil. Changelog: [iOS][Fixed] - Prevent Nullptr segfault in TurboModule init path Reviewed By: fkgozali Differential Revision: D35942323 fbshipit-source-id: 7755800379c4bc733502314f3af3f401e9b04872 --- .../core/platform/ios/RCTTurboModuleManager.mm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModuleManager.mm b/ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModuleManager.mm index 6b63449d78b..5c24790a1aa 100644 --- a/ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModuleManager.mm +++ b/ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModuleManager.mm @@ -521,6 +521,19 @@ static Class getFallbackClassFromName(const char *name) std::lock_guard delegateGuard(_turboModuleManagerDelegateMutex); module = [_delegate getModuleInstanceFromClass:moduleClass]; } + + /** + * If the application is unable to create the TurboModule object from its class: + * abort TurboModule creation, and early return nil. + */ + if (!module) { + RCTLogWarn( + @"TurboModuleManager delegate %@ returned nil TurboModule object for module with name=\"%s\" and class=%@", + NSStringFromClass([_delegate class]), + moduleName, + NSStringFromClass(moduleClass)); + return nil; + } } else { module = [moduleClass new]; }