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
This commit is contained in:
Nick Lockwood 2015-10-29 05:13:58 -07:00 committed by facebook-github-bot-5
parent b86a6e3b44
commit e9c7ebfd9a
1 changed files with 16 additions and 5 deletions

View File

@ -363,12 +363,23 @@ void RCTParseObjCMethodName(NSString **objCMethodName, NSArray **arguments)
if (nullability == RCTNonnullable) { if (nullability == RCTNonnullable) {
RCTArgumentBlock oldBlock = argumentBlocks[i - 2]; RCTArgumentBlock oldBlock = argumentBlocks[i - 2];
argumentBlocks[i - 2] = ^(RCTBridge *bridge, NSUInteger index, id json) { argumentBlocks[i - 2] = ^(RCTBridge *bridge, NSUInteger index, id json) {
if (json == nil) { if (json != nil) {
RCTLogArgumentError(weakSelf, index, typeName, "must not be null"); if (!oldBlock(bridge, index, json)) {
return NO; return NO;
} else { }
return oldBlock(bridge, index, json); 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;
}; };
} }
} }