From eddb0a5f21cb005097baed3a478f0ca0ca55c185 Mon Sep 17 00:00:00 2001 From: Zhisheng Huang Date: Fri, 31 Aug 2018 18:30:46 -0700 Subject: [PATCH] Safely check the sectionIndex to be within the right bound before converting to use convertSectionReloadToItemUpdates Summary: This made the items reload safer, as I tested that passing in an out-of-bound section index to -[UICollectionView numberOfItemsInSection:] would crash the app. UIKit did not handle this gracefully. Reviewed By: calimarkus Differential Revision: D9624061 fbshipit-source-id: bcf14a3d4746b648046992adf221faff36a0257c --- Source/IGListAdapterUpdater.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/IGListAdapterUpdater.m b/Source/IGListAdapterUpdater.m index 37eb290f..9adf6027 100644 --- a/Source/IGListAdapterUpdater.m +++ b/Source/IGListAdapterUpdater.m @@ -330,7 +330,9 @@ static NSArray *convertSectionReloadToItemUpdates(NSIndexSet *sec if (self.preferItemReloadsForSectionReloads) { [reloads enumerateIndexesUsingBlock:^(NSUInteger sectionIndex, BOOL * _Nonnull stop) { NSMutableIndexSet *localIndexSet = [NSMutableIndexSet indexSetWithIndex:sectionIndex]; - if ([collectionView numberOfItemsInSection:sectionIndex] == [collectionView.dataSource collectionView:collectionView numberOfItemsInSection:sectionIndex]) { + if (sectionIndex < [collectionView numberOfSections] + && sectionIndex < [collectionView.dataSource numberOfSectionsInCollectionView:collectionView] + && [collectionView numberOfItemsInSection:sectionIndex] == [collectionView.dataSource collectionView:collectionView numberOfItemsInSection:sectionIndex]) { // Perfer to do item reloads instead, if the number of items in section is unchanged. [itemUpdates addObjectsFromArray:convertSectionReloadToItemUpdates(localIndexSet, collectionView)]; } else {