93 lines · plain
1// RUN: %clang_analyze_cc1 -Wno-objc-literal-conversion -analyzer-checker=core,unix.Malloc,osx.cocoa.NonNilReturnValue,debug.ExprInspection -verify %s2 3void clang_analyzer_eval(int);4 5typedef signed char BOOL;6typedef long NSInteger;7typedef unsigned long NSUInteger;8 9@protocol NSObject10@end11@interface NSObject <NSObject> {}12@end13@protocol NSCopying14@end15@protocol NSCoding16@end17 18@interface NSString @end19@interface NSString (NSStringExtensionMethods)20+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;21@end22 23@interface NSNumber24+ (NSNumber *)numberWithChar:(char)value;25+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;26+ (NSNumber *)numberWithShort:(short)value;27+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;28+ (NSNumber *)numberWithInt:(int)value;29+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;30+ (NSNumber *)numberWithLong:(long)value;31+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;32+ (NSNumber *)numberWithLongLong:(long long)value;33+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;34+ (NSNumber *)numberWithFloat:(float)value;35+ (NSNumber *)numberWithDouble:(double)value;36+ (NSNumber *)numberWithBool:(BOOL)value;37+ (NSNumber *)numberWithInteger:(NSInteger)value ;38+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value ;39@end40 41@interface NSValue : NSObject <NSCopying, NSCoding>42- (void)getValue:(void *)value;43+ (NSValue *)valueWithBytes:(const void *)value44 objCType:(const char *)type;45@end46 47typedef typeof(sizeof(int)) size_t;48extern void *malloc(size_t);49extern void free(void *);50extern char *strdup(const char *str);51 52id constant_string(void) {53 return @("boxed constant string.");54}55 56id dynamic_string(void) {57 return @(strdup("boxed dynamic string")); // expected-warning{{Potential memory leak}}58}59 60typedef struct __attribute__((objc_boxable)) {61 const char *str;62} BoxableStruct;63 64id leak_within_boxed_struct(void) {65 BoxableStruct bs;66 bs.str = strdup("dynamic string"); // The duped string shall be owned by val.67 NSValue *val = @(bs); // no-warning68 return val;69}70 71id leak_of_boxed_struct(void) {72 BoxableStruct *bs = malloc(sizeof(BoxableStruct)); // The pointer stored in bs isn't owned by val.73 NSValue *val = @(*bs); // expected-warning{{Potential leak of memory pointed to by 'bs'}}74 return val;75}76 77id const_char_pointer(int *x) {78 if (x)79 return @(3);80 return @(*x); // expected-warning {{Dereference of null pointer (loaded from variable 'x')}}81}82 83void checkNonNil(void) {84 clang_analyzer_eval(!!@3); // expected-warning{{TRUE}}85 clang_analyzer_eval(!!@(3+4)); // expected-warning{{TRUE}}86 clang_analyzer_eval(!!@(57.0)); // expected-warning{{TRUE}}87 88 const char *str = "abc";89 clang_analyzer_eval(!!@(str)); // expected-warning{{TRUE}}90 clang_analyzer_eval(!!@__objc_yes); // expected-warning{{TRUE}}91}92 93