Skip to content

Commit

Permalink
Fix crash when requesting OOB layout attributes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ryan Nystrom authored and facebook-github-bot committed Apr 19, 2017
1 parent 693cb8a commit 4441bd8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Source/IGListCollectionViewLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde
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);
Expand All @@ -192,8 +200,13 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind
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);

Expand Down
30 changes: 30 additions & 0 deletions Tests/IGListCollectionViewLayoutTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -694,4 +694,34 @@ - (void)test_ {
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

0 comments on commit 4441bd8

Please sign in to comment.