From 1a92c15b50a2742d55713d6e5de7aaed6f855a42 Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Mon, 7 Nov 2016 15:31:08 -0800 Subject: [PATCH] Return nil if accesing OOB section Summary: Rare crashers that take `NSNotFound` from `sectionFor...` API and then immediately look up the object w/out checking for not found. Instead return nil. Reviewed By: dshahidehpour Differential Revision: D4142946 fbshipit-source-id: ca80f87729b5ee6699740de897d73b819d27d132 --- Source/Internal/IGListSectionMap.m | 7 ++++++- Tests/{IGListObjectMapTests.m => IGListSectionMapTests.m} | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) rename Tests/{IGListObjectMapTests.m => IGListSectionMapTests.m} (90%) diff --git a/Source/Internal/IGListSectionMap.m b/Source/Internal/IGListSectionMap.m index 323cde0c..bce0927a 100644 --- a/Source/Internal/IGListSectionMap.m +++ b/Source/Internal/IGListSectionMap.m @@ -75,7 +75,12 @@ } - (id)objectForSection:(NSUInteger)section { - return self.objects[section]; + NSArray *objects = self.objects; + if (section < objects.count) { + return objects[section]; + } else { + return nil; + } } - (NSUInteger)sectionForObject:(id)object { diff --git a/Tests/IGListObjectMapTests.m b/Tests/IGListSectionMapTests.m similarity index 90% rename from Tests/IGListObjectMapTests.m rename to Tests/IGListSectionMapTests.m index c19200bf..9d210614 100644 --- a/Tests/IGListObjectMapTests.m +++ b/Tests/IGListSectionMapTests.m @@ -82,4 +82,12 @@ XCTAssertEqual(counter, 2); } +- (void)test_whenAccessingOOBSection_thatNilIsReturned { + NSArray *objects = @[@0, @1, @2]; + NSArray *sectionControllers = @[@"a", @"b", @"c"]; + IGListSectionMap *map = [[IGListSectionMap alloc] initWithMapTable:[NSMapTable strongToStrongObjectsMapTable]]; + [map updateWithObjects:objects sectionControllers:sectionControllers]; + XCTAssertNil([map objectForSection:4]); +} + @end