From c1fc8d01d21fe47886bb36b8577adbea084d5900 Mon Sep 17 00:00:00 2001 From: Kris Kline Date: Tue, 12 Nov 2024 15:40:06 -0600 Subject: [PATCH 1/2] Address static analyzer warning in OCPartialMockObject * Issue #543 about potential nil passed to class_addMethod() OCPartialMockObject#setupForwarderForSelector * Modified to just return if there is no existing implementation for the method --- Source/OCMock/OCPartialMockObject.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/OCMock/OCPartialMockObject.m b/Source/OCMock/OCPartialMockObject.m index 6a7c53c3..b3019e03 100644 --- a/Source/OCMock/OCPartialMockObject.m +++ b/Source/OCMock/OCPartialMockObject.m @@ -206,6 +206,9 @@ - (void)setupForwarderForSelector:(SEL)sel Method originalMethod = class_getInstanceMethod(mockedClass, sel); /* Might be NULL if the selector is forwarded to another class */ IMP originalIMP = (originalMethod != NULL) ? method_getImplementation(originalMethod) : NULL; + if(originalIMP == NULL) + return; + const char *types = (originalMethod != NULL) ? method_getTypeEncoding(originalMethod) : NULL; // TODO: check the fallback implementation is actually sufficient if(types == NULL) From f77389535e8ac6da6069b7121f2510d213801bb1 Mon Sep 17 00:00:00 2001 From: Kris Kline Date: Tue, 3 Dec 2024 09:38:31 -0600 Subject: [PATCH 2/2] Change setupForwarderForSelector to still call "class_replaceMethod" if original implementation couldn't be found --- Source/OCMock/OCPartialMockObject.m | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Source/OCMock/OCPartialMockObject.m b/Source/OCMock/OCPartialMockObject.m index b3019e03..1d7891f1 100644 --- a/Source/OCMock/OCPartialMockObject.m +++ b/Source/OCMock/OCPartialMockObject.m @@ -206,9 +206,6 @@ - (void)setupForwarderForSelector:(SEL)sel Method originalMethod = class_getInstanceMethod(mockedClass, sel); /* Might be NULL if the selector is forwarded to another class */ IMP originalIMP = (originalMethod != NULL) ? method_getImplementation(originalMethod) : NULL; - if(originalIMP == NULL) - return; - const char *types = (originalMethod != NULL) ? method_getTypeEncoding(originalMethod) : NULL; // TODO: check the fallback implementation is actually sufficient if(types == NULL) @@ -217,7 +214,8 @@ - (void)setupForwarderForSelector:(SEL)sel Class subclass = object_getClass([self realObject]); IMP forwarderIMP = [mockedClass instanceMethodForwarderForSelector:sel]; class_replaceMethod(subclass, sel, forwarderIMP, types); - class_addMethod(subclass, aliasSelector, originalIMP, types); + if(originalIMP != NULL) + class_addMethod(subclass, aliasSelector, originalIMP, types); }