From 782d4b53304ac9b994b3f29c633bea28b16ef3a4 Mon Sep 17 00:00:00 2001 From: "Felipe A. Rodriguez" Date: Thu, 20 Jul 2017 18:06:59 +0200 Subject: [PATCH 1/2] Fix endless loop if index set is nil --- Frameworks/Foundation/NSArray.mm | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Frameworks/Foundation/NSArray.mm b/Frameworks/Foundation/NSArray.mm index df6ba3bf20..499de0962e 100644 --- a/Frameworks/Foundation/NSArray.mm +++ b/Frameworks/Foundation/NSArray.mm @@ -818,14 +818,21 @@ - (NSUInteger)hash { @Status Interoperable */ - (NSArray*)objectsAtIndexes:(NSIndexSet*)indexes { - unsigned idx = [indexes firstIndex]; - id ret = [NSMutableArray array]; - unsigned count = [self count]; + NSUInteger idx = NSNotFound; + id ret = nil; + NSUInteger count = [self count]; + + if (indexes == nil) + [NSException raise:NSInvalidArgumentException format:@"No index set"]; + else { + ret = [NSMutableArray array]; + idx = [indexes firstIndex]; + } while (idx != NSNotFound) { if (idx >= count) { TraceCritical(TAG, L"objectsAtIndexes: index > count (%d > %d), throwing exception", idx, count); - [NSException raise:@"Array out of bounds" format:@""]; + [NSException raise:NSRangeException format:@"Index out of bounds"]; return nil; } [ret addObject:[self objectAtIndex:idx]]; From 9c59a33e5c8def20ded17895d4a4dc3307c05b9d Mon Sep 17 00:00:00 2001 From: "Felipe A. Rodriguez" Date: Fri, 21 Jul 2017 02:30:13 +0200 Subject: [PATCH 2/2] code style change --- Frameworks/Foundation/NSArray.mm | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Frameworks/Foundation/NSArray.mm b/Frameworks/Foundation/NSArray.mm index 499de0962e..2c55cd5c53 100644 --- a/Frameworks/Foundation/NSArray.mm +++ b/Frameworks/Foundation/NSArray.mm @@ -818,21 +818,22 @@ - (NSUInteger)hash { @Status Interoperable */ - (NSArray*)objectsAtIndexes:(NSIndexSet*)indexes { + NS_COLLECTION_THROW_IF_NULL_REASON(indexes, @"indexes must not be nil”); NSUInteger idx = NSNotFound; id ret = nil; NSUInteger count = [self count]; - if (indexes == nil) - [NSException raise:NSInvalidArgumentException format:@"No index set"]; - else { - ret = [NSMutableArray array]; - idx = [indexes firstIndex]; - } + if (indexes == nil) + [NSException raise:NSInvalidArgumentException format:@"No index set"]; + else { + ret = [NSMutableArray array]; + idx = [indexes firstIndex]; + } while (idx != NSNotFound) { if (idx >= count) { TraceCritical(TAG, L"objectsAtIndexes: index > count (%d > %d), throwing exception", idx, count); - [NSException raise:NSRangeException format:@"Index out of bounds"]; + [NSException raise:NSRangeException format:@"Index out of bounds"]; return nil; } [ret addObject:[self objectAtIndex:idx]];