From 3d8b83927b40584abe62c6599f876d49e646770a Mon Sep 17 00:00:00 2001 From: Mehdi Mulani Date: Mon, 23 Jul 2018 14:44:20 -0700 Subject: [PATCH] Use module copies in RCTBridge APIs Summary: This array can be modified while it's being iterated, so we need to take a copy. I also found another crash and guarded against it. Reviewed By: fkgozali Differential Revision: D8955708 fbshipit-source-id: 76250bc5d451776e74505733c0f3c14e555fb576 --- React/Base/RCTBridge.m | 2 +- React/CxxBridge/RCTCxxBridge.mm | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/React/Base/RCTBridge.m b/React/Base/RCTBridge.m index acfc09dd376c60..ade50c05dc1a50 100644 --- a/React/Base/RCTBridge.m +++ b/React/Base/RCTBridge.m @@ -246,7 +246,7 @@ - (id)moduleForClass:(Class)moduleClass - (NSArray *)modulesConformingToProtocol:(Protocol *)protocol { NSMutableArray *modules = [NSMutableArray new]; - for (Class moduleClass in self.moduleClasses) { + for (Class moduleClass in [self.moduleClasses copy]) { if ([moduleClass conformsToProtocol:protocol]) { id module = [self moduleForClass:moduleClass]; if (module) { diff --git a/React/CxxBridge/RCTCxxBridge.mm b/React/CxxBridge/RCTCxxBridge.mm index 184612f1c1fc21..a35425efdd1383 100644 --- a/React/CxxBridge/RCTCxxBridge.mm +++ b/React/CxxBridge/RCTCxxBridge.mm @@ -544,8 +544,9 @@ - (void)_initializeBridge:(std::shared_ptr)executorFactory RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTCxxBridge initModulesWithDispatchGroup:] autoexported moduleData", nil); - NSMutableArray *moduleDataByID = [NSMutableArray arrayWithCapacity:moduleClasses.count]; - for (Class moduleClass in moduleClasses) { + NSArray *moduleClassesCopy = [moduleClasses copy]; + NSMutableArray *moduleDataByID = [NSMutableArray arrayWithCapacity:moduleClassesCopy.count]; + for (Class moduleClass in moduleClassesCopy) { NSString *moduleName = RCTBridgeModuleNameForClass(moduleClass); // Check for module name collisions