[clang] Adding model for NSString length to avoid false positive npes.

Reviewed By: akotulski

Differential Revision: D4231656

fbshipit-source-id: 4201e57
master
Dulma Churchill 8 years ago committed by Facebook Github Bot
parent a8b0871660
commit 616ee9276b

@ -12,6 +12,8 @@
void __get_array_length(const UInt8); void __get_array_length(const UInt8);
void __infer_assume(bool cond);
@implementation NSString @implementation NSString
+ (instancetype)stringWithUTF8String:(const char*)bytes { + (instancetype)stringWithUTF8String:(const char*)bytes {
@ -57,4 +59,15 @@ void __get_array_length(const UInt8);
self->value = format->value; self->value = format->value;
return self; return self;
} }
- (int)length {
if (self == nil) {
return 0;
} else {
int res;
__infer_assume(res >= 0);
return res;
}
}
@end @end

@ -33,6 +33,7 @@ SOURCES_DEFAULT = \
npe/Npe_with_equal_names.m \ npe/Npe_with_equal_names.m \
npe/block.m \ npe/block.m \
npe/skip_method_with_nil_object.m \ npe/skip_method_with_nil_object.m \
npe/Nsstring_length_no_npe.m \
procdescs/MethodCall.m \ procdescs/MethodCall.m \
property/main.c \ property/main.c \
resource_leaks/ResourceLeakExample.m \ resource_leaks/ResourceLeakExample.m \

@ -0,0 +1,13 @@
#include <Foundation/Foundation.h>
@interface Nsstring_length_no_npe : NSObject
@end
@implementation Nsstring_length_no_npe
- (NSDictionary*)logMessage:(NSString* __nullable)message {
if (message.length > 0) {
return @{ @"key" : message }; // No NPE because of model of NSString length.
} else
return nil;
}
@end
Loading…
Cancel
Save