Permalink
Browse files

making sure method encodings match before swizzling

  • Loading branch information...
1 parent 2cfea79 commit 29f4868007afef86a42c6d06ca751061bf49ea68 @Machx committed Oct 28, 2012
Showing with 19 additions and 0 deletions.
  1. +1 −0 Source/CWRuntimeUtilities.h
  2. +18 −0 Source/CWRuntimeUtilities.m
View
1 Source/CWRuntimeUtilities.h
@@ -35,6 +35,7 @@ static const NSInteger kCWErrorNoOriginalInstanceMethod = 201;
static const NSInteger kCWErrorNoNewInstanceMethod = 202;
static const NSInteger kCWErrorNoOriginalClassMethod = 203;
static const NSInteger kCWErrorNoNewClassMethod = 204;
+static const NSInteger kCWErrorNonMatchingMethodEncodings = 205;
//so we don't have to include <objc/runtime.h> here in this header
typedef struct objc_method *Method;
View
18 Source/CWRuntimeUtilities.m
@@ -53,6 +53,15 @@ void CWSwizzleInstanceMethods(Class instanceClass, SEL originalSel, SEL newSel,
return;
}
+ const char *method1_encoding = method_getTypeEncoding(originalMethod);
+ const char *method2_encoding = method_getTypeEncoding(newMethod);
+ if ((!strcmp(method1_encoding, method2_encoding)) && *error) {
+ *error = CWCreateError(kCWRuntimeErrorDomain, kCWErrorNonMatchingMethodEncodings,
+ [NSString stringWithFormat:@"Method Encodings don't match: %s != %s",
+ method1_encoding,method2_encoding]);
+ return;
+ }
+
if (class_addMethod(instanceClass,
originalSel,
method_getImplementation(newMethod),
@@ -87,6 +96,15 @@ void CWSwizzleClassMethods(Class methodClass, SEL originalSel, SEL newSel, NSErr
}
}
+ const char *method1_encoding = method_getTypeEncoding(originalMethod);
+ const char *method2_encoding = method_getTypeEncoding(newMethod);
+ if ((!strcmp(method1_encoding, method2_encoding)) && *error) {
+ *error = CWCreateError(kCWRuntimeErrorDomain, kCWErrorNonMatchingMethodEncodings,
+ [NSString stringWithFormat:@"Method Encodings don't match: %s != %s",
+ method1_encoding,method2_encoding]);
+ return;
+ }
+
if (class_addMethod(methodClass,
originalSel,
method_getImplementation(newMethod),

0 comments on commit 29f4868

Please sign in to comment.