blob: 297c63d3e9a1478f92ad5847477d79d2ffe02aa9 [file] [log] [blame]
Ted Kremenekf536ca32009-01-22 18:54:47 +00001// RUN: clang -analyze -checker-cfref -analyzer-store-basic -verify %s &&
2// RUN: clang -analyze -checker-cfref -analyzer-store-region -verify %s
Ted Kremenek859be3b2008-06-16 20:37:30 +00003
4//===----------------------------------------------------------------------===//
5// The following code is reduced using delta-debugging from
6// Foundation.h (Mac OS X).
7//
8// It includes the basic definitions for the test cases below.
9// Not directly including Foundation.h directly makes this test case
10// both svelte and portable to non-Mac platforms.
11//===----------------------------------------------------------------------===//
12
13typedef const void * CFTypeRef;
14typedef const struct __CFString * CFStringRef;
15typedef const struct __CFAllocator * CFAllocatorRef;
16extern const CFAllocatorRef kCFAllocatorDefault;
17extern CFTypeRef CFRetain(CFTypeRef cf);
18typedef const struct __CFDictionary * CFDictionaryRef;
Ted Kremenek2fb78a72008-12-17 21:50:35 +000019const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key);
Ted Kremenek859be3b2008-06-16 20:37:30 +000020extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...);
21typedef signed char BOOL;
22typedef int NSInteger;
23typedef unsigned int NSUInteger;
24@class NSString, Protocol;
25extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
26typedef NSInteger NSComparisonResult;
27typedef struct _NSZone NSZone;
28@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
29@protocol NSObject
30- (BOOL)isEqual:(id)object;
31- (oneway void)release;
Ted Kremenek2fb78a72008-12-17 21:50:35 +000032- (id)retain;
Ted Kremenek859be3b2008-06-16 20:37:30 +000033@end
34@protocol NSCopying
35- (id)copyWithZone:(NSZone *)zone;
36@end
37@protocol NSMutableCopying
38- (id)mutableCopyWithZone:(NSZone *)zone;
39@end
40@protocol NSCoding
41- (void)encodeWithCoder:(NSCoder *)aCoder;
42@end
43@interface NSObject <NSObject> {}
44- (id)init;
45+ (id)alloc;
46@end
47extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
48typedef struct {} NSFastEnumerationState;
49@protocol NSFastEnumeration
50- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
51@end
52@class NSString;
53typedef struct _NSRange {} NSRange;
54@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
55- (NSUInteger)count;
56@end
57@interface NSMutableArray : NSArray
58- (void)addObject:(id)anObject;
59- (id)initWithCapacity:(NSUInteger)numItems;
60@end
61typedef unsigned short unichar;
62@class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale;
63typedef NSUInteger NSStringCompareOptions;
64@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
65- (NSComparisonResult)compare:(NSString *)string;
66- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
67- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
68- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
69- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
70- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
71@end
72@interface NSSimpleCString : NSString {} @end
73@interface NSConstantString : NSSimpleCString @end
74extern void *_NSConstantStringClassReference;
75
76//===----------------------------------------------------------------------===//
77// Test cases.
78//===----------------------------------------------------------------------===//
79
80NSComparisonResult f1(NSString* s) {
81 NSString *aString = 0;
82 return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil.}}
83}
84
85NSComparisonResult f2(NSString* s) {
86 NSString *aString = 0;
87 return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil.}}
88}
89
90NSComparisonResult f3(NSString* s, NSStringCompareOptions op) {
91 NSString *aString = 0;
92 return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil.}}
93}
94
95NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) {
96 NSString *aString = 0;
97 return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil.}}
98}
99
100NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) {
101 NSString *aString = 0;
102 return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil.}}
103}
104
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000105NSArray *f6(NSString* s) {
Ted Kremenek859be3b2008-06-16 20:37:30 +0000106 return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil.}}
107}
108
109NSString* f7(NSString* s1, NSString* s2, NSString* s3) {
110
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000111 NSString* s4 = (NSString*)
Ted Kremenekcf118d42009-02-04 23:49:09 +0000112 CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}}
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000113 (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"),
114 s1, s2, s3);
Ted Kremenek859be3b2008-06-16 20:37:30 +0000115
116 CFRetain(s4);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000117 return s4;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000118}
119
120NSMutableArray* f8() {
121
122 NSString* s = [[NSString alloc] init];
123 NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2];
124 [a addObject:s];
125 [s release]; // no-warning
126 return a;
127}
128
129void f9() {
130
131 NSString* s = [[NSString alloc] init];
132 NSString* q = s;
133 [s release];
134 [q release]; // expected-warning {{used after it is released}}
135}
136
137NSString* f10() {
Ted Kremenek859be3b2008-06-16 20:37:30 +0000138 static NSString* s = 0;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000139 if (!s) s = [[NSString alloc] init];
Ted Kremenek859be3b2008-06-16 20:37:30 +0000140 return s; // no-warning
141}
142
Ted Kremenek2fb78a72008-12-17 21:50:35 +0000143// Test case for regression reported in <rdar://problem/6452745>.
144// Essentially 's' should not be considered allocated on the false branch.
145// This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp).
146NSString* f11(CFDictionaryRef dict, const char* key) {
147 NSString* s = (NSString*) CFDictionaryGetValue(dict, key);
148 [s retain];
149 if (s) {
150 [s release];
151 }
152}
153
Ted Kremenek784606f2008-12-18 23:40:58 +0000154// Test case for passing a tracked object by-reference to a function we
155// don't undersand.
156void unknown_function_f12(NSString** s);
157void f12() {
158 NSString *string = [[NSString alloc] init];
159 unknown_function_f12(&string); // no-warning
160}
161
162
Ted Kremenek859be3b2008-06-16 20:37:30 +0000163@interface C1 : NSObject {}
164- (NSString*) getShared;
165+ (C1*) sharedInstance;
166@end
Ted Kremenekaeca9632008-07-03 15:37:02 +0000167@implementation C1 : NSObject {}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000168- (NSString*) getShared {
169 static NSString* s = 0;
170 if (!s) s = [[NSString alloc] init];
171 return s; // no-warning
172}
173+ (C1 *)sharedInstance {
174 static C1 *sharedInstance = 0;
175 if (!sharedInstance) {
176 sharedInstance = [[C1 alloc] init];
177 }
178 return sharedInstance; // no-warning
179}
180@end
181
182@interface SharedClass : NSObject
183+ (id)sharedInstance;
Ted Kremenek234a4c22009-01-07 00:39:56 +0000184- (id)notShared;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000185@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000186
Ted Kremenekaeca9632008-07-03 15:37:02 +0000187@implementation SharedClass
Ted Kremenek859be3b2008-06-16 20:37:30 +0000188
189- (id)_init {
190 if ((self = [super init])) {
191 NSLog(@"Bar");
192 }
193 return self;
194}
195
Ted Kremenek234a4c22009-01-07 00:39:56 +0000196- (id)notShared {
Ted Kremenek3148eb42009-01-24 00:55:43 +0000197 return [[SharedClass alloc] _init]; // expected-warning{{[naming convention] leak of returned object}}
Ted Kremenek234a4c22009-01-07 00:39:56 +0000198}
199
Ted Kremenek859be3b2008-06-16 20:37:30 +0000200+ (id)sharedInstance {
201 static SharedClass *_sharedInstance = 0;
202 if (!_sharedInstance) {
203 _sharedInstance = [[SharedClass alloc] _init];
204 }
205 return _sharedInstance; // no-warning
206}
207@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000208
209id testSharedClassFromFunction() {
210 return [[SharedClass alloc] _init]; // no-warning
211}
212