From e9c7ebfd9aab2fb5f231a19d6e3ba5cac5626237 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Thu, 29 Oct 2015 05:13:58 -0700 Subject: [PATCH] Ensure bad JS does not crash the app Summary: public We have code in place to ensure that a red box is displayed when bad arguments are sent to exported methods, however the methods were still being called with nil values for those arguments, resulting in crashes if the method wasn't set up to handle nil gracefully. This diff ensures that methods will not be called if any of the argument conversion functions log an error. It also explicitly checks for nil output for arguments that are marked as nonnull. Reviewed By: javache, tadeuzagallo Differential Revision: D2580658 fb-gh-sync-id: aad6be758ea19f9b4521f3f9f0407bf672c0a2dd --- React/Base/RCTModuleMethod.m | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/React/Base/RCTModuleMethod.m b/React/Base/RCTModuleMethod.m index efc0076ef..21d6447b4 100644 --- a/React/Base/RCTModuleMethod.m +++ b/React/Base/RCTModuleMethod.m @@ -363,12 +363,23 @@ void RCTParseObjCMethodName(NSString **objCMethodName, NSArray **arguments) if (nullability == RCTNonnullable) { RCTArgumentBlock oldBlock = argumentBlocks[i - 2]; argumentBlocks[i - 2] = ^(RCTBridge *bridge, NSUInteger index, id json) { - if (json == nil) { - RCTLogArgumentError(weakSelf, index, typeName, "must not be null"); - return NO; - } else { - return oldBlock(bridge, index, json); + if (json != nil) { + if (!oldBlock(bridge, index, json)) { + return NO; + } + if (isNullableType) { + // Check converted value wasn't null either, as method probably + // won't gracefully handle a nil vallue for a nonull argument + void *value; + [invocation getArgument:&value atIndex:index + 2]; + if (value == NULL) { + return NO; + } + } + return YES; } + RCTLogArgumentError(weakSelf, index, typeName, "must not be null"); + return NO; }; } }