blob: 702551bbda3e6d8312610336e1868086eb3e8b13 [file] [log] [blame]
Ted Kremeneka3f45402009-04-29 16:03:59 +00001// RUN: clang-cc -arch i386 -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
2// RUN: clang-cc -arch i386 -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s
Ted Kremenek1670e402009-04-11 00:11:10 +00003
4
Ted Kremeneka3f45402009-04-29 16:03:59 +00005// NOTWORK: clang-cc -arch i386 -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s &&
6// NOTWORK: clang-cc -arch i386 -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s
Ted Kremenek859be3b2008-06-16 20:37:30 +00007
8//===----------------------------------------------------------------------===//
9// The following code is reduced using delta-debugging from
10// Foundation.h (Mac OS X).
11//
12// It includes the basic definitions for the test cases below.
13// Not directly including Foundation.h directly makes this test case
14// both svelte and portable to non-Mac platforms.
15//===----------------------------------------------------------------------===//
16
Ted Kremeneka3f45402009-04-29 16:03:59 +000017typedef int int32_t;
Ted Kremenek859be3b2008-06-16 20:37:30 +000018typedef const void * CFTypeRef;
19typedef const struct __CFString * CFStringRef;
20typedef const struct __CFAllocator * CFAllocatorRef;
21extern const CFAllocatorRef kCFAllocatorDefault;
22extern CFTypeRef CFRetain(CFTypeRef cf);
Ted Kremenek09f14192009-04-21 20:01:03 +000023void CFRelease(CFTypeRef cf);
Ted Kremenek859be3b2008-06-16 20:37:30 +000024typedef const struct __CFDictionary * CFDictionaryRef;
Ted Kremenek2fb78a72008-12-17 21:50:35 +000025const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key);
Ted Kremenek859be3b2008-06-16 20:37:30 +000026extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...);
27typedef signed char BOOL;
28typedef int NSInteger;
29typedef unsigned int NSUInteger;
30@class NSString, Protocol;
31extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
32typedef NSInteger NSComparisonResult;
33typedef struct _NSZone NSZone;
34@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
35@protocol NSObject
36- (BOOL)isEqual:(id)object;
37- (oneway void)release;
Ted Kremenek2fb78a72008-12-17 21:50:35 +000038- (id)retain;
Ted Kremenek859be3b2008-06-16 20:37:30 +000039@end
40@protocol NSCopying
41- (id)copyWithZone:(NSZone *)zone;
42@end
43@protocol NSMutableCopying
44- (id)mutableCopyWithZone:(NSZone *)zone;
45@end
46@protocol NSCoding
47- (void)encodeWithCoder:(NSCoder *)aCoder;
48@end
49@interface NSObject <NSObject> {}
50- (id)init;
51+ (id)alloc;
52@end
53extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
54typedef struct {} NSFastEnumerationState;
55@protocol NSFastEnumeration
56- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
57@end
58@class NSString;
59typedef struct _NSRange {} NSRange;
60@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
61- (NSUInteger)count;
62@end
63@interface NSMutableArray : NSArray
64- (void)addObject:(id)anObject;
65- (id)initWithCapacity:(NSUInteger)numItems;
66@end
67typedef unsigned short unichar;
68@class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale;
69typedef NSUInteger NSStringCompareOptions;
70@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
71- (NSComparisonResult)compare:(NSString *)string;
72- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
73- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
74- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
75- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
76- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
Ted Kremeneke87450e2009-04-23 19:11:35 +000077+ (id)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2)));
Ted Kremenek859be3b2008-06-16 20:37:30 +000078@end
79@interface NSSimpleCString : NSString {} @end
80@interface NSConstantString : NSSimpleCString @end
81extern void *_NSConstantStringClassReference;
82
83//===----------------------------------------------------------------------===//
84// Test cases.
85//===----------------------------------------------------------------------===//
86
87NSComparisonResult f1(NSString* s) {
88 NSString *aString = 0;
89 return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil.}}
90}
91
92NSComparisonResult f2(NSString* s) {
93 NSString *aString = 0;
94 return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil.}}
95}
96
97NSComparisonResult f3(NSString* s, NSStringCompareOptions op) {
98 NSString *aString = 0;
99 return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil.}}
100}
101
102NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) {
103 NSString *aString = 0;
104 return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil.}}
105}
106
107NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) {
108 NSString *aString = 0;
109 return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil.}}
110}
111
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000112NSArray *f6(NSString* s) {
Ted Kremenek859be3b2008-06-16 20:37:30 +0000113 return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil.}}
114}
115
116NSString* f7(NSString* s1, NSString* s2, NSString* s3) {
117
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000118 NSString* s4 = (NSString*)
Ted Kremenekcf118d42009-02-04 23:49:09 +0000119 CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}}
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000120 (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"),
121 s1, s2, s3);
Ted Kremenek859be3b2008-06-16 20:37:30 +0000122
123 CFRetain(s4);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000124 return s4;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000125}
126
127NSMutableArray* f8() {
128
129 NSString* s = [[NSString alloc] init];
130 NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2];
131 [a addObject:s];
132 [s release]; // no-warning
133 return a;
134}
135
136void f9() {
137
138 NSString* s = [[NSString alloc] init];
139 NSString* q = s;
140 [s release];
141 [q release]; // expected-warning {{used after it is released}}
142}
143
144NSString* f10() {
Ted Kremenek859be3b2008-06-16 20:37:30 +0000145 static NSString* s = 0;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000146 if (!s) s = [[NSString alloc] init];
Ted Kremenek859be3b2008-06-16 20:37:30 +0000147 return s; // no-warning
148}
149
Ted Kremenek2fb78a72008-12-17 21:50:35 +0000150// Test case for regression reported in <rdar://problem/6452745>.
151// Essentially 's' should not be considered allocated on the false branch.
152// This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp).
153NSString* f11(CFDictionaryRef dict, const char* key) {
154 NSString* s = (NSString*) CFDictionaryGetValue(dict, key);
155 [s retain];
156 if (s) {
157 [s release];
158 }
159}
160
Ted Kremenek784606f2008-12-18 23:40:58 +0000161// Test case for passing a tracked object by-reference to a function we
Ted Kremenek09f14192009-04-21 20:01:03 +0000162// don't understand.
Ted Kremenek784606f2008-12-18 23:40:58 +0000163void unknown_function_f12(NSString** s);
164void f12() {
165 NSString *string = [[NSString alloc] init];
166 unknown_function_f12(&string); // no-warning
167}
168
Ted Kremenek09f14192009-04-21 20:01:03 +0000169// Test double release of CFString (PR 4014).
170void f13(void) {
171 CFStringRef ref = CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100);
172 CFRelease(ref);
173 CFRelease(ref); // expected-warning{{Reference-counted object is used after it is released}}
174}
Ted Kremenek784606f2008-12-18 23:40:58 +0000175
Ted Kremenek859be3b2008-06-16 20:37:30 +0000176@interface C1 : NSObject {}
177- (NSString*) getShared;
178+ (C1*) sharedInstance;
179@end
Ted Kremenekaeca9632008-07-03 15:37:02 +0000180@implementation C1 : NSObject {}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000181- (NSString*) getShared {
182 static NSString* s = 0;
183 if (!s) s = [[NSString alloc] init];
184 return s; // no-warning
185}
186+ (C1 *)sharedInstance {
187 static C1 *sharedInstance = 0;
188 if (!sharedInstance) {
189 sharedInstance = [[C1 alloc] init];
190 }
191 return sharedInstance; // no-warning
192}
193@end
194
195@interface SharedClass : NSObject
196+ (id)sharedInstance;
Ted Kremenek234a4c22009-01-07 00:39:56 +0000197- (id)notShared;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000198@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000199
Ted Kremenekaeca9632008-07-03 15:37:02 +0000200@implementation SharedClass
Ted Kremenek859be3b2008-06-16 20:37:30 +0000201
202- (id)_init {
203 if ((self = [super init])) {
204 NSLog(@"Bar");
205 }
206 return self;
207}
208
Ted Kremenek234a4c22009-01-07 00:39:56 +0000209- (id)notShared {
Ted Kremenek043254a2009-02-07 22:55:48 +0000210 return [[SharedClass alloc] _init]; // expected-warning{{leak}}
Ted Kremenek234a4c22009-01-07 00:39:56 +0000211}
212
Ted Kremenek859be3b2008-06-16 20:37:30 +0000213+ (id)sharedInstance {
214 static SharedClass *_sharedInstance = 0;
215 if (!_sharedInstance) {
216 _sharedInstance = [[SharedClass alloc] _init];
217 }
218 return _sharedInstance; // no-warning
219}
220@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000221
222id testSharedClassFromFunction() {
223 return [[SharedClass alloc] _init]; // no-warning
224}
225
Ted Kremenek1670e402009-04-11 00:11:10 +0000226// Test OSCompareAndSwap
227_Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue );
Ted Kremenek98104722009-05-01 17:37:31 +0000228_Bool OSAtomicCompareAndSwap32Barrier( int32_t __oldValue, int32_t __newValue, volatile int32_t *__theValue );
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000229extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation);
Ted Kremenek1670e402009-04-11 00:11:10 +0000230
231void testOSCompareAndSwap() {
232 NSString *old = 0;
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000233 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek1670e402009-04-11 00:11:10 +0000234 if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old))
235 [s release];
236 else
237 [old release];
238}
239
Ted Kremeneka3f45402009-04-29 16:03:59 +0000240void testOSCompareAndSwap32Barrier() {
241 NSString *old = 0;
242 NSString *s = [[NSString alloc] init]; // no-warning
243 if (!OSAtomicCompareAndSwap32Barrier((int32_t) 0, (int32_t) s, (int32_t*) &old))
244 [s release];
245 else
246 [old release];
247}
248
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000249void test_objc_atomicCompareAndSwap() {
250 NSString *old = 0;
251 NSString *s = [[NSString alloc] init]; // no-warning
252 if (!objc_atomicCompareAndSwapPtr(0, s, &old))
253 [s release];
254 else
255 [old release];
256}
257
Ted Kremeneke87450e2009-04-23 19:11:35 +0000258// Test stringWithFormat (<rdar://problem/6815234>)
259void test_stringWithFormat() {
260 NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain];
261 [string release];
Ted Kremenekf9df1362009-04-23 21:25:57 +0000262 [string release]; // expected-warning{{Incorrect decrement of the reference count}}
Ted Kremeneke87450e2009-04-23 19:11:35 +0000263}
264
Ted Kremenek92511432009-05-03 06:08:32 +0000265// Test isTrackedObjectType().
Ted Kremenek97d095f2009-04-23 22:11:07 +0000266typedef NSString* WonkyTypedef;
267@interface TestIsTracked
268+ (WonkyTypedef)newString;
269@end
270
271void test_isTrackedObjectType(void) {
272 NSString *str = [TestIsTracked newString]; // expected-warning{{Potential leak}}
273}
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000274
Ted Kremenek92511432009-05-03 06:08:32 +0000275// Test isTrackedCFObjectType().
276@interface TestIsCFTracked
277+ (CFStringRef) badNewCFString;
278+ (CFStringRef) newCFString;
279@end
280
281@implementation TestIsCFTracked
282+ (CFStringRef) newCFString {
283 return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // no-warning
284}
285+ (CFStringRef) badNewCFString {
286 return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // expected-warning{{leak}}
287}
288
289
290
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000291// Test @synchronized
292void test_synchronized(id x) {
293 @synchronized(x) {
294 NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain]; // expected-warning {{leak}}
295 }
296}
297
Ted Kremenek92511432009-05-03 06:08:32 +0000298// Test return from method starting with 'new' or 'copy'