ZKSwizzle icon indicating copy to clipboard operation
ZKSwizzle copied to clipboard

Swizzling respondsToSelector: leads to infinite recursion do to description: calls in

Open adamf opened this issue 8 years ago • 0 comments

If ZKSwizzle is used to swizzle NSObject's respondsToSelector: infinite recursion occurs leading to a crash. This is because destinationSelectorForSelector: uses the NSString format specifier %@ which calls an object's description: which uses respondsToSelector:, which then repeats itself forever since ZKOriginalImplementation also calls destinationSelectorForSelector:. And so on.

Patch below; passes tests. The fix used is to convert the NSString representations of the class and selector to UTF8String (eg, to C strings) and then use those C strings to create the original selector string.

diff --git a/ZKSwizzle/ZKSwizzle.m b/ZKSwizzle/ZKSwizzle.m
index 25a3577..90b2dc3 100644
--- a/ZKSwizzle/ZKSwizzle.m
+++ b/ZKSwizzle/ZKSwizzle.m
@@ -20,7 +20,9 @@ + (BOOL)_ZK_ignoreTypes;
 }

 static SEL destinationSelectorForSelector(SEL cmd, Class dst) {
-    return NSSelectorFromString([@"_ZK_old_" stringByAppendingFormat:@"%s_%@", class_getName(dst), NSStringFromSelector(cmd)]);
+    const char *selName = [NSStringFromSelector(cmd) UTF8String];
+    const char *className = [NSStringFromClass(dst) UTF8String];
+    return NSSelectorFromString([@"_ZK_old_" stringByAppendingFormat:@"%s_%s", className, selName]);
 }

 static Class classFromInfo(const char *info) {

adamf avatar Nov 11 '16 18:11 adamf