diff --git a/ChangeLog b/ChangeLog index 93a62b8ab..2f77c89b4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2010-06-25 Richard Frith-Macdonald + + * Source/NSKeyedArchiver.m: + * Source/NSKeyedUnarchiver.m: + Implement NSInteger methods and add range checking when decoding. + 2010-06-25 Richard Frith-Macdonald * Source/NSLocale.m diff --git a/Source/NSKeyedArchiver.m b/Source/NSKeyedArchiver.m index efbb47d7f..39c1c8f9c 100644 --- a/Source/NSKeyedArchiver.m +++ b/Source/NSKeyedArchiver.m @@ -632,6 +632,13 @@ static NSDictionary *makeReference(unsigned ref) [_enc setObject: [NSNumber numberWithInt: anInteger] forKey: aKey]; } +- (void) encodeInteger: (NSInteger)anInteger forKey: (NSString*)aKey +{ + CHECKKEY + + [_enc setObject: [NSNumber numberWithInteger: anInteger] forKey: aKey]; +} + - (void) encodeInt32: (int32_t)anInteger forKey: (NSString*)aKey { CHECKKEY diff --git a/Source/NSKeyedUnarchiver.m b/Source/NSKeyedUnarchiver.m index 1f098b16c..aee92325c 100644 --- a/Source/NSKeyedUnarchiver.m +++ b/Source/NSKeyedUnarchiver.m @@ -492,48 +492,45 @@ static NSMapTable *globalClassMap = 0; - (int) decodeIntForKey: (NSString*)aKey { - NSString *oldKey = aKey; - GETVAL - if (o != nil) - { - if ([o isKindOfClass: [NSNumber class]] == YES) - { - long long l = [o longLongValue]; + int64_t i = [self decodeInt64ForKey: aKey]; - return l; - } - else - { - [NSException raise: NSInvalidUnarchiveOperationException - format: @"[%@ +%@]: value for key(%@) is '%@'", - NSStringFromClass([self class]), NSStringFromSelector(_cmd), - oldKey, o]; - } +#if (INT_MAX < INT64_MAX) + if (i > INT_MAX || i < INT_MIN) + { + [NSException raise: NSRangeException + format: @"[%@ +%@]: value for key(%@) is out of range", + NSStringFromClass([self class]), NSStringFromSelector(_cmd), aKey]; } - return 0; +#endif + return (int)i; +} + +- (int) decodeIntegerForKey: (NSString*)aKey +{ + int64_t i = [self decodeInt64ForKey: aKey]; + +#if (INTPTR_MAX < INT64_MAX) + if (i > INTPTR_MAX || i < INTPTR_MIN) + { + [NSException raise: NSRangeException + format: @"[%@ +%@]: value for key(%@) is out of range", + NSStringFromClass([self class]), NSStringFromSelector(_cmd), aKey]; + } +#endif + return (NSInteger)i; } - (int32_t) decodeInt32ForKey: (NSString*)aKey { - NSString *oldKey = aKey; - GETVAL - if (o != nil) - { - if ([o isKindOfClass: [NSNumber class]] == YES) - { - long long l = [o longLongValue]; + int64_t i = [self decodeInt64ForKey: aKey]; - return l; - } - else - { - [NSException raise: NSInvalidUnarchiveOperationException - format: @"[%@ +%@]: value for key(%@) is '%@'", - NSStringFromClass([self class]), NSStringFromSelector(_cmd), - oldKey, o]; - } + if (i > INT32_MAX || i < INT32_MIN) + { + [NSException raise: NSRangeException + format: @"[%@ +%@]: value for key(%@) is out of range", + NSStringFromClass([self class]), NSStringFromSelector(_cmd), aKey]; } - return 0; + return (int32_t)i; } - (int64_t) decodeInt64ForKey: (NSString*)aKey