blob: a95e8868c3c9e0e0c621b943933b9a685ad4ec00 [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
Ted Kremenek1670e402009-04-11 00:11:10 +00002// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s
3
4
5// NOTWORK: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s &&
6// NOTWORK: clang-cc -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
17typedef const void * CFTypeRef;
18typedef const struct __CFString * CFStringRef;
19typedef const struct __CFAllocator * CFAllocatorRef;
20extern const CFAllocatorRef kCFAllocatorDefault;
21extern CFTypeRef CFRetain(CFTypeRef cf);
Ted Kremenek09f14192009-04-21 20:01:03 +000022void CFRelease(CFTypeRef cf);
Ted Kremenek859be3b2008-06-16 20:37:30 +000023typedef const struct __CFDictionary * CFDictionaryRef;
Ted Kremenek2fb78a72008-12-17 21:50:35 +000024const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key);
Ted Kremenek859be3b2008-06-16 20:37:30 +000025extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...);
26typedef signed char BOOL;
27typedef int NSInteger;
28typedef unsigned int NSUInteger;
29@class NSString, Protocol;
30extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
31typedef NSInteger NSComparisonResult;
32typedef struct _NSZone NSZone;
33@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
34@protocol NSObject
35- (BOOL)isEqual:(id)object;
36- (oneway void)release;
Ted Kremenek2fb78a72008-12-17 21:50:35 +000037- (id)retain;
Ted Kremenek859be3b2008-06-16 20:37:30 +000038@end
39@protocol NSCopying
40- (id)copyWithZone:(NSZone *)zone;
41@end
42@protocol NSMutableCopying
43- (id)mutableCopyWithZone:(NSZone *)zone;
44@end
45@protocol NSCoding
46- (void)encodeWithCoder:(NSCoder *)aCoder;
47@end
48@interface NSObject <NSObject> {}
49- (id)init;
50+ (id)alloc;
51@end
52extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
53typedef struct {} NSFastEnumerationState;
54@protocol NSFastEnumeration
55- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
56@end
57@class NSString;
58typedef struct _NSRange {} NSRange;
59@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
60- (NSUInteger)count;
61@end
62@interface NSMutableArray : NSArray
63- (void)addObject:(id)anObject;
64- (id)initWithCapacity:(NSUInteger)numItems;
65@end
66typedef unsigned short unichar;
67@class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale;
68typedef NSUInteger NSStringCompareOptions;
69@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
70- (NSComparisonResult)compare:(NSString *)string;
71- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
72- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
73- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
74- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
75- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
Ted Kremeneke87450e2009-04-23 19:11:35 +000076+ (id)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2)));
Ted Kremenek859be3b2008-06-16 20:37:30 +000077@end
78@interface NSSimpleCString : NSString {} @end
79@interface NSConstantString : NSSimpleCString @end
80extern void *_NSConstantStringClassReference;
81
82//===----------------------------------------------------------------------===//
83// Test cases.
84//===----------------------------------------------------------------------===//
85
86NSComparisonResult f1(NSString* s) {
87 NSString *aString = 0;
88 return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil.}}
89}
90
91NSComparisonResult f2(NSString* s) {
92 NSString *aString = 0;
93 return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil.}}
94}
95
96NSComparisonResult f3(NSString* s, NSStringCompareOptions op) {
97 NSString *aString = 0;
98 return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil.}}
99}
100
101NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) {
102 NSString *aString = 0;
103 return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil.}}
104}
105
106NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) {
107 NSString *aString = 0;
108 return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil.}}
109}
110
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000111NSArray *f6(NSString* s) {
Ted Kremenek859be3b2008-06-16 20:37:30 +0000112 return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil.}}
113}
114
115NSString* f7(NSString* s1, NSString* s2, NSString* s3) {
116
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000117 NSString* s4 = (NSString*)
Ted Kremenekcf118d42009-02-04 23:49:09 +0000118 CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}}
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000119 (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"),
120 s1, s2, s3);
Ted Kremenek859be3b2008-06-16 20:37:30 +0000121
122 CFRetain(s4);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000123 return s4;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000124}
125
126NSMutableArray* f8() {
127
128 NSString* s = [[NSString alloc] init];
129 NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2];
130 [a addObject:s];
131 [s release]; // no-warning
132 return a;
133}
134
135void f9() {
136
137 NSString* s = [[NSString alloc] init];
138 NSString* q = s;
139 [s release];
140 [q release]; // expected-warning {{used after it is released}}
141}
142
143NSString* f10() {
Ted Kremenek859be3b2008-06-16 20:37:30 +0000144 static NSString* s = 0;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000145 if (!s) s = [[NSString alloc] init];
Ted Kremenek859be3b2008-06-16 20:37:30 +0000146 return s; // no-warning
147}
148
Ted Kremenek2fb78a72008-12-17 21:50:35 +0000149// Test case for regression reported in <rdar://problem/6452745>.
150// Essentially 's' should not be considered allocated on the false branch.
151// This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp).
152NSString* f11(CFDictionaryRef dict, const char* key) {
153 NSString* s = (NSString*) CFDictionaryGetValue(dict, key);
154 [s retain];
155 if (s) {
156 [s release];
157 }
158}
159
Ted Kremenek784606f2008-12-18 23:40:58 +0000160// Test case for passing a tracked object by-reference to a function we
Ted Kremenek09f14192009-04-21 20:01:03 +0000161// don't understand.
Ted Kremenek784606f2008-12-18 23:40:58 +0000162void unknown_function_f12(NSString** s);
163void f12() {
164 NSString *string = [[NSString alloc] init];
165 unknown_function_f12(&string); // no-warning
166}
167
Ted Kremenek09f14192009-04-21 20:01:03 +0000168// Test double release of CFString (PR 4014).
169void f13(void) {
170 CFStringRef ref = CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100);
171 CFRelease(ref);
172 CFRelease(ref); // expected-warning{{Reference-counted object is used after it is released}}
173}
Ted Kremenek784606f2008-12-18 23:40:58 +0000174
Ted Kremenek859be3b2008-06-16 20:37:30 +0000175@interface C1 : NSObject {}
176- (NSString*) getShared;
177+ (C1*) sharedInstance;
178@end
Ted Kremenekaeca9632008-07-03 15:37:02 +0000179@implementation C1 : NSObject {}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000180- (NSString*) getShared {
181 static NSString* s = 0;
182 if (!s) s = [[NSString alloc] init];
183 return s; // no-warning
184}
185+ (C1 *)sharedInstance {
186 static C1 *sharedInstance = 0;
187 if (!sharedInstance) {
188 sharedInstance = [[C1 alloc] init];
189 }
190 return sharedInstance; // no-warning
191}
192@end
193
194@interface SharedClass : NSObject
195+ (id)sharedInstance;
Ted Kremenek234a4c22009-01-07 00:39:56 +0000196- (id)notShared;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000197@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000198
Ted Kremenekaeca9632008-07-03 15:37:02 +0000199@implementation SharedClass
Ted Kremenek859be3b2008-06-16 20:37:30 +0000200
201- (id)_init {
202 if ((self = [super init])) {
203 NSLog(@"Bar");
204 }
205 return self;
206}
207
Ted Kremenek234a4c22009-01-07 00:39:56 +0000208- (id)notShared {
Ted Kremenek043254a2009-02-07 22:55:48 +0000209 return [[SharedClass alloc] _init]; // expected-warning{{leak}}
Ted Kremenek234a4c22009-01-07 00:39:56 +0000210}
211
Ted Kremenek859be3b2008-06-16 20:37:30 +0000212+ (id)sharedInstance {
213 static SharedClass *_sharedInstance = 0;
214 if (!_sharedInstance) {
215 _sharedInstance = [[SharedClass alloc] _init];
216 }
217 return _sharedInstance; // no-warning
218}
219@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000220
221id testSharedClassFromFunction() {
222 return [[SharedClass alloc] _init]; // no-warning
223}
224
Ted Kremenek1670e402009-04-11 00:11:10 +0000225// Test OSCompareAndSwap
226_Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue );
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000227extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation);
Ted Kremenek1670e402009-04-11 00:11:10 +0000228
229void testOSCompareAndSwap() {
230 NSString *old = 0;
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000231 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek1670e402009-04-11 00:11:10 +0000232 if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old))
233 [s release];
234 else
235 [old release];
236}
237
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000238void test_objc_atomicCompareAndSwap() {
239 NSString *old = 0;
240 NSString *s = [[NSString alloc] init]; // no-warning
241 if (!objc_atomicCompareAndSwapPtr(0, s, &old))
242 [s release];
243 else
244 [old release];
245}
246
Ted Kremeneke87450e2009-04-23 19:11:35 +0000247// Test stringWithFormat (<rdar://problem/6815234>)
248void test_stringWithFormat() {
249 NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain];
250 [string release];
Ted Kremenekf9df1362009-04-23 21:25:57 +0000251 [string release]; // expected-warning{{Incorrect decrement of the reference count}}
Ted Kremeneke87450e2009-04-23 19:11:35 +0000252}
253