From 742251da1cc76ebe8427046f945f77fd0754a10d Mon Sep 17 00:00:00 2001 From: Mark Sinkovics Date: Mon, 12 Oct 2020 01:45:43 -0700 Subject: [PATCH] fix: crash when insert nil value into a dictionary (#30066) Summary: This PR attempts to fix issue https://github.com/facebook/react-native/issues/28278 and https://github.com/facebook/react-native/issues/29525 On Crashlytics, the following error occurs in file `RCTWebSocketModule.m` at method `-[RCTWebSocketModule webSocket:didFailWithError:]` when a nil value is inserted into a dictionary as a value. ``` Fatal Exception: NSInvalidArgumentException *** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0] ``` This PR is following the suggestion of this: https://github.com/facebook/react-native/issues/28278#issuecomment-597461650 and it replaces the values of any property if it is nil. In detail: - it converts `error` to empty NSString if the original value is nil - it converts `socketID` to a NSNumber object, which stores `-1` if the original value is nil ## Changelog [iOS] [Fixed] - A crash in WebSocket module Pull Request resolved: https://github.com/facebook/react-native/pull/30066 Test Plan: We were not able to reproduce the crash, but the report itself provided enough information to find a solution for this issue. Reviewed By: shergin Differential Revision: D24241147 Pulled By: sammy-SC fbshipit-source-id: d5d632b49ca77b5d8be8b9c32358bef68f17d30a --- React/CoreModules/RCTWebSocketModule.mm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/React/CoreModules/RCTWebSocketModule.mm b/React/CoreModules/RCTWebSocketModule.mm index 9648b18e4fed45..c120bc627baf24 100644 --- a/React/CoreModules/RCTWebSocketModule.mm +++ b/React/CoreModules/RCTWebSocketModule.mm @@ -166,7 +166,9 @@ - (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error NSNumber *socketID = [webSocket reactTag]; _contentHandlers[socketID] = nil; _sockets[socketID] = nil; - [self sendEventWithName:@"websocketFailed" body:@{@"message" : error.localizedDescription, @"id" : socketID}]; + NSDictionary *body = + @{@"message" : error.localizedDescription ?: @"Undefined, error is nil", @"id" : socketID ?: @(-1)}; + [self sendEventWithName:@"websocketFailed" body:body]; } - (void)webSocket:(RCTSRWebSocket *)webSocket