美文网首页
防止数组越界Crash示例

防止数组越界Crash示例

作者: 小米咸鱼 | 来源:发表于2021-11-10 20:58 被阅读0次
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSArray (PreventCrash)

@end

@interface NSMutableArray (PreventCrash)

@end

NS_ASSUME_NONNULL_END
#import "NSArray+PreventCrash.h"
#import <objc/runtime.h>

@implementation NSArray (PreventCrash)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //处理数组越界crash
        NSArray* classNameArray = @[@"__NSArrayI",@"__NSArrayM",@"__NSSingleObjectArrayI"];
        [classNameArray enumerateObjectsUsingBlock:^(NSString*  className, NSUInteger idx, BOOL * _Nonnull stop) {
            Method objectAtIndexedSubscript = class_getInstanceMethod(objc_getClass(className.UTF8String), @selector(objectAtIndexedSubscript:));
            
            NSString* swizzingSelName = [className stringByAppendingString:@"_swizzing_objectAtIndexedSubscript:"];
            SEL swizzingSEL = NSSelectorFromString(swizzingSelName);
            Method swizzingObjectAtIndexedSubscript = class_getInstanceMethod(self, swizzingSEL);
            
            
            BOOL addSuccess = class_addMethod(self, @selector(objectAtIndexedSubscript:), method_getImplementation(swizzingObjectAtIndexedSubscript), method_getTypeEncoding(swizzingObjectAtIndexedSubscript));
            if (addSuccess) {
                class_replaceMethod(self, swizzingSEL, method_getImplementation(objectAtIndexedSubscript), method_getTypeEncoding(objectAtIndexedSubscript));
            }else{
                method_exchangeImplementations(objectAtIndexedSubscript, swizzingObjectAtIndexedSubscript);
            }
        }];
        
    });
}

#pragma mark - 数组越界方法
- (id)__NSArrayI_swizzing_objectAtIndexedSubscript:(NSUInteger)idx{
    if (idx < self.count) {
        return self[idx];
    }
    return nil;
}

- (id)__NSArrayM_swizzing_objectAtIndexedSubscript:(NSUInteger)idx{
    if (idx < self.count) {
        return self[idx];
    }
    return nil;
}

- (id)__NSSingleObjectArrayI_swizzing_objectAtIndexedSubscript:(NSUInteger)idx{
    if (idx < self.count) {
        return self[idx];
    }
    return nil;
}

@end


@implementation NSMutableArray (PreventCrash)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //处理数组add nil
        Class mutableArrayClass = objc_getClass(@"__NSArrayM".UTF8String);
        SEL mutableInsertSEL = @selector(insertObject:atIndex:);
        SEL swizzingMutableInsertSEL = @selector(__NSArrayM_insertObject:atIndex:);

        Method mutableInsertMethod = class_getInstanceMethod(mutableArrayClass, mutableInsertSEL);
        Method swizzingMutableInsertMethod = class_getInstanceMethod(self, swizzingMutableInsertSEL);

        BOOL success = class_addMethod(mutableArrayClass, mutableInsertSEL, method_getImplementation(swizzingMutableInsertMethod), method_getTypeEncoding(swizzingMutableInsertMethod));
        if (success) {
            class_addMethod(mutableArrayClass, swizzingMutableInsertSEL, method_getImplementation(mutableInsertMethod), method_getTypeEncoding(mutableInsertMethod));
        }else{
            method_exchangeImplementations(mutableInsertMethod, swizzingMutableInsertMethod);
        }
        
    });
}

#pragma mark - NSArrayM
- (void)__NSArrayM_insertObject:(id)anObject atIndex:(NSUInteger)index{
    if (!anObject) return;
    [self __NSArrayM_insertObject:anObject atIndex:index];
}

@end

相关文章

  • 防止数组越界Crash示例

  • iOS数组防止越界crash

    有时候项目中总是出现一些无法预知的情况,导致数组越界是程序crash,如果这种意外情况无法避免,那么只能从侧面采取...

  • 用Runtime防止数组越界crash

    利用runtime可以实现交换系统方法这一强大功能,下面将利用这一功能实现即使数组越界也不会crash这一功能。 ...

  • GDB 难以定位的 Crash - 越界访问

    示例代码: 这段程序越界写入数组会导致 crash,但是 core 文件定位的时候并不好找,bt 打印的信息: 从...

  • Crash防护

    Container crash(数组越界,插nil等)NSString crash (字符串操作的crash)NS...

  • Try_Catch_finally

    数组越界等Crash时没问题,但野指针时依然会崩溃。

  • iOS 数组越界 Crash处理

    我们先来看看有可能会出现的数组越界Crash的地方; 上面代码是有可能会越界的;出现Crash也不好复现,发出去的...

  • iOS防止数组越界

    可以直接hook数组的objectIndex方法进行判断,但是一定要记住,NSArray真正的类型是__NSArr...

  • 防止数组越界崩溃

    1. 数组越界 访问为空是我们的app的一大元凶,当访问数组经常会访问为空,这是就会抛出异常导致app闪退,有时当...

  • iOS 常见crash的简单处理

    在日常项目中,常见的crash包括:给NSNull发送消息,数组越界,字典传空值等。我们可以对这些crash简单的...

网友评论

      本文标题:防止数组越界Crash示例

      本文链接:https://www.haomeiwen.com/subject/gdvizltx.html