Skip to content

Commit ff46986

Browse files
committed
Catch a few additional NSInvalidArgument exception cases that the Ccoca collection classes catch.
1 parent e414e1f commit ff46986

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

JSONKit.m

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ + (id)allocWithZone:(NSZone *)zone
615615
JKArray *array = NULL;
616616
if(JK_EXPECT_T((array = (JKArray *)calloc(1UL, _JKArrayInstanceSize)) != NULL)) { // Directly allocate the JKArray instance via calloc.
617617
array->isa = (mutableCollection == NO) ? _JKArrayClass : _JKMutableArrayClass;
618+
if((array = [array init]) == NULL) { return(NULL); }
618619
array->capacity = count;
619620
array->count = count;
620621
if(JK_EXPECT_F((array->objects = (id *)malloc(sizeof(id) * array->capacity)) == NULL)) { [array autorelease]; return(NULL); }
@@ -685,7 +686,8 @@ - (NSUInteger)count
685686
- (void)getObjects:(id *)objectsPtr range:(NSRange)range
686687
{
687688
NSParameterAssert((objects != NULL) && (count <= capacity));
688-
if((range.location > count) || (NSMaxRange(range) > count)) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%lu) beyond bounds (%lu)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), NSMaxRange(range), count]; }
689+
if((objectsPtr == NULL) && (NSMaxRange(range) > 0UL)) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: pointer to objects array is NULL but range length is %lu", NSStringFromClass([self class]), NSStringFromSelector(_cmd), NSMaxRange(range)]; }
690+
if((range.location > count) || (NSMaxRange(range) > count)) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%lu) beyond bounds (%lu)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), NSMaxRange(range), count]; }
689691
memcpy(objectsPtr, objects + range.location, range.length * sizeof(id));
690692
}
691693

@@ -698,7 +700,7 @@ - (id)objectAtIndex:(NSUInteger)objectIndex
698700

699701
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len
700702
{
701-
NSParameterAssert((objects != NULL) && (count <= capacity));
703+
NSParameterAssert((state != NULL) && (stackbuf != NULL) && (len > 0UL) && (objects != NULL) && (count <= capacity));
702704
if(JK_EXPECT_F(state->state == 0UL)) { state->mutationsPtr = (unsigned long *)&mutations; state->itemsPtr = stackbuf; }
703705
if(JK_EXPECT_F(state->state >= count)) { return(0UL); }
704706

@@ -934,6 +936,7 @@ static void _JKDictionaryResizeIfNeccessary(JKDictionary *dictionary) {
934936
JKDictionary *dictionary = NULL;
935937
if(JK_EXPECT_T((dictionary = (JKDictionary *)calloc(1UL, _JKDictionaryInstanceSize)) != NULL)) { // Directly allocate the JKArray instance via calloc.
936938
dictionary->isa = (mutableCollection == NO) ? _JKDictionaryClass : _JKMutableDictionaryClass;
939+
if((dictionary = [dictionary init]) == NULL) { return(NULL); }
937940
dictionary->capacity = _JKDictionaryCapacityForCount(count);
938941
dictionary->count = 0UL;
939942

@@ -1023,6 +1026,7 @@ - (NSUInteger)count
10231026

10241027
- (id)objectForKey:(id)aKey
10251028
{
1029+
NSParameterAssert((entry != NULL) && (count <= capacity));
10261030
JKHashTableEntry *atEntry = _JKDictionaryHashTableEntryForKey(self, aKey);
10271031
return((atEntry != NULL) ? atEntry->object : NULL);
10281032
}
@@ -1050,7 +1054,7 @@ - (void)getObjects:(id *)objects andKeys:(id *)keys
10501054

10511055
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len
10521056
{
1053-
NSParameterAssert((entry != NULL) && (count <= capacity));
1057+
NSParameterAssert((state != NULL) && (stackbuf != NULL) && (len > 0UL) && (entry != NULL) && (count <= capacity));
10541058
if(JK_EXPECT_F(state->state == 0UL)) { state->mutationsPtr = (unsigned long *)&mutations; state->itemsPtr = stackbuf; }
10551059
if(JK_EXPECT_F(state->state >= capacity)) { return(0UL); }
10561060

@@ -1112,7 +1116,7 @@ + (void)load
11121116

11131117
- (void)setObject:(id)anObject forKey:(id)aKey
11141118
{
1115-
if(aKey == NULL) { [NSException raise:NSInvalidArgumentException format:@"*** -[%@ %@]: attempt to insert nil key", NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; }
1119+
if(aKey == NULL) { [NSException raise:NSInvalidArgumentException format:@"*** -[%@ %@]: attempt to insert nil key", NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; }
11161120
if(anObject == NULL) { [NSException raise:NSInvalidArgumentException format:@"*** -[%@ %@]: attempt to insert nil value (key: %@)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), aKey]; }
11171121

11181122
_JKDictionaryResizeIfNeccessary((JKDictionary *)self);
@@ -1126,6 +1130,7 @@ - (void)setObject:(id)anObject forKey:(id)aKey
11261130

11271131
- (void)removeObjectForKey:(id)aKey
11281132
{
1133+
if(aKey == NULL) { [NSException raise:NSInvalidArgumentException format:@"*** -[%@ %@]: attempt to remove nil key", NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; }
11291134
JKHashTableEntry *entry = _JKDictionaryHashTableEntryForKey((JKDictionary *)self, aKey);
11301135
if(entry != NULL) {
11311136
_JKDictionaryRemoveObjectWithEntry((JKDictionary *)self, entry);

0 commit comments

Comments
 (0)