Skip to content

Commit

Permalink
Null Exception Handling | Input Validation - RCTAlertController - RCT…
Browse files Browse the repository at this point in the history
…DevLoadingView (#35689)

Summary:
Enhancing native iOS modules and preventing crashes inside the RCTAlertController

## Changelog
[iOS][Fixed] - Handle properly a `nil` `keyWindows` in the AlertController

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[IOS] [SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

Pull Request resolved: #35689

Reviewed By: cipolleschi

Differential Revision: D42179169

Pulled By: ryancat

fbshipit-source-id: 05a6788f610db1d222e3c10b3c774c75edaf55f5
  • Loading branch information
admirsaheta authored and facebook-github-bot committed Jan 3, 2023
1 parent aacf287 commit 79e603c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions React/CoreModules/RCTAlertController.m
Expand Up @@ -20,10 +20,17 @@ @implementation RCTAlertController
- (UIWindow *)alertWindow
{
if (_alertWindow == nil) {
_alertWindow = [[UIWindow alloc] initWithFrame:RCTSharedApplication().keyWindow.bounds];
_alertWindow.rootViewController = [UIViewController new];
_alertWindow.windowLevel = UIWindowLevelAlert + 1;
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
if (keyWindow) {
_alertWindow = [[UIWindow alloc] initWithFrame:keyWindow.bounds];
_alertWindow.rootViewController = [UIViewController new];
_alertWindow.windowLevel = UIWindowLevelAlert + 1;
} else {
// keyWindow is nil, so we cannot create and initialize _alertWindow
NSLog(@"Unable to create alert window: keyWindow is nil");
}
}

return _alertWindow;
}

Expand Down
14 changes: 14 additions & 0 deletions React/CoreModules/RCTDevLoadingView.mm
Expand Up @@ -110,6 +110,20 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(
return;
}

// Input validation
if (message == nil || [message isEqualToString:@""]) {
NSLog(@"Error: message cannot be nil or empty");
return;
}
if (color == nil) {
NSLog(@"Error: color cannot be nil");
return;
}
if (backgroundColor == nil) {
NSLog(@"Error: backgroundColor cannot be nil");
return;
}

dispatch_async(dispatch_get_main_queue(), ^{
self->_showDate = [NSDate date];
if (!self->_window && !RCTRunningInTestEnvironment()) {
Expand Down

0 comments on commit 79e603c

Please sign in to comment.