summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Schleifer <js@webkeks.org>2012-11-12 10:29:50 +0100
committerJonathan Schleifer <js@webkeks.org>2012-11-12 12:24:46 +0100
commit110e0a254544d2099dd747b466a0f0f5817fff1d (patch)
tree712bf3d6233e44fe0b0f8fd0930e44b6b3b7e65c
parent6c6828f0f47460894ba62374d3b280450ff05194 (diff)
Check more arguments for nil.
-rw-r--r--src/OFArray.m36
-rw-r--r--src/OFArray_adjacent.m38
-rw-r--r--src/OFDictionary.m25
-rw-r--r--src/OFDictionary_hashtable.m20
-rw-r--r--src/OFMutableArray.m32
-rw-r--r--src/OFMutableArray_adjacent.m60
-rw-r--r--src/OFMutableSet.m9
-rw-r--r--src/OFSet.m6
-rw-r--r--src/OFSet_hashtable.m25
9 files changed, 207 insertions, 44 deletions
diff --git a/src/OFArray.m b/src/OFArray.m
index 878fd6a7..fcfd68d0 100644
--- a/src/OFArray.m
+++ b/src/OFArray.m
@@ -173,6 +173,12 @@ static struct {
- initWithObject: (id)object
{
+ if (object == nil) {
+ Class c = [self class];
+ [self release];
+ @throw [OFInvalidArgumentException exceptionWithClass: c];
+ }
+
return [self initWithObjects: object, nil];
}
@@ -278,7 +284,12 @@ static struct {
- (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])
@@ -289,7 +300,12 @@ static struct {
- (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)
@@ -367,9 +383,15 @@ static struct {
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)
@@ -610,7 +632,13 @@ static struct {
- (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];
diff --git a/src/OFArray_adjacent.m b/src/OFArray_adjacent.m
index 74e0076a..5beb754e 100644
--- a/src/OFArray_adjacent.m
+++ b/src/OFArray_adjacent.m
@@ -52,6 +52,10 @@
self = [self init];
@try {
+ if (object == nil)
+ @throw [OFInvalidArgumentException
+ exceptionWithClass: [self class]];
+
[array addItem: &object];
[object retain];
} @catch (id e) {
@@ -92,6 +96,9 @@
self = [self init];
+ if (array_ == nil)
+ return self;
+
@try {
objects = [array_ objects];
count = [array_ count];
@@ -128,9 +135,18 @@
@try {
size_t i;
+ BOOL ok = YES;
+
+ for (i = 0; i < count; i++) {
+ if (objects[i] == nil)
+ ok = NO;
- for (i = 0; i < count; i++)
[objects[i] retain];
+ }
+
+ if (!ok)
+ @throw [OFInvalidArgumentException
+ exceptionWithClass: [self class]];
[array addItemsFromCArray: objects
count: count];
@@ -222,8 +238,14 @@
- (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])
@@ -234,8 +256,14 @@
- (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)
diff --git a/src/OFDictionary.m b/src/OFDictionary.m
index 12ef8826..3dfaa269 100644
--- a/src/OFDictionary.m
+++ b/src/OFDictionary.m
@@ -24,6 +24,7 @@
#import "OFString.h"
#import "OFXMLElement.h"
+#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "autorelease.h"
@@ -202,6 +203,10 @@ static struct {
- initWithObject: (id)object
forKey: (id)key
{
+ if (key == nil || object == nil)
+ @throw [OFInvalidArgumentException
+ exceptionWithClass: [self class]];
+
return [self initWithKeysAndObjects: key, object, nil];
}
@@ -316,10 +321,16 @@ static struct {
- (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);
@@ -334,10 +345,16 @@ static struct {
- (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);
diff --git a/src/OFDictionary_hashtable.m b/src/OFDictionary_hashtable.m
index 9c5206e0..5d90801d 100644
--- a/src/OFDictionary_hashtable.m
+++ b/src/OFDictionary_hashtable.m
@@ -66,11 +66,6 @@ struct of_dictionary_hashtable_bucket
uint32_t i;
OFDictionary_hashtable *hashtable;
- if (dictionary == nil)
- @throw [OFInvalidArgumentException
- exceptionWithClass: [self class]
- selector: _cmd];
-
if (![dictionary isKindOfClass:
[OFDictionary_hashtable class]] &&
![dictionary isKindOfClass:
@@ -116,6 +111,9 @@ struct of_dictionary_hashtable_bucket
- 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
@@ -129,6 +127,10 @@ struct of_dictionary_hashtable_bucket
id key;
uint32_t i, newSize;
+ if (dictionary == nil)
+ @throw [OFInvalidArgumentException
+ exceptionWithClass: [self class]];
+
count = [dictionary count];
if (count > UINT32_MAX)
@@ -289,6 +291,10 @@ struct of_dictionary_hashtable_bucket
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;
@@ -624,7 +630,7 @@ struct of_dictionary_hashtable_bucket
{
uint32_t i;
- if (count == 0)
+ if (object == nil || count == 0)
return NO;
for (i = 0; i < size; i++)
@@ -639,7 +645,7 @@ struct of_dictionary_hashtable_bucket
{
uint32_t i;
- if (count == 0)
+ if (object == nil || count == 0)
return NO;
for (i = 0; i < size; i++)
diff --git a/src/OFMutableArray.m b/src/OFMutableArray.m
index 399f84e2..a88cba2b 100644
--- a/src/OFMutableArray.m
+++ b/src/OFMutableArray.m
@@ -232,7 +232,13 @@ quicksort(OFMutableArray *array, size_t left, size_t right)
- (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]) {
@@ -246,7 +252,13 @@ quicksort(OFMutableArray *array, size_t left, size_t right)
- (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) {
@@ -266,7 +278,13 @@ quicksort(OFMutableArray *array, size_t left, size_t right)
- (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]) {
@@ -279,7 +297,13 @@ quicksort(OFMutableArray *array, size_t left, size_t right)
- (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) {
diff --git a/src/OFMutableArray_adjacent.m b/src/OFMutableArray_adjacent.m
index 0a4be8e0..2a9d95b8 100644
--- a/src/OFMutableArray_adjacent.m
+++ b/src/OFMutableArray_adjacent.m
@@ -35,6 +35,10 @@
- (void)addObject: (id)object
{
+ if (object == nil)
+ @throw [OFInvalidArgumentException
+ exceptionWithClass: [self class]];
+
[array addItem: &object];
[object retain];
@@ -44,6 +48,10 @@
- (void)insertObject: (id)object
atIndex: (size_t)index
{
+ if (object == nil)
+ @throw [OFInvalidArgumentException
+ exceptionWithClass: [self class]];
+
[array insertItem: &object
atIndex: index];
[object retain];
@@ -70,8 +78,15 @@
- (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]) {
@@ -87,9 +102,15 @@
- (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]];
@@ -101,8 +122,15 @@
- (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) {
@@ -117,8 +145,15 @@
- (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]) {
@@ -136,8 +171,15 @@
- (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) {
diff --git a/src/OFMutableSet.m b/src/OFMutableSet.m
index d2c4d476..5a1f4036 100644
--- a/src/OFMutableSet.m
+++ b/src/OFMutableSet.m
@@ -164,10 +164,12 @@ static struct {
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;
@@ -186,9 +188,10 @@ static struct {
- (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];
diff --git a/src/OFSet.m b/src/OFSet.m
index f456fe4c..97539e18 100644
--- a/src/OFSet.m
+++ b/src/OFSet.m
@@ -326,9 +326,10 @@ static struct {
- (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);
@@ -344,9 +345,10 @@ static struct {
- (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);
diff --git a/src/OFSet_hashtable.m b/src/OFSet_hashtable.m
index 670827a9..eee505b9 100644
--- a/src/OFSet_hashtable.m
+++ b/src/OFSet_hashtable.m
@@ -50,16 +50,22 @@
{
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
@@ -78,15 +84,19 @@
{
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);
@@ -204,6 +214,9 @@
- (BOOL)containsObject: (id)object
{
+ if (object == nil)
+ return NO;
+
return ([dictionary objectForKey: object] != nil);
}