Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -171,10 +171,16 @@ return [super init]; } - initWithObject: (id)object { + if (object == nil) { + Class c = [self class]; + [self release]; + @throw [OFInvalidArgumentException exceptionWithClass: c]; + } + return [self initWithObjects: object, nil]; } - initWithObjects: (id)firstObject, ... { @@ -276,11 +282,16 @@ return [self objectAtIndex: index]; } - (size_t)indexOfObject: (id)object { - size_t i, count = [self count]; + size_t i, count; + + if (object == nil) + return OF_NOT_FOUND; + + count = [self count]; for (i = 0; i < count; i++) if ([[self objectAtIndex: i] isEqual: object]) return i; @@ -287,11 +298,16 @@ return OF_NOT_FOUND; } - (size_t)indexOfObjectIdenticalTo: (id)object { - size_t i, count = [self count]; + size_t i, count; + + if (object == nil) + return OF_NOT_FOUND; + + count = [self count]; for (i = 0; i < count; i++) if ([self objectAtIndex: i] == object) return i; @@ -365,12 +381,18 @@ usingSelector: (SEL)selector { void *pool; OFMutableString *ret; id *objects; - size_t i, count = [self count]; + size_t i, count; IMP append; + + if (separator == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + count = [self count]; if (count == 0) return @""; if (count == 1) return [[self firstObject] performSelector: selector]; @@ -608,11 +630,17 @@ } #endif - (OFArray*)arrayByAddingObject: (id)object { - OFMutableArray *ret = [[self mutableCopy] autorelease]; + OFMutableArray *ret; + + if (object == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + ret = [[self mutableCopy] autorelease]; [ret addObject: object]; [ret makeImmutable]; return ret; Index: src/OFArray_adjacent.m ================================================================== --- src/OFArray_adjacent.m +++ src/OFArray_adjacent.m @@ -50,10 +50,14 @@ - initWithObject: (id)object { self = [self init]; @try { + if (object == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + [array addItem: &object]; [object retain]; } @catch (id e) { [self release]; @throw e; @@ -90,10 +94,13 @@ id *objects; size_t i, count; self = [self init]; + if (array_ == nil) + return self; + @try { objects = [array_ objects]; count = [array_ count]; } @catch (id e) { [self release]; @@ -126,13 +133,22 @@ { self = [self init]; @try { size_t i; + BOOL ok = YES; - for (i = 0; i < count; i++) + for (i = 0; i < count; i++) { + if (objects[i] == nil) + ok = NO; + [objects[i] retain]; + } + + if (!ok) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; [array addItemsFromCArray: objects count: count]; } @catch (id e) { size_t i; @@ -220,12 +236,18 @@ buffer[i] = objects[range.location + i]; } - (size_t)indexOfObject: (id)object { - id *objects = [array cArray]; - size_t i, count = [array count]; + id *objects; + size_t i, count; + + if (object == nil) + return OF_NOT_FOUND; + + objects = [array cArray]; + count = [array count]; for (i = 0; i < count; i++) if ([objects[i] isEqual: object]) return i; @@ -232,12 +254,18 @@ return OF_NOT_FOUND; } - (size_t)indexOfObjectIdenticalTo: (id)object { - id *objects = [array cArray]; - size_t i, count = [array count]; + id *objects; + size_t i, count; + + if (object == nil) + return OF_NOT_FOUND; + + objects = [array cArray]; + count = [array count]; for (i = 0; i < count; i++) if (objects[i] == object) return i; Index: src/OFDictionary.m ================================================================== --- src/OFDictionary.m +++ src/OFDictionary.m @@ -22,10 +22,11 @@ #import "OFDictionary_hashtable.h" #import "OFArray.h" #import "OFString.h" #import "OFXMLElement.h" +#import "OFInvalidArgumentException.h" #import "OFNotImplementedException.h" #import "autorelease.h" static struct { @@ -200,10 +201,14 @@ } - initWithObject: (id)object forKey: (id)key { + if (key == nil || object == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + return [self initWithKeysAndObjects: key, object, nil]; } - initWithObjects: (OFArray*)objects forKeys: (OFArray*)keys @@ -314,14 +319,20 @@ return YES; } - (BOOL)containsObject: (id)object { - void *pool = objc_autoreleasePoolPush(); - OFEnumerator *enumerator = [self objectEnumerator]; + void *pool; + OFEnumerator *enumerator; id currentObject; + if (object == nil) + return NO; + + pool = objc_autoreleasePoolPush(); + enumerator = [self objectEnumerator]; + while ((currentObject = [enumerator nextObject]) != nil) { if ([currentObject isEqual: object]) { objc_autoreleasePoolPop(pool); return YES; } @@ -332,14 +343,20 @@ return NO; } - (BOOL)containsObjectIdenticalTo: (id)object { - void *pool = objc_autoreleasePoolPush(); - OFEnumerator *enumerator = [self objectEnumerator]; + void *pool; + OFEnumerator *enumerator; id currentObject; + if (object == nil) + return NO; + + pool = objc_autoreleasePoolPush(); + enumerator = [self objectEnumerator]; + while ((currentObject = [enumerator nextObject]) != nil) { if (currentObject == object) { objc_autoreleasePoolPop(pool); return YES; } Index: src/OFDictionary_hashtable.m ================================================================== --- src/OFDictionary_hashtable.m +++ src/OFDictionary_hashtable.m @@ -64,15 +64,10 @@ @try { uint32_t i; OFDictionary_hashtable *hashtable; - if (dictionary == nil) - @throw [OFInvalidArgumentException - exceptionWithClass: [self class] - selector: _cmd]; - if (![dictionary isKindOfClass: [OFDictionary_hashtable class]] && ![dictionary isKindOfClass: [OFMutableDictionary_hashtable class]]) @throw [OFInvalidArgumentException @@ -114,10 +109,13 @@ return self; } - initWithDictionary: (OFDictionary*)dictionary { + if (dictionary == nil) + return [self init]; + if ([dictionary class] == [OFDictionary_hashtable class] || [dictionary class] == [OFMutableDictionary_hashtable class]) return [self OF_initWithDictionary: dictionary copyKeys: YES]; @@ -127,10 +125,14 @@ void *pool; OFEnumerator *enumerator; id key; uint32_t i, newSize; + if (dictionary == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + count = [dictionary count]; if (count > UINT32_MAX) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; @@ -286,10 +288,14 @@ size = newSize; for (i = 0; i < count; i++) { uint32_t hash, last; + + if (keys[i] == nil || objects[i] == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; hash = [keys[i] hash]; last = size; for (j = hash & (size - 1); j < last && data[j] != NULL; @@ -622,11 +628,11 @@ - (BOOL)containsObject: (id)object { uint32_t i; - if (count == 0) + if (object == nil || count == 0) return NO; for (i = 0; i < size; i++) if (data[i] != NULL && data[i] != DELETED && [data[i]->object isEqual: object]) @@ -637,11 +643,11 @@ - (BOOL)containsObjectIdenticalTo: (id)object { uint32_t i; - if (count == 0) + if (object == nil || count == 0) return NO; for (i = 0; i < size; i++) if (data[i] != NULL && data[i] != DELETED && data[i]->object == object) Index: src/OFMutableArray.m ================================================================== --- src/OFMutableArray.m +++ src/OFMutableArray.m @@ -230,11 +230,17 @@ } - (void)replaceObject: (id)oldObject withObject: (id)newObject { - size_t i, count = [self count]; + size_t i, count; + + if (oldObject == nil || newObject == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + count = [self count]; for (i = 0; i < count; i++) { if ([[self objectAtIndex: i] isEqual: oldObject]) { [self replaceObjectAtIndex: i withObject: newObject]; @@ -244,11 +250,17 @@ } - (void)replaceObjectIdenticalTo: (id)oldObject withObject: (id)newObject { - size_t i, count = [self count]; + size_t i, count; + + if (oldObject == nil || newObject == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + count = [self count]; for (i = 0; i < count; i++) { if ([self objectAtIndex: i] == oldObject) { [self replaceObjectAtIndex: i withObject: newObject]; @@ -264,11 +276,17 @@ selector: _cmd]; } - (void)removeObject: (id)object { - size_t i, count = [self count]; + size_t i, count; + + if (object == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + count = [self count]; for (i = 0; i < count; i++) { if ([[self objectAtIndex: i] isEqual: object]) { [self removeObjectAtIndex: i]; @@ -277,11 +295,17 @@ } } - (void)removeObjectIdenticalTo: (id)object { - size_t i, count = [self count]; + size_t i, count; + + if (object == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + count = [self count]; for (i = 0; i < count; i++) { if ([self objectAtIndex: i] == object) { [self removeObjectAtIndex: i]; Index: src/OFMutableArray_adjacent.m ================================================================== --- src/OFMutableArray_adjacent.m +++ src/OFMutableArray_adjacent.m @@ -33,19 +33,27 @@ [self inheritMethodsFromClass: [OFArray_adjacent class]]; } - (void)addObject: (id)object { + if (object == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + [array addItem: &object]; [object retain]; mutations++; } - (void)insertObject: (id)object atIndex: (size_t)index { + if (object == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + [array insertItem: &object atIndex: index]; [object retain]; mutations++; @@ -68,12 +76,19 @@ } - (void)replaceObject: (id)oldObject withObject: (id)newObject { - id *objects = [array cArray]; - size_t i, count = [array count]; + id *objects; + size_t i, count; + + if (oldObject == nil || newObject == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + objects = [array cArray]; + count = [array count]; for (i = 0; i < count; i++) { if ([objects[i] isEqual: oldObject]) { [newObject retain]; [objects[i] release]; @@ -85,13 +100,19 @@ } - (void)replaceObjectAtIndex: (size_t)index withObject: (id)object { - id *objects = [array cArray]; + id *objects; id oldObject; + if (object == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + objects = [array cArray]; + if (index >= [array count]) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; oldObject = objects[index]; objects[index] = [object retain]; @@ -99,12 +120,19 @@ } - (void)replaceObjectIdenticalTo: (id)oldObject withObject: (id)newObject { - id *objects = [array cArray]; - size_t i, count = [array count]; + id *objects; + size_t i, count; + + if (oldObject == nil || newObject == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + objects = [array cArray]; + count = [array count]; for (i = 0; i < count; i++) { if (objects[i] == oldObject) { [newObject retain]; [objects[i] release]; @@ -115,12 +143,19 @@ } } - (void)removeObject: (id)object { - id *objects = [array cArray]; - size_t i, count = [array count]; + id *objects; + size_t i, count; + + if (object == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + objects = [array cArray]; + count = [array count]; for (i = 0; i < count; i++) { if ([objects[i] isEqual: object]) { object = objects[i]; @@ -134,12 +169,19 @@ } } - (void)removeObjectIdenticalTo: (id)object { - id *objects = [array cArray]; - size_t i, count = [array count]; + id *objects; + size_t i, count; + + if (object == nil) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class]]; + + objects = [array cArray]; + count = [array count]; for (i = 0; i < count; i++) { if (objects[i] == object) { [array removeItemAtIndex: i]; mutations++; Index: src/OFMutableSet.m ================================================================== --- src/OFMutableSet.m +++ src/OFMutableSet.m @@ -162,14 +162,16 @@ cArray = [self allocMemoryWithSize: sizeof(id) count: count]; @try { - OFEnumerator *enumerator = [self objectEnumerator]; + OFEnumerator *enumerator; id object; - size_t i = 0; + size_t i; + i = 0; + enumerator = [self objectEnumerator]; while ((object = [enumerator nextObject]) != nil) { assert(i < count); cArray[i++] = object; } @@ -184,13 +186,14 @@ } - (void)unionSet: (OFSet*)set { void *pool = objc_autoreleasePoolPush(); - OFEnumerator *enumerator = [set objectEnumerator]; + OFEnumerator *enumerator; id object; + enumerator = [set objectEnumerator]; while ((object = [enumerator nextObject]) != nil) [self addObject: object]; objc_autoreleasePoolPop(pool); } Index: src/OFSet.m ================================================================== --- src/OFSet.m +++ src/OFSet.m @@ -324,13 +324,14 @@ } - (BOOL)isSubsetOfSet: (OFSet*)set { void *pool = objc_autoreleasePoolPush(); - OFEnumerator *enumerator = [self objectEnumerator]; + OFEnumerator *enumerator; id object; + enumerator = [self objectEnumerator]; while ((object = [enumerator nextObject]) != nil) { if (![set containsObject: object]) { objc_autoreleasePoolPop(pool); return NO; } @@ -342,13 +343,14 @@ } - (BOOL)intersectsSet: (OFSet*)set { void *pool = objc_autoreleasePoolPush(); - OFEnumerator *enumerator = [self objectEnumerator]; + OFEnumerator *enumerator; id object; + enumerator = [self objectEnumerator]; while ((object = [enumerator nextObject]) != nil) { if ([set containsObject: object]) { objc_autoreleasePoolPop(pool); return YES; } Index: src/OFSet_hashtable.m ================================================================== --- src/OFSet_hashtable.m +++ src/OFSet_hashtable.m @@ -48,20 +48,26 @@ - initWithSet: (OFSet*)set { self = [self init]; + if (set == nil) + return self; + @try { void *pool = objc_autoreleasePoolPush(); - OFNumber *one = [OFNumber numberWithSize: 1]; - OFEnumerator *enumerator = [set objectEnumerator]; + OFNumber *one; + OFEnumerator *enumerator; id object; + + one = [OFNumber numberWithSize: 1]; /* * We can't just copy the dictionary as the specified set might * be a counted set, but we're just a normal set. */ + enumerator = [set objectEnumerator]; while ((object = [enumerator nextObject]) != nil) [dictionary OF_setObject: one forKey: object copyKey: NO]; @@ -76,19 +82,23 @@ - initWithArray: (OFArray*)array { self = [self init]; + if (array == nil) + return self; + @try { void *pool = objc_autoreleasePoolPush(); OFNumber *one = [OFNumber numberWithSize: 1]; - id *objects = [array objects]; - size_t i, count = [array count]; + OFEnumerator *enumerator; + id object; - for (i = 0; i < count; i++) + enumerator = [array objectEnumerator]; + while ((object = [enumerator nextObject]) != nil) [dictionary OF_setObject: one - forKey: objects[i] + forKey: object copyKey: NO]; objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @@ -202,10 +212,13 @@ return [dictionary count]; } - (BOOL)containsObject: (id)object { + if (object == nil) + return NO; + return ([dictionary objectForKey: object] != nil); } - (BOOL)isEqual: (id)object {