From 826accc21b3b3f2c9b8fda1be382b2f289dd6ac7 Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Fri, 2 Dec 2016 10:01:48 -0800 Subject: [PATCH] [clang] Adding model for NSNumber boxing functions Reviewed By: akotulski Differential Revision: D4265407 fbshipit-source-id: 1e38b85 --- infer/models/objc/src/NSNumber.m | 85 +++++++++++++------ .../objc/errors/npe/BoxedNumberExample.m | 8 ++ 2 files changed, 65 insertions(+), 28 deletions(-) diff --git a/infer/models/objc/src/NSNumber.m b/infer/models/objc/src/NSNumber.m index cbc8837cb..6f6671b7f 100644 --- a/infer/models/objc/src/NSNumber.m +++ b/infer/models/objc/src/NSNumber.m @@ -11,58 +11,87 @@ @implementation NSNumber -+ (NSNumber*)numberWithInt:(int)value { - // using alloc as the documentation doesn't say it may return nil - NSNumber* number = [self alloc]; - return [number initWithInt:value]; -} - - (id)initWithInt:(int)v { self->value = (double)v; return self; } -+ (NSNumber*)numberWithFloat:(float)value { - // using alloc as the documentation doesn't say it may return nil - NSNumber* number = [NSNumber alloc]; - return [number initWithInt:value]; +- (id)initWithFloat:(float)v { + return [self initWithInt:(int)v]; } -- (id)initWithFloat:(float)v { - self->value = (double)v; - return self; +- (id)initWithBool:(BOOL)v { + return [self initWithInt:(int)v]; } -+ (NSNumber*)numberWithDouble:(double)value { +- (id)initWithDouble:(double)v { + return [self initWithInt:(int)v]; +} + +- (id)initWithUnsignedInteger:(NSUInteger)v { + return [self initWithInt:(int)v]; +} + ++ (NSNumber*)numberWithInt:(int)value { // using alloc as the documentation doesn't say it may return nil NSNumber* number = [self alloc]; return [number initWithInt:value]; } -- (id)initWithDouble:(double)v { - self->value = v; - return self; ++ (NSNumber*)numberWithChar:(char)value { + return [NSNumber numberWithInt:(int)value]; +} + ++ (NSNumber*)numberWithShort:(short)value { + return [NSNumber numberWithInt:(int)value]; } + (NSNumber*)numberWithBool:(BOOL)value { - // using alloc as the documentation doesn't say it may return nil - NSNumber* number = [self alloc]; - return [number initWithBool:value]; + return [NSNumber numberWithInt:(int)value]; } -- (id)initWithBool:(BOOL)v { - self->value = (double)v; - return self; ++ (NSNumber*)numberWithInteger:(NSInteger)value { + return [NSNumber numberWithInt:(int)value]; } -- (id)initWithUnsignedInteger:(NSUInteger)v { - self->value = v; - return self; ++ (NSNumber*)numberWithFloat:(float)value { + return [NSNumber numberWithInt:(int)value]; +} + ++ (NSNumber*)numberWithDouble:(double)value { + return [NSNumber numberWithInt:(int)value]; +} + ++ (NSNumber*)numberWithLong:(long)value { + return [NSNumber numberWithInt:(int)value]; +} + ++ (NSNumber*)numberWithLongLong:(long long)value { + return [NSNumber numberWithInt:(int)value]; +} + ++ (NSNumber*)numberWithUnsignedInt:(unsigned int)value { + return [NSNumber numberWithInt:(int)value]; +} + ++ (NSNumber*)numberWithUnsignedChar:(unsigned char)value { + return [NSNumber numberWithInt:(int)value]; +} + ++ (NSNumber*)numberWithUnsignedShort:(unsigned short)value { + return [NSNumber numberWithInt:(int)value]; } + (NSNumber*)numberWithUnsignedInteger:(NSUInteger)value { - NSNumber* number = [self alloc]; - return [number initWithUnsignedInteger:value]; + return [NSNumber numberWithInt:(int)value]; +} + ++ (NSNumber*)numberWithUnsignedLong:(unsigned long)value { + return [NSNumber numberWithInt:(int)value]; +} + ++ (NSNumber*)numberWithUnsignedLongLong:(unsigned long long)value { + return [NSNumber numberWithInt:(int)value]; } @end diff --git a/infer/tests/codetoanalyze/objc/errors/npe/BoxedNumberExample.m b/infer/tests/codetoanalyze/objc/errors/npe/BoxedNumberExample.m index 303e21ba2..73ff4b06d 100644 --- a/infer/tests/codetoanalyze/objc/errors/npe/BoxedNumberExample.m +++ b/infer/tests/codetoanalyze/objc/errors/npe/BoxedNumberExample.m @@ -33,4 +33,12 @@ typedef NS_ENUM(NSUInteger, SpacingEnum) { } } +// no null dereference because of models +- (void)test:(int)n { + NSMutableDictionary* reactionsPerOffset = [NSMutableDictionary new]; + NSNumber* randomIndex = @(arc4random_uniform(n)); + reactionsPerOffset[randomIndex] = + @([reactionsPerOffset[randomIndex] integerValue] + 1); +} + @end