Rightpoint / RZImport

Automatic importing of data from NSDictionary to Cocoa objects
Other
18 stars 6 forks source link

Importing string as large NSNumber crashes in 32-bit build, works in 64-bit build. #22

Closed SteveCaine closed 9 years ago

SteveCaine commented 9 years ago

The imported value appears correct at end of -rzi_importValue:forKey:withMappings:ignoredKeys: but appears to be garbage immediately after, at the end of -rzi_importValuesFromDict:

Xcode 6.1, iPhone 4s simulator crashes, iPhone 6 simulator works.

Here's the JSON:

{"server_dt":"1417304059"}

Here's success in 64-bit build:

2014-12-22 13:52:50.138 RZImportBug[38195:1893565] result = 2014-11-29 23:34:19 +0000

Here's the crash in 32-bit build:

// code
NSTimeInterval secondsSince1970 = [self.server_dt integerValue]; Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x37c9801a)
// Xcode console
self    ApiTime *   0x7c980990
NSObject  
_server_dt    NSNumber *  0x7c9ce8e0
NSValue (null)
_servertime   NSDate *    nil 0x00000000

Here's the code:

#import "NSObject+RZImport.h"

// --------------------------------------------------

@interface ApiTime : NSObject
@property (assign, nonatomic)            NSNumber   *server_dt;  // imported
@property (strong, nonatomic, readwrite) NSDate     *servertime; // calculated
- (NSDate *)time;  
@end

@implementation ApiTime
- (NSDate *)time {
    if (self.servertime == nil) {
//      NSLog(@" server_dt = %@", self.server_dt); // even this crashes in 32-bit
        NSTimeInterval secondsSince1970 = [self.server_dt integerValue];
        self.servertime = [NSDate dateWithTimeIntervalSince1970:secondsSince1970];
    }
    return self.servertime;
}

- (NSString *)description {
    NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p>", NSStringFromClass([self class]), self];
    [result appendFormat:@" time = %@", self.time];
    return result;
}
@end

// --------------------------------------------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    const char *chars = "{\"server_dt\":\"1417304059\"}";
    NSData *data = [NSData dataWithBytes:chars length:strlen(chars)];
    NSError *error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error) {
        NSLog(@"Error reading JSON: %@", [error localizedDescription]);
    }
    else {
        ApiTime *obj = [ApiTime rzi_objectFromDictionary:json];
        NSLog(@"result = %@", [obj time]);
    }
}

Here's a screenshot:

simplest crash

SteveCaine commented 9 years ago

The Xcode project that reproduced this issue is available for upload.

SteveCaine commented 9 years ago

Not an RZIImport issue. Bug was on my end; the NSNumber property in question (server_dt) had been declared 'assign' rather than 'strong' or 'copy'. If it hadn't been working in 64-bit mode I might have spotted that sooner. Closing.