blob: 6e644d5fbc814411c2110d50171249d3fa37648a [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;
76@end
77@interface NSSimpleCString : NSString {} @end
78@interface NSConstantString : NSSimpleCString @end
79extern void *_NSConstantStringClassReference;
80
81//===----------------------------------------------------------------------===//
82// Test cases.
83//===----------------------------------------------------------------------===//
84
85NSComparisonResult f1(NSString* s) {
86 NSString *aString = 0;
87 return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil.}}
88}
89
90NSComparisonResult f2(NSString* s) {
91 NSString *aString = 0;
92 return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil.}}
93}
94
95NSComparisonResult f3(NSString* s, NSStringCompareOptions op) {
96 NSString *aString = 0;
97 return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil.}}
98}
99
100NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) {
101 NSString *aString = 0;
102 return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil.}}
103}
104
105NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) {
106 NSString *aString = 0;
107 return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil.}}
108}
109
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000110NSArray *f6(NSString* s) {
Ted Kremenek859be3b2008-06-16 20:37:30 +0000111 return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil.}}
112}
113
114NSString* f7(NSString* s1, NSString* s2, NSString* s3) {
115
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000116 NSString* s4 = (NSString*)
Ted Kremenekcf118d42009-02-04 23:49:09 +0000117 CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}}
Daniel Dunbar4489fe12008-08-05 00:07:51 +0000118 (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"),
119 s1, s2, s3);
Ted Kremenek859be3b2008-06-16 20:37:30 +0000120
121 CFRetain(s4);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000122 return s4;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000123}
124
125NSMutableArray* f8() {
126
127 NSString* s = [[NSString alloc] init];
128 NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2];
129 [a addObject:s];
130 [s release]; // no-warning
131 return a;
132}
133
134void f9() {
135
136 NSString* s = [[NSString alloc] init];
137 NSString* q = s;
138 [s release];
139 [q release]; // expected-warning {{used after it is released}}
140}
141
142NSString* f10() {
Ted Kremenek859be3b2008-06-16 20:37:30 +0000143 static NSString* s = 0;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000144 if (!s) s = [[NSString alloc] init];
Ted Kremenek859be3b2008-06-16 20:37:30 +0000145 return s; // no-warning
146}
147
Ted Kremenek2fb78a72008-12-17 21:50:35 +0000148// Test case for regression reported in <rdar://problem/6452745>.
149// Essentially 's' should not be considered allocated on the false branch.
150// This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp).
151NSString* f11(CFDictionaryRef dict, const char* key) {
152 NSString* s = (NSString*) CFDictionaryGetValue(dict, key);
153 [s retain];
154 if (s) {
155 [s release];
156 }
157}
158
Ted Kremenek784606f2008-12-18 23:40:58 +0000159// Test case for passing a tracked object by-reference to a function we
Ted Kremenek09f14192009-04-21 20:01:03 +0000160// don't understand.
Ted Kremenek784606f2008-12-18 23:40:58 +0000161void unknown_function_f12(NSString** s);
162void f12() {
163 NSString *string = [[NSString alloc] init];
164 unknown_function_f12(&string); // no-warning
165}
166
Ted Kremenek09f14192009-04-21 20:01:03 +0000167// Test double release of CFString (PR 4014).
168void f13(void) {
169 CFStringRef ref = CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100);
170 CFRelease(ref);
171 CFRelease(ref); // expected-warning{{Reference-counted object is used after it is released}}
172}
Ted Kremenek784606f2008-12-18 23:40:58 +0000173
Ted Kremenek859be3b2008-06-16 20:37:30 +0000174@interface C1 : NSObject {}
175- (NSString*) getShared;
176+ (C1*) sharedInstance;
177@end
Ted Kremenekaeca9632008-07-03 15:37:02 +0000178@implementation C1 : NSObject {}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000179- (NSString*) getShared {
180 static NSString* s = 0;
181 if (!s) s = [[NSString alloc] init];
182 return s; // no-warning
183}
184+ (C1 *)sharedInstance {
185 static C1 *sharedInstance = 0;
186 if (!sharedInstance) {
187 sharedInstance = [[C1 alloc] init];
188 }
189 return sharedInstance; // no-warning
190}
191@end
192
193@interface SharedClass : NSObject
194+ (id)sharedInstance;
Ted Kremenek234a4c22009-01-07 00:39:56 +0000195- (id)notShared;
Ted Kremenek859be3b2008-06-16 20:37:30 +0000196@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000197
Ted Kremenekaeca9632008-07-03 15:37:02 +0000198@implementation SharedClass
Ted Kremenek859be3b2008-06-16 20:37:30 +0000199
200- (id)_init {
201 if ((self = [super init])) {
202 NSLog(@"Bar");
203 }
204 return self;
205}
206
Ted Kremenek234a4c22009-01-07 00:39:56 +0000207- (id)notShared {
Ted Kremenek043254a2009-02-07 22:55:48 +0000208 return [[SharedClass alloc] _init]; // expected-warning{{leak}}
Ted Kremenek234a4c22009-01-07 00:39:56 +0000209}
210
Ted Kremenek859be3b2008-06-16 20:37:30 +0000211+ (id)sharedInstance {
212 static SharedClass *_sharedInstance = 0;
213 if (!_sharedInstance) {
214 _sharedInstance = [[SharedClass alloc] _init];
215 }
216 return _sharedInstance; // no-warning
217}
218@end
Ted Kremenek234a4c22009-01-07 00:39:56 +0000219
220id testSharedClassFromFunction() {
221 return [[SharedClass alloc] _init]; // no-warning
222}
223
Ted Kremenek1670e402009-04-11 00:11:10 +0000224// Test OSCompareAndSwap
225_Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue );
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000226extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation);
Ted Kremenek1670e402009-04-11 00:11:10 +0000227
228void testOSCompareAndSwap() {
229 NSString *old = 0;
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000230 NSString *s = [[NSString alloc] init]; // no-warning
Ted Kremenek1670e402009-04-11 00:11:10 +0000231 if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old))
232 [s release];
233 else
234 [old release];
235}
236
Ted Kremenekb3bf76f2009-04-11 00:54:13 +0000237void test_objc_atomicCompareAndSwap() {
238 NSString *old = 0;
239 NSString *s = [[NSString alloc] init]; // no-warning
240 if (!objc_atomicCompareAndSwapPtr(0, s, &old))
241 [s release];
242 else
243 [old release];
244}
245