Skip to content

Commit

Permalink
Avoid calling abstract methods in RCTComposedViewRegistry
Browse files Browse the repository at this point in the history
Summary:
`RCTComposedViewRegistry` extends `NSMutableDictionary` which is a clustered class in iOS.
NSMutableDictionary is techncially an abstract class, but when instantiated by `[NSMutableDictionary new];` the system will return one of concrete classes that inherit from `NSMutableDictionary`, opaquely from the perspective of the caller.

By calling `super`, we are actually calling the not implemented method for the abstract class. If this happen, this can crash the app.

Given that the `RCTComposedViewRegistry` is extending the dictionary only for its interface but is using other mechanisms as storage, is it fair to return `NULL`if the storages don't have the requested view.

## Changelog
[iOS][Fixed] -  Avoid calling abstract methods in RCTComposedViewRegistry

Reviewed By: cortinico

Differential Revision: D56755427

fbshipit-source-id: f5c56dc59ccc6b30c00199b4196c42eb9b021e2b
  • Loading branch information
cipolleschi committed May 1, 2024
1 parent 305249f commit 59e7ed5
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/react-native/React/Modules/RCTUIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,7 @@ - (NSEnumerator *)keyEnumerator
- (id)objectForKey:(id)key
{
if (![key isKindOfClass:[NSNumber class]]) {
return [super objectForKey:key];
return NULL;
}

NSNumber *index = (NSNumber *)key;
Expand All @@ -1705,23 +1705,22 @@ - (id)objectForKey:(id)key
if (view) {
return [RCTUIManager paperViewOrCurrentView:view];
}
return [super objectForKey:key];
return NULL;
}

- (void)removeObjectForKey:(id)key
{
if (![key isKindOfClass:[NSNumber class]]) {
return [super removeObjectForKey:key];
return;
}

NSNumber *tag = (NSNumber *)key;

if (_registry[key]) {
NSMutableDictionary *mutableRegistry = (NSMutableDictionary *)_registry;
[mutableRegistry removeObjectForKey:tag];
} else if ([_uiManager viewForReactTag:tag]) {
[_uiManager removeViewFromRegistry:tag];
} else {
[super removeObjectForKey:key];
}
}

Expand Down

0 comments on commit 59e7ed5

Please sign in to comment.