Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -46,24 +46,25 @@ } - (void*)getMemWithSize: (size_t)size { struct __ofobject_allocated_mem *iter; + + if (size == 0) + return NULL; if ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) { [[OFNoMemException newWithObject: self andSize: sizeof(struct __ofobject_allocated_mem) ] raise]; - return NULL; } if ((iter->ptr = malloc(size)) == NULL) { free(iter); [[OFNoMemException newWithObject: self andSize: size] raise]; - return NULL; } iter->next = NULL; iter->prev = __mem_pool; @@ -77,10 +78,13 @@ - (void*)getMemForNItems: (size_t)nitems ofSize: (size_t)size { size_t memsize; + + if (nitems == 0 || size == 0) + return NULL; if (size > SIZE_MAX / nitems) [[OFOverflowException newWithObject: self] raise]; memsize = nitems * size; @@ -92,10 +96,15 @@ { struct __ofobject_allocated_mem *iter; if (ptr == NULL) return [self getMemWithSize: size]; + + if (size == 0) { + [self freeMem: ptr]; + return NULL; + } for (iter = __mem_pool; iter != NULL; iter = iter->prev) { if (iter->ptr == ptr) { if ((ptr = realloc(iter->ptr, size)) == NULL) [[OFNoMemException newWithObject: self @@ -114,10 +123,19 @@ - (void*)resizeMem: (void*)ptr toNItems: (size_t)nitems ofSize: (size_t)size { size_t memsize; + + if (ptr == NULL) + return [self getMemForNItems: nitems + ofSize: size]; + + if (nitems == 0 || size == 0) { + [self freeMem: ptr]; + return NULL; + } if (size > SIZE_MAX / nitems) [[OFOverflowException newWithObject: self] raise]; memsize = nitems * size; @@ -134,11 +152,11 @@ if (iter->prev != NULL) iter->prev->next = iter->next; if (iter->next != NULL) iter->next->prev = iter->prev; if (__mem_pool == iter) - __mem_pool = NULL; + __mem_pool = iter->prev; free(iter); free(ptr); return self; Index: tests/OFObject/OFObject.m ================================================================== --- tests/OFObject/OFObject.m +++ tests/OFObject/OFObject.m @@ -9,21 +9,20 @@ * the packaging of this file. */ #import #import -#import #import "OFObject.h" #import "OFExceptions.h" #define CATCH_EXCEPTION(code, exception) \ - caught = false; \ + caught = NO; \ @try { \ code; \ } @catch (exception *e) { \ - caught = true; \ + caught = YES; \ puts("CAUGHT! Error string was:"); \ fputs([e cString], stdout); \ puts("Resuming..."); \ } \ if (!caught) { \ @@ -33,11 +32,11 @@ int main() { OFObject *obj = [OFObject new]; - bool caught; + BOOL caught; void *p, *q, *r; /* Test freeing memory not allocated by obj */ puts("Freeing memory not allocated by object (should throw an " "exception)...");