From 5b151c5fa03c1c75e961f355e17b5d37f2e4c7a6 Mon Sep 17 00:00:00 2001 From: Levin Li Date: Wed, 17 Nov 2021 21:35:49 +0800 Subject: [PATCH] Do not call handler blocks if they are nil --- Headers/GNUstepBase/GSBlocks.h | 15 +++++++++------ Source/GSSorting.h | 2 +- Source/NSArray.m | 10 +++++----- Source/NSData.m | 8 ++++---- Source/NSDictionary.m | 2 +- Source/NSFileManager.m | 2 +- Source/NSOperation.m | 8 ++------ Source/NSOrderedSet.m | 10 +++++----- Source/NSPredicate.m | 2 +- Source/NSSet.m | 2 +- Source/NSSortDescriptor.m | 2 +- Source/NSTimer.m | 2 +- 12 files changed, 32 insertions(+), 33 deletions(-) diff --git a/Headers/GNUstepBase/GSBlocks.h b/Headers/GNUstepBase/GSBlocks.h index 3295d4d56..ea0a20343 100644 --- a/Headers/GNUstepBase/GSBlocks.h +++ b/Headers/GNUstepBase/GSBlocks.h @@ -48,12 +48,12 @@ typedef retTy(^name)() /** * Calls a block. Works irrespective of whether the compiler supports blocks. */ -#define CALL_BLOCK(block, args, ...) block(args, ## __VA_ARGS__) +#define CALL_NON_NULL_BLOCK(block, args, ...) block(args, ## __VA_ARGS__) /** * Calls a block without arguments. */ -#define CALL_BLOCK_NO_ARGS(block) block() +#define CALL_NON_NULL_BLOCK_NO_ARGS(block) block() #else /* Fall-back versions for when the compiler doesn't have native blocks support. @@ -76,9 +76,9 @@ typedef retTy(^name)() retTy (*invoke)(void*);\ } *name -#define CALL_BLOCK(block, args, ...) block->invoke(block, args, ## __VA_ARGS__) +#define CALL_NON_NULL_BLOCK(block, args, ...) block->invoke(block, args, ## __VA_ARGS__) -#define CALL_BLOCK_NO_ARGS(block) block->invoke(block) +#define CALL_NON_NULL_BLOCK_NO_ARGS(block) block->invoke(block) #define BLOCK_SCOPE #else /* GCC_VERSION >= 3000 */ @@ -100,13 +100,16 @@ typedef retTy(^name)() } *name -#define CALL_BLOCK(block, args...) block->invoke(block, args) -#define CALL_BLOCK_NO_ARGS(block) block->invoke(block) +#define CALL_NON_NULL_BLOCK(block, args...) block->invoke(block, args) +#define CALL_NON_NULL_BLOCK_NO_ARGS(block) block->invoke(block) #define BLOCK_SCOPE #endif /* GCC_VERSION >= 3000 */ #endif /* __has_feature(blocks) */ +#define CALL_BLOCK(block, args...) ((NULL != block) ? CALL_NON_NULL_BLOCK(block, args) : nil) +#define CALL_BLOCK_NO_ARGS(block) ((NULL != block) ? CALL_NON_NULL_BLOCK_NO_ARGS(block) : nil) + #if __has_include() # include #else diff --git a/Source/GSSorting.h b/Source/GSSorting.h index e3d0b0368..eb90ab184 100644 --- a/Source/GSSorting.h +++ b/Source/GSSorting.h @@ -148,7 +148,7 @@ GSCompareUsingDescriptorOrComparator(id first, id second, id descOrComp, return [(NSSortDescriptor*)descOrComp compareObject: first toObject: second]; case GSComparisonTypeComparatorBlock: - return CALL_BLOCK(((NSComparator)descOrComp), first, second); + return CALL_NON_NULL_BLOCK(((NSComparator)descOrComp), first, second); case GSComparisonTypeFunction: return ((NSInteger (*)(id, id, void *))descOrComp)(first, diff --git a/Source/NSArray.m b/Source/NSArray.m index adc2e7b46..18b2bb1c2 100644 --- a/Source/NSArray.m +++ b/Source/NSArray.m @@ -1146,7 +1146,7 @@ compare(id elem1, id elem2, void* context) } if (range.length == 1) { - switch (CALL_BLOCK(comparator, key, [self objectAtIndex: range.location])) + switch (CALL_NON_NULL_BLOCK(comparator, key, [self objectAtIndex: range.location])) { case NSOrderedSame: return range.location; @@ -1212,7 +1212,7 @@ compare(id elem1, id elem2, void* context) * For a search from the left, we'd have the correct index anyways. Check * whether it's equal to the key and return NSNotFound otherwise */ - return (NSOrderedSame == CALL_BLOCK(comparator, + return (NSOrderedSame == CALL_NON_NULL_BLOCK(comparator, key, [self objectAtIndex: index]) ? index : NSNotFound); } // Never reached @@ -1858,10 +1858,10 @@ compare(id elem1, id elem2, void* context) } else // call block directly # endif - if (CALL_BLOCK(predicate, obj, count, &shouldStop)) + if (CALL_NON_NULL_BLOCK(predicate, obj, count, &shouldStop)) { /* TODO: It would be more efficient to collect an NSRange and only - * pass it to the index set when CALL_BLOCK returned NO. */ + * pass it to the index set when CALL_NON_NULL_BLOCK returned NO. */ [set addIndex: count]; } if (shouldStop) @@ -1959,7 +1959,7 @@ compare(id elem1, id elem2, void* context) } else // call block directly # endif - if (CALL_BLOCK(predicate, obj, count, &shouldStop)) + if (CALL_NON_NULL_BLOCK(predicate, obj, count, &shouldStop)) { index = count; shouldStop = YES; diff --git a/Source/NSData.m b/Source/NSData.m index c5bf2017f..ac0137238 100644 --- a/Source/NSData.m +++ b/Source/NSData.m @@ -3579,7 +3579,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) { if (deallocator != NULL) { - CALL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, length); + CALL_NON_NULL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, length); DESTROY(deallocator); } // Clear out the ivars so that super doesn't double free. @@ -4442,7 +4442,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) { if (deallocator != NULL) { - CALL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity); + CALL_NON_NULL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity); // Clear out the ivars so that super doesn't double free. bytes = NULL; length = 0; @@ -4472,7 +4472,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) memcpy(tmp, bytes, capacity < size ? capacity : size); if (deallocator != NULL) { - CALL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity); + CALL_NON_NULL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity); DESTROY(deallocator); zone = NSDefaultMallocZone(); } @@ -4483,7 +4483,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) } else if (deallocator != NULL) { - CALL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity); + CALL_NON_NULL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity); DESTROY(deallocator); zone = NSDefaultMallocZone(); } diff --git a/Source/NSDictionary.m b/Source/NSDictionary.m index ffbba59a9..be18517ce 100644 --- a/Source/NSDictionary.m +++ b/Source/NSDictionary.m @@ -1071,7 +1071,7 @@ compareIt(id o1, id o2, void* context) } else // call block directly #endif - if (CALL_BLOCK(aPredicate, key, obj, &shouldStop)) + if (CALL_NON_NULL_BLOCK(aPredicate, key, obj, &shouldStop)) { addObject(buildSet, addObjectSelector, key); } diff --git a/Source/NSFileManager.m b/Source/NSFileManager.m index 30591ec49..453c1cf4d 100644 --- a/Source/NSFileManager.m +++ b/Source/NSFileManager.m @@ -2900,7 +2900,7 @@ static inline void gsedRelease(GSEnumeratedDirectory X) _currentFilePath, [NSError _last]); if (_errorHandler != NULL) { - flag = CALL_BLOCK(_errorHandler, + flag = CALL_NON_NULL_BLOCK(_errorHandler, [NSURL fileURLWithPath: _currentFilePath], [NSError _last]); } diff --git a/Source/NSOperation.m b/Source/NSOperation.m index 68902032e..1182ff71c 100644 --- a/Source/NSOperation.m +++ b/Source/NSOperation.m @@ -526,11 +526,7 @@ static NSArray *empty = nil; internal->finished = YES; [self didChangeValueForKey: @"isFinished"]; } - if (NULL != internal->completionBlock) - { - CALL_BLOCK_NO_ARGS( - ((GSOperationCompletionBlock)internal->completionBlock)); - } + CALL_BLOCK_NO_ARGS(((GSOperationCompletionBlock)internal->completionBlock)); } [internal->lock unlock]; } @@ -584,7 +580,7 @@ static NSArray *empty = nil; while ((theBlock = (GSBlockOperationBlock)[en nextObject]) != NULL) { - CALL_BLOCK_NO_ARGS(theBlock); + CALL_NON_NULL_BLOCK_NO_ARGS(theBlock); } } @end diff --git a/Source/NSOrderedSet.m b/Source/NSOrderedSet.m index e2e92c539..acc9ae264 100644 --- a/Source/NSOrderedSet.m +++ b/Source/NSOrderedSet.m @@ -751,7 +751,7 @@ static SEL remSel; } if (range.length == 1) { - switch (CALL_BLOCK(comparator, key, [self objectAtIndex: range.location])) + switch (CALL_NON_NULL_BLOCK(comparator, key, [self objectAtIndex: range.location])) { case NSOrderedSame: return range.location; @@ -818,7 +818,7 @@ static SEL remSel; * For a search from the left, we'd have the correct index anyways. Check * whether it's equal to the key and return NSNotFound otherwise */ - return (NSOrderedSame == CALL_BLOCK(comparator, + return (NSOrderedSame == CALL_NON_NULL_BLOCK(comparator, key, [self objectAtIndex: index]) ? index : NSNotFound); } // Never reached @@ -886,7 +886,7 @@ static SEL remSel; } else // call block directly # endif - if (CALL_BLOCK(predicate, obj, count, &shouldStop)) + if (CALL_NON_NULL_BLOCK(predicate, obj, count, &shouldStop)) { index = count; shouldStop = YES; @@ -959,10 +959,10 @@ static SEL remSel; } else // call block directly # endif - if (CALL_BLOCK(predicate, obj, count, &shouldStop)) + if (CALL_NON_NULL_BLOCK(predicate, obj, count, &shouldStop)) { /* TODO: It would be more efficient to collect an NSRange and only - * pass it to the index set when CALL_BLOCK returned NO. */ + * pass it to the index set when CALL_NON_NULL_BLOCK returned NO. */ [set addIndex: count]; } if (shouldStop) diff --git a/Source/NSPredicate.m b/Source/NSPredicate.m index 36d849c64..6a46e2446 100644 --- a/Source/NSPredicate.m +++ b/Source/NSPredicate.m @@ -2718,7 +2718,7 @@ GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringCompareOption substitutionVariables: (GS_GENERIC_CLASS(NSDictionary, NSString*,id)*)variables { - return CALL_BLOCK(_block, object, variables); + return CALL_NON_NULL_BLOCK(_block, object, variables); } - (BOOL) evaluateWithObject: (id)object diff --git a/Source/NSSet.m b/Source/NSSet.m index 93a9cfa5d..aae1b8e90 100644 --- a/Source/NSSet.m +++ b/Source/NSSet.m @@ -933,7 +933,7 @@ static Class NSMutableSet_concrete_class; FOR_IN (id, obj, enumerator) { - BOOL include = CALL_BLOCK(aBlock, obj, &shouldStop); + BOOL include = CALL_NON_NULL_BLOCK(aBlock, obj, &shouldStop); if (include) { diff --git a/Source/NSSortDescriptor.m b/Source/NSSortDescriptor.m index 64b989b18..80728d95c 100644 --- a/Source/NSSortDescriptor.m +++ b/Source/NSSortDescriptor.m @@ -120,7 +120,7 @@ static BOOL initialized = NO; } else { - result = CALL_BLOCK(((NSComparator)_comparator), comparedKey1, comparedKey2); + result = CALL_NON_NULL_BLOCK(((NSComparator)_comparator), comparedKey1, comparedKey2); } if (_ascending == NO) diff --git a/Source/NSTimer.m b/Source/NSTimer.m index 0a31aeeae..82dd279f3 100644 --- a/Source/NSTimer.m +++ b/Source/NSTimer.m @@ -290,7 +290,7 @@ static Class NSDate_class; { if ((id)_block != nil) { - CALL_BLOCK(_block, self); + CALL_NON_NULL_BLOCK(_block, self); } else {