NSIndexSet indexesInRange:... dies with empty range

Originator:me
Number:rdar://10908993 Date Originated:21-Feb-2012 10:07 PM
Status:Duplicate/7443208 Resolved:
Product:Mac OS X Product Version:10.7.3 (11D50)
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Calling -[NSIndexSet indexesInRange:options:passingTest:] with an empty range throws an exception:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSIndexSet indexesInRange:options:passingTest:]: last range index (-1) beyond bounds (9223372036854775806)'

This should be valid and return an empty index set. -[NSIndexSet countOfIndexesInRange:] returns 0, and similar methods also correctly handle an empty range. The attached file shows this.

% clang -fobjc-arc -framework Foundation -o indexset indexset.m
% ./indexset
2012-02-21 21:58:29.280 indexset[49158:707] halfRange = {0, 3}
2012-02-21 21:58:29.282 indexset[49158:707] indexes in halfRange: 3
2012-02-21 21:58:29.283 indexset[49158:707] array of 6: <NSIndexSet: 0x7fba19d003c0>[number of indexes: 3 (in 1 ranges), indexes: (0-2)]
2012-02-21 21:58:29.284 indexset[49158:707] halfRange = {0, 0}
2012-02-21 21:58:29.285 indexset[49158:707] indexes in halfRange: 0
2012-02-21 21:58:29.286 indexset[49158:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSIndexSet indexesInRange:options:passingTest:]: last range index (-1) beyond bounds (9223372036854775806)'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff97879fc6 __exceptionPreprocess + 198
	1   libobjc.A.dylib                     0x00007fff97a40d5e objc_exception_throw + 43
	2   Foundation                          0x00007fff91c02170 __NSIndexSetParameterCheckIterate + 126
	3   Foundation                          0x00007fff91c679ad -[NSIndexSet indexesInRange:options:passingTest:] + 85
	4   indexset                            0x0000000101e45d25 firstHalfIndexes + 421
	5   indexset                            0x0000000101e45b29 main + 345
	6   indexset                            0x0000000101e459c4 start + 52
	7   ???                                 0x0000000000000001 0x0 + 1
)
terminate called throwing an exception[1]    49158 abort      ./indexset

Comments

indexset.m

#import <Foundation/Foundation.h>


static NSIndexSet *firstHalfIndexes(NSArray *ary)
{
    NSIndexSet *is = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, ary.count)];
    NSRange halfRange = {0, ary.count / 2};
    NSLog(@"halfRange = %@", NSStringFromRange(halfRange));
    NSLog(@"indexes in halfRange: %lu", [is countOfIndexesInRange:halfRange]);
    return [is indexesInRange:halfRange options:0 passingTest:^BOOL(NSUInteger idx, BOOL *stop) { return YES; }];
}


int main(int argc, const char *argv[])
{
    @autoreleasepool {
        NSLog(@"array of 6: %@", firstHalfIndexes([NSArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", nil]));
        NSLog(@"array of 0: %@", firstHalfIndexes([NSArray array]));
    }
    return 0;
}

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!