blob: e7ac730c72e10179e67f8f5d1f1af8d67f7370b4 [file] [log] [blame]
Ted Kremenek42adacb2012-10-11 20:58:21 +00001// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,alpha.core -analyzer-store=region -analyzer-constraints=range -verify -Wno-objc-root-class %s
2// RUN: %clang_cc1 -DTEST_64 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,alpha.core -analyzer-store=region -analyzer-constraints=range -verify -Wno-objc-root-class %s
Ted Kremenek45f7c272009-10-15 01:40:34 +00003
Ted Kremenek859be3b2008-06-16 20:37:30 +00004
5//===----------------------------------------------------------------------===//
6// The following code is reduced using delta-debugging from
7// Foundation.h (Mac OS X).
8//
9// It includes the basic definitions for the test cases below.
10// Not directly including Foundation.h directly makes this test case
11// both svelte and portable to non-Mac platforms.
12//===----------------------------------------------------------------------===//
13
Ted Kremenek45f7c272009-10-15 01:40:34 +000014#ifdef TEST_64
15typedef long long int64_t;
16_Bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
17#define COMPARE_SWAP_BARRIER OSAtomicCompareAndSwap64Barrier
18typedef int64_t intptr_t;
19#else
Ted Kremeneka3f45402009-04-29 16:03:59 +000020typedef int int32_t;
Ted Kremenek45f7c272009-10-15 01:40:34 +000021_Bool OSAtomicCompareAndSwap32Barrier( int32_t __oldValue, int32_t __newValue, volatile int32_t *__theValue );
22#define COMPARE_SWAP_BARRIER OSAtomicCompareAndSwap32Barrier
23typedef int32_t intptr_t;
24#endif
25
Ted Kremenek859be3b2008-06-16 20:37:30 +000026typedef const void * CFTypeRef;
27typedef const struct __CFString * CFStringRef;
28typedef const struct __CFAllocator * CFAllocatorRef;
29extern const CFAllocatorRef kCFAllocatorDefault;
30extern CFTypeRef CFRetain(CFTypeRef cf);
Ted Kremenek09f14192009-04-21 20:01:03 +000031void CFRelease(CFTypeRef cf);
Ted Kremenek859be3b2008-06-16 20:37:30 +000032typedef const struct __CFDictionary * CFDictionaryRef;
Ted Kremenek2fb78a72008-12-17 21:50:35 +000033const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key);
Ted Kremenek859be3b2008-06-16 20:37:30 +000034extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...);
35typedef signed char BOOL;
36typedef int NSInteger;
37typedef unsigned int NSUInteger;
38@class NSString, Protocol;
39extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
40typedef NSInteger NSComparisonResult;
41typedef struct _NSZone NSZone;
42@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
43@protocol NSObject
44- (BOOL)isEqual:(id)object;
45- (oneway void)release;
Ted Kremenek2fb78a72008-12-17 21:50:35 +000046- (id)retain;
Ted Kremenek95d3b902009-05-11 15:26:06 +000047- (id)autorelease;
Ted Kremenek859be3b2008-06-16 20:37:30 +000048@end
49@protocol NSCopying
50- (id)copyWithZone:(NSZone *)zone;
51@end
52@protocol NSMutableCopying
53- (id)mutableCopyWithZone:(NSZone *)zone;
54@end
55@protocol NSCoding
56- (void)encodeWithCoder:(NSCoder *)aCoder;
57@end
58@interface NSObject <NSObject> {}
59- (id)init;
60+ (id)alloc;
61@end
62extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
63typedef struct {} NSFastEnumerationState;
64@protocol NSFastEnumeration
65- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
66@end
67@class NSString;
68typedef struct _NSRange {} NSRange;
69@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
70- (NSUInteger)count;
71@end
72@interface NSMutableArray : NSArray
73- (void)addObject:(id)anObject;
74- (id)initWithCapacity:(NSUInteger)numItems;
75@end
76typedef unsigned short unichar;
77@class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale;
78typedef NSUInteger NSStringCompareOptions;
79@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
80- (NSComparisonResult)compare:(NSString *)string;
81- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
82- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
83- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
84- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
85- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
Ted Kremeneke87450e2009-04-23 19:11:35 +000086+ (id)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2)));
Ted Kremenek859be3b2008-06-16 20:37:30 +000087@end
88@interface NSSimpleCString : NSString {} @end
89@interface NSConstantString : NSSimpleCString @end
90extern void *_NSConstantStringClassReference;
91
92//===----------------------------------------------------------------------===//
93// Test cases.
94//===----------------------------------------------------------------------===//
95
96NSComparisonResult f1(NSString* s) {
97 NSString *aString = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +000098 return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil}}
Ted Kremenek859be3b2008-06-16 20:37:30 +000099}
100
101NSComparisonResult f2(NSString* s) {
102 NSString *aString = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000103 return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000104}
105
106NSComparisonResult f3(NSString* s, NSStringCompareOptions op) {
107 NSString *aString = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000108 return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000109}
110
111NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) {
112 NSString *aString = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000113 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 +0000114}
115
116NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) {
117 NSString *aString = 0;
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000118 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 +0000119}
120
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000121NSArray *f6(NSString* s) {
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000122 return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000123}
124
125NSString* f7(NSString* s1, NSString* s2, NSString* s3) {
126
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000127 NSString* s4 = (NSString*)
Ted Kremenekcf118d42009-02-04 23:49:09 +0000128 CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}}
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000129 (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"),
130 s1, s2, s3);
Ted Kremenek859be3b2008-06-16 20:37:30 +0000131
132 CFRetain(s4);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000133 return s4;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000134}
135
136NSMutableArray* f8() {
137
138 NSString* s = [[NSString alloc] init];
139 NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2];
140 [a addObject:s];
141 [s release]; // no-warning
142 return a;
143}
144
145void f9() {
146
147 NSString* s = [[NSString alloc] init];
148 NSString* q = s;
149 [s release];
150 [q release]; // expected-warning {{used after it is released}}
151}
152
153NSString* f10() {
Ted Kremenek859be3b2008-06-16 20:37:30 +0000154 static NSString* s = 0;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000155 if (!s) s = [[NSString alloc] init];
Ted Kremenek859be3b2008-06-16 20:37:30 +0000156 return s; // no-warning
157}
158
Ted Kremenek2fb78a72008-12-17 21:50:35 +0000159// Test case for regression reported in <rdar://problem/6452745>.
160// Essentially 's' should not be considered allocated on the false branch.
161// This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp).
162NSString* f11(CFDictionaryRef dict, const char* key) {
163 NSString* s = (NSString*) CFDictionaryGetValue(dict, key);
164 [s retain];
165 if (s) {
166 [s release];
167 }
Mike Stump4393b3f2009-07-21 18:46:15 +0000168 return 0;
Ted Kremenek2fb78a72008-12-17 21:50:35 +0000169}
170
Ted Kremenek784606f2008-12-18 23:40:58 +0000171// Test case for passing a tracked object by-reference to a function we
Ted Kremenek09f14192009-04-21 20:01:03 +0000172// don't understand.
Ted Kremenek784606f2008-12-18 23:40:58 +0000173void unknown_function_f12(NSString** s);
174void f12() {
175 NSString *string = [[NSString alloc] init];
176 unknown_function_f12(&string); // no-warning
177}
178
Ted Kremenek09f14192009-04-21 20:01:03 +0000179// Test double release of CFString (PR 4014).
180void f13(void) {
181 CFStringRef ref = CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100);
182 CFRelease(ref);
183 CFRelease(ref); // expected-warning{{Reference-counted object is used after it is released}}
184}
Ted Kremenek784606f2008-12-18 23:40:58 +0000185
Anders Carlssonb62bdce2011-03-08 20:05:26 +0000186@interface MyString : NSString
187@end
188
189void f14(MyString *s) {
Richard Trieu2fe9b7f2011-12-15 00:38:15 +0000190 [s compare:0]; // expected-warning {{Argument to 'MyString' method 'compare:' cannot be nil}}
Anders Carlssonb62bdce2011-03-08 20:05:26 +0000191}
192
Ted Kremenek95d3b902009-05-11 15:26:06 +0000193// Test regular use of -autorelease
194@interface TestAutorelease
195-(NSString*) getString;
196@end
197@implementation TestAutorelease
198-(NSString*) getString {
199 NSString *str = [[NSString alloc] init];
200 return [str autorelease]; // no-warning
201}
Ted Kremenek2033a952009-05-13 07:12:33 +0000202- (void)m1
203{
204 NSString *s = [[NSString alloc] init]; // expected-warning{{leak}}
205 [s retain];
206 [s autorelease];
207}
208- (void)m2
209{
210 NSString *s = [[[NSString alloc] init] autorelease]; // expected-warning{{leak}}
211 [s retain];
212}
213- (void)m3
214{
215 NSString *s = [[[NSString alloc] init] autorelease];
216 [s retain];
217 [s autorelease];
218}
219- (void)m4
220{
221 NSString *s = [[NSString alloc] init]; // expected-warning{{leak}}
222 [s retain];
223}
224- (void)m5
225{
226 NSString *s = [[NSString alloc] init];
227 [s autorelease];
228}
Ted Kremenek95d3b902009-05-11 15:26:06 +0000229@end
230
Ted Kremenek859be3b2008-06-16 20:37:30 +0000231@interface C1 : NSObject {}
232- (NSString*) getShared;
233+ (C1*) sharedInstance;
234@end
Ted Kremenekaeca9632008-07-03 15:37:02 +0000235@implementation C1 : NSObject {}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000236- (NSString*) getShared {
237 static NSString* s = 0;
238 if (!s) s = [[NSString alloc] init];
239 return s; // no-warning
240}
241+ (C1 *)sharedInstance {
242 static C1 *sharedInstance = 0;
243 if (!sharedInstance) {
244 sharedInstance = [[C1 alloc] init];
245 }
246 return sharedInstance; // no-warning
247}
248@end
249
250@interface SharedClass : NSObject
251+ (id)sharedInstance;
Ted Kremenek234a4c22009-01-07 00:39:56 +0000252- (id)notShared;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000253@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000254
Ted Kremenekaeca9632008-07-03 15:37:02 +0000255@implementation SharedClass
Ted Kremenek859be3b2008-06-16 20:37:30 +0000256
257- (id)_init {
258 if ((self = [super init])) {
259 NSLog(@"Bar");
260 }
261 return self;
262}
263
Ted Kremenek234a4c22009-01-07 00:39:56 +0000264- (id)notShared {
Ted Kremenek043254a2009-02-07 22:55:48 +0000265 return [[SharedClass alloc] _init]; // expected-warning{{leak}}
Ted Kremenek234a4c22009-01-07 00:39:56 +0000266}
267
Ted Kremenek859be3b2008-06-16 20:37:30 +0000268+ (id)sharedInstance {
269 static SharedClass *_sharedInstance = 0;
270 if (!_sharedInstance) {
271 _sharedInstance = [[SharedClass alloc] _init];
272 }
273 return _sharedInstance; // no-warning
274}
275@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000276
277id testSharedClassFromFunction() {
278 return [[SharedClass alloc] _init]; // no-warning
279}
280
Ted Kremenek1670e402009-04-11 00:11:10 +0000281// Test OSCompareAndSwap
282_Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue );
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000283extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation);
Ted Kremenek1670e402009-04-11 00:11:10 +0000284
285void testOSCompareAndSwap() {
286 NSString *old = 0;
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000287 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek1670e402009-04-11 00:11:10 +0000288 if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old))
289 [s release];
290 else
291 [old release];
292}
293
Ted Kremenek6bcd5a02009-12-09 23:29:55 +0000294void testOSCompareAndSwapXXBarrier_local() {
Ted Kremeneka3f45402009-04-29 16:03:59 +0000295 NSString *old = 0;
296 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek45f7c272009-10-15 01:40:34 +0000297 if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) &old))
Ted Kremeneka3f45402009-04-29 16:03:59 +0000298 [s release];
299 else
300 [old release];
301}
302
Ted Kremenek6bcd5a02009-12-09 23:29:55 +0000303void testOSCompareAndSwapXXBarrier_local_no_direct_release() {
Ted Kremenek45f7c272009-10-15 01:40:34 +0000304 NSString *old = 0;
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000305 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek45f7c272009-10-15 01:40:34 +0000306 if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) &old))
307 return;
308 else
309 [old release];
310}
311
312int testOSCompareAndSwapXXBarrier_id(Class myclass, id xclass) {
313 if (COMPARE_SWAP_BARRIER(0, (intptr_t) myclass, (intptr_t*) &xclass))
Ted Kremenek0aeaf5a2009-07-29 18:18:25 +0000314 return 1;
315 return 0;
Ted Kremenek45f7c272009-10-15 01:40:34 +0000316}
Ted Kremenek0aeaf5a2009-07-29 18:18:25 +0000317
Ted Kremenek6bcd5a02009-12-09 23:29:55 +0000318void test_objc_atomicCompareAndSwap_local() {
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000319 NSString *old = 0;
320 NSString *s = [[NSString alloc] init]; // no-warning
321 if (!objc_atomicCompareAndSwapPtr(0, s, &old))
322 [s release];
323 else
324 [old release];
325}
326
Ted Kremenek6bcd5a02009-12-09 23:29:55 +0000327void test_objc_atomicCompareAndSwap_local_no_direct_release() {
Ted Kremenek45f7c272009-10-15 01:40:34 +0000328 NSString *old = 0;
Zhongxing Xu2f4a6b22009-12-09 08:32:57 +0000329 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek45f7c272009-10-15 01:40:34 +0000330 if (!objc_atomicCompareAndSwapPtr(0, s, &old))
331 return;
332 else
333 [old release];
334}
335
Ted Kremenek6bcd5a02009-12-09 23:29:55 +0000336void test_objc_atomicCompareAndSwap_parameter(NSString **old) {
337 NSString *s = [[NSString alloc] init]; // no-warning
338 if (!objc_atomicCompareAndSwapPtr(0, s, old))
339 [s release];
340 else
341 [*old release];
342}
343
344void test_objc_atomicCompareAndSwap_parameter_no_direct_release(NSString **old) {
345 NSString *s = [[NSString alloc] init]; // expected-warning{{leak}}
346 if (!objc_atomicCompareAndSwapPtr(0, s, old))
347 return;
348 else
349 [*old release];
350}
351
Ted Kremenek45f7c272009-10-15 01:40:34 +0000352
Ted Kremeneke87450e2009-04-23 19:11:35 +0000353// Test stringWithFormat (<rdar://problem/6815234>)
354void test_stringWithFormat() {
355 NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain];
356 [string release];
Ted Kremenekf9df1362009-04-23 21:25:57 +0000357 [string release]; // expected-warning{{Incorrect decrement of the reference count}}
Ted Kremeneke87450e2009-04-23 19:11:35 +0000358}
359
Ted Kremenek92511432009-05-03 06:08:32 +0000360// Test isTrackedObjectType().
Ted Kremenek97d095f2009-04-23 22:11:07 +0000361typedef NSString* WonkyTypedef;
362@interface TestIsTracked
363+ (WonkyTypedef)newString;
364@end
365
366void test_isTrackedObjectType(void) {
367 NSString *str = [TestIsTracked newString]; // expected-warning{{Potential leak}}
368}
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000369
Ted Kremenek92511432009-05-03 06:08:32 +0000370// Test isTrackedCFObjectType().
371@interface TestIsCFTracked
372+ (CFStringRef) badNewCFString;
373+ (CFStringRef) newCFString;
374@end
375
376@implementation TestIsCFTracked
377+ (CFStringRef) newCFString {
378 return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // no-warning
379}
380+ (CFStringRef) badNewCFString {
381 return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // expected-warning{{leak}}
382}
383
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000384// Test @synchronized
385void test_synchronized(id x) {
386 @synchronized(x) {
387 NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain]; // expected-warning {{leak}}
388 }
389}
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000390@end
Zhongxing Xu604848a2010-01-11 06:52:53 +0000391
392void testOSCompareAndSwapXXBarrier_parameter(NSString **old) {
393 NSString *s = [[NSString alloc] init]; // no-warning
394 if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) old))
395 [s release];
396 else
397 [*old release];
398}
Zhongxing Xu951b3342010-01-11 07:40:00 +0000399
400void testOSCompareAndSwapXXBarrier_parameter_no_direct_release(NSString **old) {
401 NSString *s = [[NSString alloc] init]; // no-warning
402 if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) old))
403 [s release];
404 else
405 return;
406}
Anna Zaks453cb852013-02-02 00:30:04 +0000407
408@interface AlwaysInlineBodyFarmBodies : NSObject {
409 NSString *_value;
410}
411 - (NSString *)_value;
412 - (void)callValue;
413@end
414
415@implementation AlwaysInlineBodyFarmBodies
416
417- (NSString *)_value {
418 if (!_value) {
419 NSString *s = [[NSString alloc] init];
420 if (!OSAtomicCompareAndSwapPtr(0, s, (void**)&_value)) {
421 [s release];
422 }
423 }
424 return _value;
425}
426
427- (void)callValue {
428 [self _value];
429}
430@end