Skip to content

Commit 79e603c

Browse files
admirsahetafacebook-github-bot
authored andcommitted
Null Exception Handling | Input Validation - RCTAlertController - RCTDevLoadingView (#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
1 parent aacf287 commit 79e603c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

React/CoreModules/RCTAlertController.m

+10-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ @implementation RCTAlertController
2020
- (UIWindow *)alertWindow
2121
{
2222
if (_alertWindow == nil) {
23-
_alertWindow = [[UIWindow alloc] initWithFrame:RCTSharedApplication().keyWindow.bounds];
24-
_alertWindow.rootViewController = [UIViewController new];
25-
_alertWindow.windowLevel = UIWindowLevelAlert + 1;
23+
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
24+
if (keyWindow) {
25+
_alertWindow = [[UIWindow alloc] initWithFrame:keyWindow.bounds];
26+
_alertWindow.rootViewController = [UIViewController new];
27+
_alertWindow.windowLevel = UIWindowLevelAlert + 1;
28+
} else {
29+
// keyWindow is nil, so we cannot create and initialize _alertWindow
30+
NSLog(@"Unable to create alert window: keyWindow is nil");
31+
}
2632
}
33+
2734
return _alertWindow;
2835
}
2936

React/CoreModules/RCTDevLoadingView.mm

+14
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(
110110
return;
111111
}
112112

113+
// Input validation
114+
if (message == nil || [message isEqualToString:@""]) {
115+
NSLog(@"Error: message cannot be nil or empty");
116+
return;
117+
}
118+
if (color == nil) {
119+
NSLog(@"Error: color cannot be nil");
120+
return;
121+
}
122+
if (backgroundColor == nil) {
123+
NSLog(@"Error: backgroundColor cannot be nil");
124+
return;
125+
}
126+
113127
dispatch_async(dispatch_get_main_queue(), ^{
114128
self->_showDate = [NSDate date];
115129
if (!self->_window && !RCTRunningInTestEnvironment()) {

0 commit comments

Comments
 (0)