美文网首页
iOS数据安全-null空值与数组越界处理

iOS数据安全-null空值与数组越界处理

作者: 大风起兮_Seven | 来源:发表于2018-04-12 11:36 被阅读224次

    一是服务端返回数据为null

    可以使用NullSafe进行避免

    #import <objc/runtime.h>
    #import <Foundation/Foundation.h>
    
    
    #ifndef NULLSAFE_ENABLED
    #define NULLSAFE_ENABLED 1
    #endif
    
    
    #pragma GCC diagnostic ignored "-Wgnu-conditional-omitted-operand"
    
    
    @implementation NSNull (NullSafe)
    
    #if NULLSAFE_ENABLED
    
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
    {
        @synchronized([self class])
        {
            //look up method signature
            NSMethodSignature *signature = [super methodSignatureForSelector:selector];
            if (!signature)
            {
                //not supported by NSNull, search other classes
                static NSMutableSet *classList = nil;
                static NSMutableDictionary *signatureCache = nil;
                if (signatureCache == nil)
                {
                    classList = [[NSMutableSet alloc] init];
                    signatureCache = [[NSMutableDictionary alloc] init];
                    
                    //get class list
                    int numClasses = objc_getClassList(NULL, 0);
                    Class *classes = (Class *)malloc(sizeof(Class) * (unsigned long)numClasses);
                    numClasses = objc_getClassList(classes, numClasses);
                    
                    //add to list for checking
                    NSMutableSet *excluded = [NSMutableSet set];
                    for (int i = 0; i < numClasses; i++)
                    {
                        //determine if class has a superclass
                        Class someClass = classes[i];
                        Class superclass = class_getSuperclass(someClass);
                        while (superclass)
                        {
                            if (superclass == [NSObject class])
                            {
                                [classList addObject:someClass];
                                break;
                            }
                            [excluded addObject:NSStringFromClass(superclass)];
                            superclass = class_getSuperclass(superclass);
                        }
                    }
    
                    //remove all classes that have subclasses
                    for (Class someClass in excluded)
                    {
                        [classList removeObject:someClass];
                    }
    
                    //free class list
                    free(classes);
                }
                
                //check implementation cache first
                NSString *selectorString = NSStringFromSelector(selector);
                signature = signatureCache[selectorString];
                if (!signature)
                {
                    //find implementation
                    for (Class someClass in classList)
                    {
                        if ([someClass instancesRespondToSelector:selector])
                        {
                            signature = [someClass instanceMethodSignatureForSelector:selector];
                            break;
                        }
                    }
                    
                    //cache for next time
                    signatureCache[selectorString] = signature ?: [NSNull null];
                }
                else if ([signature isKindOfClass:[NSNull class]])
                {
                    signature = nil;
                }
            }
            return signature;
        }
    }
    
    - (void)forwardInvocation:(NSInvocation *)invocation
    {
        invocation.target = nil;
        [invocation invoke];
    }
    
    #endif
    
    @end
    

    二是字典,数组插入空值

    三是数组越界

    解决办法:

    (1). 分析业务避免运行时对象为nil的情况

    (2). 使用 _obj ?: @"" 设置对象为nil时的默认值,防止引发Crash

    (3). 检查调用的方法是否实现

    推荐用无侵入的第三方库来避免这种问题,必要的时候还能结合bugly上报自定义异常https://github.com/jasenhuang/NSObjectSafe

    相关文章

      网友评论

          本文标题:iOS数据安全-null空值与数组越界处理

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