mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
Add some index path validation to avoid production crashes (#2716)
This commit is contained in:
committed by
Hannah Troisi
parent
42c7bb291e
commit
45c69ada36
@@ -358,6 +358,13 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
|
||||
[self reloadDataWithCompletion:nil];
|
||||
}
|
||||
|
||||
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
|
||||
{
|
||||
if ([self validateIndexPath:indexPath]) {
|
||||
[super scrollToItemAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)reloadDataImmediately
|
||||
{
|
||||
ASDisplayNodeAssertMainThread();
|
||||
@@ -620,8 +627,35 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the index path is a valid view-index-path, and returns it if so, nil otherwise.
|
||||
*/
|
||||
- (nullable NSIndexPath *)validateIndexPath:(nullable NSIndexPath *)indexPath
|
||||
{
|
||||
if (indexPath == nil) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSInteger section = indexPath.section;
|
||||
if (section >= self.numberOfSections) {
|
||||
ASDisplayNodeFailAssert(@"Collection view index path has invalid section %lu, section count = %lu", (unsigned long)section, (unsigned long)self.numberOfSections);
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (indexPath.item >= [self numberOfItemsInSection:section]) {
|
||||
ASDisplayNodeFailAssert(@"Collection view index path has invalid item %lu in section %lu, item count = %lu", (unsigned long)indexPath.item, (unsigned long)section, (unsigned long)[self numberOfItemsInSection:section]);
|
||||
return nil;
|
||||
}
|
||||
|
||||
return indexPath;
|
||||
}
|
||||
|
||||
- (NSIndexPath *)convertIndexPathToCollectionNode:(NSIndexPath *)indexPath
|
||||
{
|
||||
if ([self validateIndexPath:indexPath] == nil) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
// If this is a section index path, we don't currently have a method
|
||||
// to do a mapping.
|
||||
if (indexPath.item == NSNotFound) {
|
||||
@@ -656,7 +690,7 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
|
||||
|
||||
- (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode
|
||||
{
|
||||
return [_dataController completedIndexPathForNode:cellNode];
|
||||
return [self validateIndexPath:[_dataController completedIndexPathForNode:cellNode]];
|
||||
}
|
||||
|
||||
- (NSArray *)visibleNodes
|
||||
|
||||
Reference in New Issue
Block a user