Fix crash when requesting OOB layout attributes

Summary: There's a small crash showing up when requesting an index path that doesn't exist. It's totally valid that this could be requested from a product and not the infra. Should fail gracefully.

Differential Revision: D4911349

fbshipit-source-id: eee8891cf9400b3c3cd5539e839296f393f82354
This commit is contained in:
Ryan Nystrom 2017-04-19 08:12:29 -07:00 committed by Facebook Github Bot
parent 693cb8a523
commit 4441bd84cc
2 changed files with 44 additions and 1 deletions

View file

@ -175,6 +175,14 @@ static void adjustZIndexForAttributes(UICollectionViewLayoutAttributes *attribut
return attributes;
}
// avoid OOB errors
const NSInteger section = indexPath.section;
const NSInteger item = indexPath.item;
if (section >= _sectionData.size()
|| item >= _sectionData[section].itemBounds.size()) {
return nil;
}
attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.frame = _sectionData[indexPath.section].itemBounds[indexPath.item];
adjustZIndexForAttributes(attributes);
@ -192,8 +200,13 @@ static void adjustZIndexForAttributes(UICollectionViewLayoutAttributes *attribut
return attributes;
}
UICollectionView *collectionView = self.collectionView;
// avoid OOB errors
const NSInteger section = indexPath.section;
if (section >= _sectionData.size()) {
return nil;
}
UICollectionView *collectionView = self.collectionView;
const IGListSectionEntry entry = _sectionData[section];
const CGFloat minY = CGRectGetMinY(entry.bounds);

View file

@ -694,4 +694,34 @@ XCTAssertEqual(CGRectGetHeight(expected), CGRectGetHeight(frame)); \
IGAssertEqualFrame([self cellForSection:5 item:0].frame, 277, 139, 136, 136);
}
- (void)test_whenQueryingAttributes_withSectionOOB_thatReturnsNil {
[self setUpWithStickyHeaders:NO topInset:0 stretchToEdge:YES];
[self prepareWithData:@[
[[IGLayoutTestSection alloc] initWithInsets:UIEdgeInsetsZero
lineSpacing:0
interitemSpacing:0
headerHeight:0
items:@[
[[IGLayoutTestItem alloc] initWithSize:CGSizeMake(33, 33)],
[[IGLayoutTestItem alloc] initWithSize:CGSizeMake(65, 33)],
]],
]];
XCTAssertNil([self.layout layoutAttributesForItemAtIndexPath:quickPath(4, 0)]);
}
- (void)test_whenQueryingAttributes_withItemOOB_thatReturnsNil {
[self setUpWithStickyHeaders:NO topInset:0 stretchToEdge:YES];
[self prepareWithData:@[
[[IGLayoutTestSection alloc] initWithInsets:UIEdgeInsetsZero
lineSpacing:0
interitemSpacing:0
headerHeight:0
items:@[
[[IGLayoutTestItem alloc] initWithSize:CGSizeMake(33, 33)],
[[IGLayoutTestItem alloc] initWithSize:CGSizeMake(65, 33)],
]],
]];
XCTAssertNil([self.layout layoutAttributesForItemAtIndexPath:quickPath(0, 4)]);
}
@end