iOS 降低 NSArray Crash 风险

在日常开发中,会存在以下应用场景

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *string = nil;
        NSArray *array = @[string];
        
        NSLog(@"%@", array);
    }
    return 0;
}

毫无疑问,这样的代码跑起来,会直接crash,那么我们有没有办法通过代码的形式,让这种场景进行可执行又不crash呢?答案是有的,可以通过runtime的方式,将我们自己的方法跟系统方法进行互换,从而达到我们所要的效果。那么问题又来了,怎么知道@[]这个是调用的哪个方法呢?我们可以用Clang将OC代码转成C++代码。
Clang之后代码如下

int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        NSString *string = __null;
        NSArray *array = ((NSArray *(*)(Class, SEL, ObjectType  _Nonnull const * _Nonnull, NSUInteger))(void *)objc_msgSend)(objc_getClass("NSArray"), sel_registerName("arrayWithObjects:count:"), (const id *)__NSContainer_literal(1U, string).arr, 1U);

        NSLog((NSString *)&__NSConstantStringImpl__var_folders_8f_y3mvcz2d4_s228xbxgg4ky0h0000gp_T_main_1c0812_mi_0, array);
    }
    return 0;
}

经过clang,可以发现@[]这种方式创建的数组是通过发送消息给NSArray执行arrayWithObjects:count:这个方法来创建的数组。

方法拿到了,下面就通过创建分类+运行时的方式,将系统方法和自定义方法进行互换。

新建一个NSArrayCategory文件

导入 #import

在实现文件中,重写 load 方法,将系统方法和自定义方法进行替换

+ (void)load {
    
    Method system_arrayWithObjectsCountMethod = class_getClassMethod(self, @selector(arrayWithObjects:count:));
    Method my_arrayWithObjectsCountMethod = class_getClassMethod(self, @selector(yxc_arrayWithObjects:count:));
    method_exchangeImplementations(system_arrayWithObjectsCountMethod, my_arrayWithObjectsCountMethod);
}

自定义 yxc_arrayWithObjects:count: 方法

/**
 @[] 字面量初始化调用方法

 @param objects 对象
 @param cnt 数组个数
 @return 数组
 */
+ (instancetype)yxc_arrayWithObjects:(id  _Nonnull const [])objects count:(NSUInteger)cnt {
    
    NSMutableArray *objectArray = [NSMutableArray array];
    
    for (int i = 0; i < cnt; i++) {
        id object = objects[i];
        if (object && ![object isKindOfClass:[NSNull class]]) {
            [objectArray addObject:object];
        }
    }
    
    return [NSArray arrayWithArray:objectArray];
}

编译代码,发现这时候main函数中同样的代码不会再崩溃了,这样我们通过 Category + runtime 的方式达到了降低crash的风险。

接下来,我们来看另外一种情况

main函数代码改成如下:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSArray *array = @[@1, @2, @3];
        NSLog(@"%@", array[7]);
    }
    return 0;
}

array数组只有三个元素,这边输出第八个元素,数组已经越界了,实际上在开发中,经常出现数组越界访问的情况,那么我们又该怎么去降低crash的风险呢?
如果按照刚才的形式,先进行 clang ,然后再通过 category + runtime进行转换

clang

int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        NSArray *array = ((NSArray *(*)(Class, SEL, ObjectType  _Nonnull const * _Nonnull, NSUInteger))(void *)objc_msgSend)(objc_getClass("NSArray"), sel_registerName("arrayWithObjects:count:"), (const id *)__NSContainer_literal(3U, ((NSNumber *(*)(Class, SEL, int))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithInt:"), 1), ((NSNumber *(*)(Class, SEL, int))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithInt:"), 2), ((NSNumber *(*)(Class, SEL, int))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithInt:"), 3)).arr, 3U);
        NSLog((NSString *)&__NSConstantStringImpl__var_folders_8f_y3mvcz2d4_s228xbxgg4ky0h0000gp_T_main_e7346f_mi_0, ((id (*)(id, SEL, NSUInteger))(void *)objc_msgSend)((id)array, sel_registerName("objectAtIndexedSubscript:"), (NSUInteger)7));
    }
    return 0;
}

替换 objectAtIndexedSubscript: 系统方法

Method system_objectAtIndexedSubscriptMethod = class_getInstanceMethod(self, @selector(objectAtIndexedSubscript:));
Method my_objectAtIndexedSubscriptMethod = class_getInstanceMethod(self, @selector(yxc_objectAtIndexedSubscript:));
method_exchangeImplementations(system_objectAtIndexedSubscriptMethod, my_objectAtIndexedSubscriptMethod);

yxc_objectAtIndexedSubscript方法

/**
 @[] 形式获取数组对象

 @param idx 数组下标
 */
- (id)yxc_objectAtIndexedSubscript:(NSUInteger)idx {
    
    if (idx >= self.count) return nil;
    
    return [self objectAtIndex:idx];
}

然后编译运行,发现还是崩溃,同样的方式,为什么不同的结果?
这时候我们需要跳入Fundation查看 yxc_objectAtIndexedSubscript 这个方法,发现这个方法并不是 NSArray 原有的方法,是 NSArray一个名为NSExtendedArray的分类方法,所以才导致我们这种方式无效。这时候,我们可以查看崩溃信息.

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndexedSubscript:]: index 7 beyond bounds [0 .. 2]'

这时候发现,系统提示的是 __NSArrayIobjectAtIndexedSubscript: 方法崩溃。此时提示的是 __NSArrayI 而不是 NSArray,所以这时候我们把load方法替换改成

Method systemMethod1 = class_getInstanceMethod(NSClassFromString(@"__NSArrayI"), @selector(objectAtIndexedSubscript:));
    Method my_objectAtIndexedSubscriptMethod = class_getInstanceMethod(self, @selector(yxc_objectAtIndexedSubscript:));
    method_exchangeImplementations(system_objectAtIndexedSubscriptMethod, my_objectAtIndexedSubscriptMethod);

这时候我们重新编译运行,这时候程序没有在crash了。

最后附上,替换系统一些方法的代码:

+ (void)load {
    
    Method system_arrayWithObjectsCountMethod = class_getClassMethod(self, @selector(arrayWithObjects:count:));
    Method my_arrayWithObjectsCountMethod = class_getClassMethod(self, @selector(yxc_arrayWithObjects:count:));
    method_exchangeImplementations(system_arrayWithObjectsCountMethod, my_arrayWithObjectsCountMethod);
    
    Method system_objectAtIndexedSubscriptMethod = class_getInstanceMethod(NSClassFromString(@"__NSArrayI"), @selector(objectAtIndexedSubscript:));
    Method my_objectAtIndexedSubscriptMethod = class_getInstanceMethod(self, @selector(yxc_objectAtIndexedSubscript:));
    method_exchangeImplementations(system_objectAtIndexedSubscriptMethod, my_objectAtIndexedSubscriptMethod);

    Method system_objectAtIndexMethod = class_getInstanceMethod(NSClassFromString(@"__NSArrayI"), @selector(objectAtIndex:));
    Method my_objectAtIndexMethod = class_getInstanceMethod(self, @selector(yxc_objectAtIndex:));
    method_exchangeImplementations(system_objectAtIndexMethod, my_objectAtIndexMethod);
    
    Method system_addObjectMethod = class_getInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(addObject:));
    Method my_addObjectMethod = class_getInstanceMethod(self, @selector(yxc_addObject:));
    method_exchangeImplementations(system_addObjectMethod, my_addObjectMethod);
    
    Method system_insertObjectAtIndexMethod = class_getInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(insertObject:atIndex:));
    Method my_insertObjectAtIndexMethod = class_getInstanceMethod(self, @selector(yxc_insertObject:atIndex:));
    method_exchangeImplementations(system_insertObjectAtIndexMethod, my_insertObjectAtIndexMethod);
    
    Method system_removeObjectAtIndexMethod = class_getInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(removeObjectAtIndex:));
    Method my_removeObjectAtIndexMethod = class_getInstanceMethod(self, @selector(yxc_removeObjectAtIndex:));
    method_exchangeImplementations(system_removeObjectAtIndexMethod, my_removeObjectAtIndexMethod);

    // 此处是可变数组的取值方法替换
    Method system_objectAtIndexedSubscriptMethod1 = class_getInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(objectAtIndexedSubscript:));
    Method my_objectAtIndexedSubscriptMethod1 = class_getInstanceMethod(self, @selector(yxc_objectAtIndexedSubscript1:));
    method_exchangeImplementations(system_objectAtIndexedSubscriptMethod1, my_objectAtIndexedSubscriptMethod1);
}

/**
 @[] 字面量初始化调用方法

 @param objects 对象
 @param cnt 数组个数
 @return 数组
 */
+ (instancetype)yxc_arrayWithObjects:(id  _Nonnull const [])objects count:(NSUInteger)cnt {
    
    NSMutableArray *objectArray = [NSMutableArray array];
    
    for (int i = 0; i < cnt; i++) {
        id object = objects[i];
        if (object && ![object isKindOfClass:[NSNull class]]) {
            [objectArray addObject:object];
        }
    }
    
    return [NSArray arrayWithArray:objectArray];
}

/**
 数组添加一个对象
 */
- (void)yxc_addObject:(id)anObject {
    
    if (!anObject) return;
    [self yxc_addObject:anObject];
}

/**
 数组插入一个对象

 @param anObject 对象
 @param index 待插入的下标
 */
- (void)yxc_insertObject:(id)anObject atIndex:(NSUInteger)index {
    
    if (!anObject) return;
    if (index > self.count) return; // 数组可以插入下标为0这个位置,如果此处 >= 会有问题
    
    [self yxc_insertObject:anObject atIndex:index];
}

/**
 根据下标移除某个对象

 @param index 需要移除的下标
 */
- (void)yxc_removeObjectAtIndex:(NSUInteger)index {
    
    if (index >= self.count) return;
    
    [self yxc_removeObjectAtIndex:index];
}

/**
 通过 index 获取对象

 @param index 数组下标
 */
- (id)yxc_objectAtIndex:(NSUInteger)index {
    
    if (index >= self.count) return nil;
    
    return [self yxc_objectAtIndex:index];
}

/**
 @[] 形式获取数组对象

 @param idx 数组下标
 */
- (id)yxc_objectAtIndexedSubscript:(NSUInteger)idx {
    
    if (idx >= self.count) return nil;
    
    return [self objectAtIndex:idx];
}

/**
 @[] 形式获取数组对象
 
 @param idx 数组下标
 */
- (id)yxc_objectAtIndexedSubscript1:(NSUInteger)idx {
    
    if (idx >= self.count) return nil;
    
    return [self objectAtIndex:idx];
}

你可能感兴趣的:(iOS 降低 NSArray Crash 风险)