blob: 4add37b975401de8f29e0f7ae54debbf210eb0ed [file] [log] [blame]
Jordy Rose17a38e22011-09-02 05:55:19 +00001// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,osx.AtomicCAS,experimental.core -analyzer-store=region -analyzer-constraints=basic -verify %s
2// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,osx.AtomicCAS,experimental.core -analyzer-store=region -analyzer-constraints=range -verify %s
3// RUN: %clang_cc1 -DTEST_64 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,osx.AtomicCAS,experimental.core -analyzer-store=region -analyzer-constraints=basic -verify %s
4// RUN: %clang_cc1 -DTEST_64 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,osx.AtomicCAS,experimental.core -analyzer-store=region -analyzer-constraints=range -verify %s
Ted Kremenek45f7c272009-10-15 01:40:34 +00005
Ted Kremenek859be3b2008-06-16 20:37:30 +00006
7//===----------------------------------------------------------------------===//
8// The following code is reduced using delta-debugging from
9// Foundation.h (Mac OS X).
10//
11// It includes the basic definitions for the test cases below.
12// Not directly including Foundation.h directly makes this test case
13// both svelte and portable to non-Mac platforms.
14//===----------------------------------------------------------------------===//
15
Ted Kremenek45f7c272009-10-15 01:40:34 +000016#ifdef TEST_64
17typedef long long int64_t;
18_Bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
19#define COMPARE_SWAP_BARRIER OSAtomicCompareAndSwap64Barrier
20typedef int64_t intptr_t;
21#else
Ted Kremeneka3f45402009-04-29 16:03:59 +000022typedef int int32_t;
Ted Kremenek45f7c272009-10-15 01:40:34 +000023_Bool OSAtomicCompareAndSwap32Barrier( int32_t __oldValue, int32_t __newValue, volatile int32_t *__theValue );
24#define COMPARE_SWAP_BARRIER OSAtomicCompareAndSwap32Barrier
25typedef int32_t intptr_t;
26#endif
27
Ted Kremenek859be3b2008-06-16 20:37:30 +000028typedef const void * CFTypeRef;
29typedef const struct __CFString * CFStringRef;
30typedef const struct __CFAllocator * CFAllocatorRef;
31extern const CFAllocatorRef kCFAllocatorDefault;
32extern CFTypeRef CFRetain(CFTypeRef cf);
Ted Kremenek09f14192009-04-21 20:01:03 +000033void CFRelease(CFTypeRef cf);
Ted Kremenek859be3b2008-06-16 20:37:30 +000034typedef const struct __CFDictionary * CFDictionaryRef;
Ted Kremenek2fb78a72008-12-17 21:50:35 +000035const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key);
Ted Kremenek859be3b2008-06-16 20:37:30 +000036extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...);
37typedef signed char BOOL;
38typedef int NSInteger;
39typedef unsigned int NSUInteger;
40@class NSString, Protocol;
41extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
42typedef NSInteger NSComparisonResult;
43typedef struct _NSZone NSZone;
44@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
45@protocol NSObject
46- (BOOL)isEqual:(id)object;
47- (oneway void)release;
Ted Kremenek2fb78a72008-12-17 21:50:35 +000048- (id)retain;
Ted Kremenek95d3b902009-05-11 15:26:06 +000049- (id)autorelease;
Ted Kremenek859be3b2008-06-16 20:37:30 +000050@end
51@protocol NSCopying
52- (id)copyWithZone:(NSZone *)zone;
53@end
54@protocol NSMutableCopying
55- (id)mutableCopyWithZone:(NSZone *)zone;
56@end
57@protocol NSCoding
58- (void)encodeWithCoder:(NSCoder *)aCoder;
59@end
60@interface NSObject <NSObject> {}
61- (id)init;
62+ (id)alloc;
63@end
64extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
65typedef struct {} NSFastEnumerationState;
66@protocol NSFastEnumeration
67- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
68@end
69@class NSString;
70typedef struct _NSRange {} NSRange;
71@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
72- (NSUInteger)count;
73@end
74@interface NSMutableArray : NSArray
75- (void)addObject:(id)anObject;
76- (id)initWithCapacity:(NSUInteger)numItems;
77@end
78typedef unsigned short unichar;
79@class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale;
80typedef NSUInteger NSStringCompareOptions;
81@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
82- (NSComparisonResult)compare:(NSString *)string;
83- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
84- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
85- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
86- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
87- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
Ted Kremeneke87450e2009-04-23 19:11:35 +000088+ (id)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2)));
Ted Kremenek859be3b2008-06-16 20:37:30 +000089@end
90@interface NSSimpleCString : NSString {} @end
91@interface NSConstantString : NSSimpleCString @end
92extern void *_NSConstantStringClassReference;
93
94//===----------------------------------------------------------------------===//
95// Test cases.
96//===----------------------------------------------------------------------===//
97
98NSComparisonResult f1(NSString* s) {
99 NSString *aString = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000100 return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000101}
102
103NSComparisonResult f2(NSString* s) {
104 NSString *aString = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000105 return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000106}
107
108NSComparisonResult f3(NSString* s, NSStringCompareOptions op) {
109 NSString *aString = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000110 return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000111}
112
113NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) {
114 NSString *aString = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000115 return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000116}
117
118NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) {
119 NSString *aString = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000120 return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000121}
122
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000123NSArray *f6(NSString* s) {
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000124 return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000125}
126
127NSString* f7(NSString* s1, NSString* s2, NSString* s3) {
128
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000129 NSString* s4 = (NSString*)
Ted Kremenekcf118d42009-02-04 23:49:09 +0000130 CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}}
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000131 (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"),
132 s1, s2, s3);
Ted Kremenek859be3b2008-06-16 20:37:30 +0000133
134 CFRetain(s4);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000135 return s4;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000136}
137
138NSMutableArray* f8() {
139
140 NSString* s = [[NSString alloc] init];
141 NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2];
142 [a addObject:s];
143 [s release]; // no-warning
144 return a;
145}
146
147void f9() {
148
149 NSString* s = [[NSString alloc] init];
150 NSString* q = s;
151 [s release];
152 [q release]; // expected-warning {{used after it is released}}
153}
154
155NSString* f10() {
Ted Kremenek859be3b2008-06-16 20:37:30 +0000156 static NSString* s = 0;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000157 if (!s) s = [[NSString alloc] init];
Ted Kremenek859be3b2008-06-16 20:37:30 +0000158 return s; // no-warning
159}
160
Ted Kremenek2fb78a72008-12-17 21:50:35 +0000161// Test case for regression reported in <rdar://problem/6452745>.
162// Essentially 's' should not be considered allocated on the false branch.
163// This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp).
164NSString* f11(CFDictionaryRef dict, const char* key) {
165 NSString* s = (NSString*) CFDictionaryGetValue(dict, key);
166 [s retain];
167 if (s) {
168 [s release];
169 }
Mike Stump4393b3f2009-07-21 18:46:15 +0000170 return 0;
Ted Kremenek2fb78a72008-12-17 21:50:35 +0000171}
172
Ted Kremenek784606f2008-12-18 23:40:58 +0000173// Test case for passing a tracked object by-reference to a function we
Ted Kremenek09f14192009-04-21 20:01:03 +0000174// don't understand.
Ted Kremenek784606f2008-12-18 23:40:58 +0000175void unknown_function_f12(NSString** s);
176void f12() {
177 NSString *string = [[NSString alloc] init];
178 unknown_function_f12(&string); // no-warning
179}
180
Ted Kremenek09f14192009-04-21 20:01:03 +0000181// Test double release of CFString (PR 4014).
182void f13(void) {
183 CFStringRef ref = CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100);
184 CFRelease(ref);
185 CFRelease(ref); // expected-warning{{Reference-counted object is used after it is released}}
186}
Ted Kremenek784606f2008-12-18 23:40:58 +0000187
Anders Carlssonb62bdce2011-03-08 20:05:26 +0000188@interface MyString : NSString
189@end
190
191void f14(MyString *s) {
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000192 [s compare:0]; // expected-warning {{Argument to 'MyString' method 'compare:' cannot be nil}}
Anders Carlssonb62bdce2011-03-08 20:05:26 +0000193}
194
Ted Kremenek95d3b902009-05-11 15:26:06 +0000195// Test regular use of -autorelease
196@interface TestAutorelease
197-(NSString*) getString;
198@end
199@implementation TestAutorelease
200-(NSString*) getString {
201 NSString *str = [[NSString alloc] init];
202 return [str autorelease]; // no-warning
203}
Ted Kremenek2033a952009-05-13 07:12:33 +0000204- (void)m1
205{
206 NSString *s = [[NSString alloc] init]; // expected-warning{{leak}}
207 [s retain];
208 [s autorelease];
209}
210- (void)m2
211{
212 NSString *s = [[[NSString alloc] init] autorelease]; // expected-warning{{leak}}
213 [s retain];
214}
215- (void)m3
216{
217 NSString *s = [[[NSString alloc] init] autorelease];
218 [s retain];
219 [s autorelease];
220}
221- (void)m4
222{
223 NSString *s = [[NSString alloc] init]; // expected-warning{{leak}}
224 [s retain];
225}
226- (void)m5
227{
228 NSString *s = [[NSString alloc] init];
229 [s autorelease];
230}
Ted Kremenek95d3b902009-05-11 15:26:06 +0000231@end
232
Ted Kremenek859be3b2008-06-16 20:37:30 +0000233@interface C1 : NSObject {}
234- (NSString*) getShared;
235+ (C1*) sharedInstance;
236@end
Ted Kremenekaeca9632008-07-03 15:37:02 +0000237@implementation C1 : NSObject {}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000238- (NSString*) getShared {
239 static NSString* s = 0;
240 if (!s) s = [[NSString alloc] init];
241 return s; // no-warning
242}
243+ (C1 *)sharedInstance {
244 static C1 *sharedInstance = 0;
245 if (!sharedInstance) {
246 sharedInstance = [[C1 alloc] init];
247 }
248 return sharedInstance; // no-warning
249}
250@end
251
252@interface SharedClass : NSObject
253+ (id)sharedInstance;
Ted Kremenek234a4c22009-01-07 00:39:56 +0000254- (id)notShared;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000255@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000256
Ted Kremenekaeca9632008-07-03 15:37:02 +0000257@implementation SharedClass
Ted Kremenek859be3b2008-06-16 20:37:30 +0000258
259- (id)_init {
260 if ((self = [super init])) {
261 NSLog(@"Bar");
262 }
263 return self;
264}
265
Ted Kremenek234a4c22009-01-07 00:39:56 +0000266- (id)notShared {
Ted Kremenek043254a2009-02-07 22:55:48 +0000267 return [[SharedClass alloc] _init]; // expected-warning{{leak}}
Ted Kremenek234a4c22009-01-07 00:39:56 +0000268}
269
Ted Kremenek859be3b2008-06-16 20:37:30 +0000270+ (id)sharedInstance {
271 static SharedClass *_sharedInstance = 0;
272 if (!_sharedInstance) {
273 _sharedInstance = [[SharedClass alloc] _init];
274 }
275 return _sharedInstance; // no-warning
276}
277@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000278
279id testSharedClassFromFunction() {
280 return [[SharedClass alloc] _init]; // no-warning
281}
282
Ted Kremenek1670e402009-04-11 00:11:10 +0000283// Test OSCompareAndSwap
284_Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue );
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000285extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation);
Ted Kremenek1670e402009-04-11 00:11:10 +0000286
287void testOSCompareAndSwap() {
288 NSString *old = 0;
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000289 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek1670e402009-04-11 00:11:10 +0000290 if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old))
291 [s release];
292 else
293 [old release];
294}
295
Ted Kremenek6bcd5a02009-12-09 23:29:55 +0000296void testOSCompareAndSwapXXBarrier_local() {
Ted Kremeneka3f45402009-04-29 16:03:59 +0000297 NSString *old = 0;
298 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek45f7c272009-10-15 01:40:34 +0000299 if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) &old))
Ted Kremeneka3f45402009-04-29 16:03:59 +0000300 [s release];
301 else
302 [old release];
303}
304
Ted Kremenek6bcd5a02009-12-09 23:29:55 +0000305void testOSCompareAndSwapXXBarrier_local_no_direct_release() {
Ted Kremenek45f7c272009-10-15 01:40:34 +0000306 NSString *old = 0;
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000307 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek45f7c272009-10-15 01:40:34 +0000308 if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) &old))
309 return;
310 else
311 [old release];
312}
313
314int testOSCompareAndSwapXXBarrier_id(Class myclass, id xclass) {
315 if (COMPARE_SWAP_BARRIER(0, (intptr_t) myclass, (intptr_t*) &xclass))
Ted Kremenek0aeaf5a2009-07-29 18:18:25 +0000316 return 1;
317 return 0;
Ted Kremenek45f7c272009-10-15 01:40:34 +0000318}
Ted Kremenek0aeaf5a2009-07-29 18:18:25 +0000319
Ted Kremenek6bcd5a02009-12-09 23:29:55 +0000320void test_objc_atomicCompareAndSwap_local() {
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000321 NSString *old = 0;
322 NSString *s = [[NSString alloc] init]; // no-warning
323 if (!objc_atomicCompareAndSwapPtr(0, s, &old))
324 [s release];
325 else
326 [old release];
327}
328
Ted Kremenek6bcd5a02009-12-09 23:29:55 +0000329void test_objc_atomicCompareAndSwap_local_no_direct_release() {
Ted Kremenek45f7c272009-10-15 01:40:34 +0000330 NSString *old = 0;
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000331 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek45f7c272009-10-15 01:40:34 +0000332 if (!objc_atomicCompareAndSwapPtr(0, s, &old))
333 return;
334 else
335 [old release];
336}
337
Ted Kremenek6bcd5a02009-12-09 23:29:55 +0000338void test_objc_atomicCompareAndSwap_parameter(NSString **old) {
339 NSString *s = [[NSString alloc] init]; // no-warning
340 if (!objc_atomicCompareAndSwapPtr(0, s, old))
341 [s release];
342 else
343 [*old release];
344}
345
346void test_objc_atomicCompareAndSwap_parameter_no_direct_release(NSString **old) {
347 NSString *s = [[NSString alloc] init]; // expected-warning{{leak}}
348 if (!objc_atomicCompareAndSwapPtr(0, s, old))
349 return;
350 else
351 [*old release];
352}
353
Ted Kremenek45f7c272009-10-15 01:40:34 +0000354
Ted Kremeneke87450e2009-04-23 19:11:35 +0000355// Test stringWithFormat (<rdar://problem/6815234>)
356void test_stringWithFormat() {
357 NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain];
358 [string release];
Ted Kremenekf9df1362009-04-23 21:25:57 +0000359 [string release]; // expected-warning{{Incorrect decrement of the reference count}}
Ted Kremeneke87450e2009-04-23 19:11:35 +0000360}
361
Ted Kremenek92511432009-05-03 06:08:32 +0000362// Test isTrackedObjectType().
Ted Kremenek97d095f2009-04-23 22:11:07 +0000363typedef NSString* WonkyTypedef;
364@interface TestIsTracked
365+ (WonkyTypedef)newString;
366@end
367
368void test_isTrackedObjectType(void) {
369 NSString *str = [TestIsTracked newString]; // expected-warning{{Potential leak}}
370}
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000371
Ted Kremenek92511432009-05-03 06:08:32 +0000372// Test isTrackedCFObjectType().
373@interface TestIsCFTracked
374+ (CFStringRef) badNewCFString;
375+ (CFStringRef) newCFString;
376@end
377
378@implementation TestIsCFTracked
379+ (CFStringRef) newCFString {
380 return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // no-warning
381}
382+ (CFStringRef) badNewCFString {
383 return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // expected-warning{{leak}}
384}
385
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000386// Test @synchronized
387void test_synchronized(id x) {
388 @synchronized(x) {
389 NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain]; // expected-warning {{leak}}
390 }
391}
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000392@end
Zhongxing Xu604848a2010-01-11 06:52:53 +0000393
394void testOSCompareAndSwapXXBarrier_parameter(NSString **old) {
395 NSString *s = [[NSString alloc] init]; // no-warning
396 if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) old))
397 [s release];
398 else
399 [*old release];
400}
Zhongxing Xu951b3342010-01-11 07:40:00 +0000401
402void testOSCompareAndSwapXXBarrier_parameter_no_direct_release(NSString **old) {
403 NSString *s = [[NSString alloc] init]; // no-warning
404 if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) old))
405 [s release];
406 else
407 return;
408}