防止數(shù)組越界Crash示例

#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, ^{
        //處理數(shù)組越界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 - 數(shù)組越界方法
- (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, ^{
        //處理數(shù)組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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容