aryaxt / OCMapper

Objective-C library to easily map NSDictionary to model objects, works perfectly with Alamofire. ObjectMapper works similar to GSON
MIT License
347 stars 45 forks source link

Bad data could cause crash #52

Open aryaxt opened 8 years ago

aryaxt commented 8 years ago
- (NSString *)propertyNameForObject:(NSObject *)object byCaseInsensitivePropertyName:(NSString *)caseInsensitivePropertyName
{
    NSString *result = nil;
    Class currentClass = [object class];
    NSString *key = [NSString stringWithFormat:@"%@.%@", NSStringFromClass(currentClass), caseInsensitivePropertyName];

    if (self.propertyNameDictionary[key])
        return self.propertyNameDictionary[key];

    while (currentClass && currentClass != [NSObject class])
    {
        unsigned int outCount, i;
        objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);

        for (i = 0; i < outCount; i++)
        {
            objc_property_t property = properties[i];
            NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];

            if ([[propertyName lowercaseString] isEqual:[caseInsensitivePropertyName lowercaseString]])
            {
                result = propertyName;
                break;
            }
        }

        free(properties);

        if (result)
        {
            self.propertyNameDictionary[key] = result;
            return result;
        }

        currentClass = class_getSuperclass(currentClass);
    }

    return nil;
}

Do a null check on properties before calling free Also do a null check for key?