Eli Friedman | c219a15 | 2009-05-19 11:12:40 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -triple i386-pc-linux-gnu -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s && |
Ted Kremenek | c037eac | 2009-07-10 00:41:58 +0000 | [diff] [blame] | 2 | // RUN: clang-cc -triple i386-pc-linux-gnu -analyze -checker-cfref -analyzer-store=basic-old-cast -analyzer-constraints=basic -verify %s && |
Ted Kremenek | e1cea75 | 2009-07-06 21:58:46 +0000 | [diff] [blame] | 3 | // RUN: clang-cc -triple i386-pc-linux-gnu -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s && |
Ted Kremenek | ba02486 | 2009-07-20 21:44:10 +0000 | [diff] [blame] | 4 | // RUN: clang-cc -triple i386-pc-linux-gnu -analyze -checker-cfref -analyzer-store=basic-old-cast -analyzer-constraints=range -verify %s && |
| 5 | // RUN: clang-cc -triple i386-pc-linux-gnu -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s && |
| 6 | // RUN: clang-cc -triple i386-pc-linux-gnu -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 7 | |
| 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 Kremenek | a3f4540 | 2009-04-29 16:03:59 +0000 | [diff] [blame] | 17 | typedef int int32_t; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 18 | typedef const void * CFTypeRef; |
| 19 | typedef const struct __CFString * CFStringRef; |
| 20 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 21 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 22 | extern CFTypeRef CFRetain(CFTypeRef cf); |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame] | 23 | void CFRelease(CFTypeRef cf); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 24 | typedef const struct __CFDictionary * CFDictionaryRef; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 25 | const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 26 | extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...); |
| 27 | typedef signed char BOOL; |
| 28 | typedef int NSInteger; |
| 29 | typedef unsigned int NSUInteger; |
| 30 | @class NSString, Protocol; |
| 31 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 32 | typedef NSInteger NSComparisonResult; |
| 33 | typedef struct _NSZone NSZone; |
| 34 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 35 | @protocol NSObject |
| 36 | - (BOOL)isEqual:(id)object; |
| 37 | - (oneway void)release; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 38 | - (id)retain; |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 39 | - (id)autorelease; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 40 | @end |
| 41 | @protocol NSCopying |
| 42 | - (id)copyWithZone:(NSZone *)zone; |
| 43 | @end |
| 44 | @protocol NSMutableCopying |
| 45 | - (id)mutableCopyWithZone:(NSZone *)zone; |
| 46 | @end |
| 47 | @protocol NSCoding |
| 48 | - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 49 | @end |
| 50 | @interface NSObject <NSObject> {} |
| 51 | - (id)init; |
| 52 | + (id)alloc; |
| 53 | @end |
| 54 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 55 | typedef struct {} NSFastEnumerationState; |
| 56 | @protocol NSFastEnumeration |
| 57 | - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; |
| 58 | @end |
| 59 | @class NSString; |
| 60 | typedef struct _NSRange {} NSRange; |
| 61 | @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> |
| 62 | - (NSUInteger)count; |
| 63 | @end |
| 64 | @interface NSMutableArray : NSArray |
| 65 | - (void)addObject:(id)anObject; |
| 66 | - (id)initWithCapacity:(NSUInteger)numItems; |
| 67 | @end |
| 68 | typedef unsigned short unichar; |
| 69 | @class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale; |
| 70 | typedef NSUInteger NSStringCompareOptions; |
| 71 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
| 72 | - (NSComparisonResult)compare:(NSString *)string; |
| 73 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask; |
| 74 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange; |
| 75 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; |
| 76 | - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string; |
| 77 | - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator; |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 78 | + (id)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2))); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 79 | @end |
| 80 | @interface NSSimpleCString : NSString {} @end |
| 81 | @interface NSConstantString : NSSimpleCString @end |
| 82 | extern void *_NSConstantStringClassReference; |
| 83 | |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | // Test cases. |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
| 88 | NSComparisonResult f1(NSString* s) { |
| 89 | NSString *aString = 0; |
| 90 | return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil.}} |
| 91 | } |
| 92 | |
| 93 | NSComparisonResult f2(NSString* s) { |
| 94 | NSString *aString = 0; |
| 95 | return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil.}} |
| 96 | } |
| 97 | |
| 98 | NSComparisonResult f3(NSString* s, NSStringCompareOptions op) { |
| 99 | NSString *aString = 0; |
| 100 | return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil.}} |
| 101 | } |
| 102 | |
| 103 | NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) { |
| 104 | NSString *aString = 0; |
| 105 | return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil.}} |
| 106 | } |
| 107 | |
| 108 | NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) { |
| 109 | NSString *aString = 0; |
| 110 | return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil.}} |
| 111 | } |
| 112 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 113 | NSArray *f6(NSString* s) { |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 114 | return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil.}} |
| 115 | } |
| 116 | |
| 117 | NSString* f7(NSString* s1, NSString* s2, NSString* s3) { |
| 118 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 119 | NSString* s4 = (NSString*) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 120 | CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}} |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 121 | (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"), |
| 122 | s1, s2, s3); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 123 | |
| 124 | CFRetain(s4); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 125 | return s4; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | NSMutableArray* f8() { |
| 129 | |
| 130 | NSString* s = [[NSString alloc] init]; |
| 131 | NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2]; |
| 132 | [a addObject:s]; |
| 133 | [s release]; // no-warning |
| 134 | return a; |
| 135 | } |
| 136 | |
| 137 | void f9() { |
| 138 | |
| 139 | NSString* s = [[NSString alloc] init]; |
| 140 | NSString* q = s; |
| 141 | [s release]; |
| 142 | [q release]; // expected-warning {{used after it is released}} |
| 143 | } |
| 144 | |
| 145 | NSString* f10() { |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 146 | static NSString* s = 0; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 147 | if (!s) s = [[NSString alloc] init]; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 148 | return s; // no-warning |
| 149 | } |
| 150 | |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 151 | // Test case for regression reported in <rdar://problem/6452745>. |
| 152 | // Essentially 's' should not be considered allocated on the false branch. |
| 153 | // This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp). |
| 154 | NSString* f11(CFDictionaryRef dict, const char* key) { |
| 155 | NSString* s = (NSString*) CFDictionaryGetValue(dict, key); |
| 156 | [s retain]; |
| 157 | if (s) { |
| 158 | [s release]; |
| 159 | } |
Mike Stump | 4393b3f | 2009-07-21 18:46:15 +0000 | [diff] [blame^] | 160 | return 0; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 163 | // Test case for passing a tracked object by-reference to a function we |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame] | 164 | // don't understand. |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 165 | void unknown_function_f12(NSString** s); |
| 166 | void f12() { |
| 167 | NSString *string = [[NSString alloc] init]; |
| 168 | unknown_function_f12(&string); // no-warning |
| 169 | } |
| 170 | |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame] | 171 | // Test double release of CFString (PR 4014). |
| 172 | void f13(void) { |
| 173 | CFStringRef ref = CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); |
| 174 | CFRelease(ref); |
| 175 | CFRelease(ref); // expected-warning{{Reference-counted object is used after it is released}} |
| 176 | } |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 178 | // Test regular use of -autorelease |
| 179 | @interface TestAutorelease |
| 180 | -(NSString*) getString; |
| 181 | @end |
| 182 | @implementation TestAutorelease |
| 183 | -(NSString*) getString { |
| 184 | NSString *str = [[NSString alloc] init]; |
| 185 | return [str autorelease]; // no-warning |
| 186 | } |
Ted Kremenek | 2033a95 | 2009-05-13 07:12:33 +0000 | [diff] [blame] | 187 | - (void)m1 |
| 188 | { |
| 189 | NSString *s = [[NSString alloc] init]; // expected-warning{{leak}} |
| 190 | [s retain]; |
| 191 | [s autorelease]; |
| 192 | } |
| 193 | - (void)m2 |
| 194 | { |
| 195 | NSString *s = [[[NSString alloc] init] autorelease]; // expected-warning{{leak}} |
| 196 | [s retain]; |
| 197 | } |
| 198 | - (void)m3 |
| 199 | { |
| 200 | NSString *s = [[[NSString alloc] init] autorelease]; |
| 201 | [s retain]; |
| 202 | [s autorelease]; |
| 203 | } |
| 204 | - (void)m4 |
| 205 | { |
| 206 | NSString *s = [[NSString alloc] init]; // expected-warning{{leak}} |
| 207 | [s retain]; |
| 208 | } |
| 209 | - (void)m5 |
| 210 | { |
| 211 | NSString *s = [[NSString alloc] init]; |
| 212 | [s autorelease]; |
| 213 | } |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 214 | @end |
| 215 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 216 | @interface C1 : NSObject {} |
| 217 | - (NSString*) getShared; |
| 218 | + (C1*) sharedInstance; |
| 219 | @end |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 220 | @implementation C1 : NSObject {} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 221 | - (NSString*) getShared { |
| 222 | static NSString* s = 0; |
| 223 | if (!s) s = [[NSString alloc] init]; |
| 224 | return s; // no-warning |
| 225 | } |
| 226 | + (C1 *)sharedInstance { |
| 227 | static C1 *sharedInstance = 0; |
| 228 | if (!sharedInstance) { |
| 229 | sharedInstance = [[C1 alloc] init]; |
| 230 | } |
| 231 | return sharedInstance; // no-warning |
| 232 | } |
| 233 | @end |
| 234 | |
| 235 | @interface SharedClass : NSObject |
| 236 | + (id)sharedInstance; |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 237 | - (id)notShared; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 238 | @end |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 240 | @implementation SharedClass |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 241 | |
| 242 | - (id)_init { |
| 243 | if ((self = [super init])) { |
| 244 | NSLog(@"Bar"); |
| 245 | } |
| 246 | return self; |
| 247 | } |
| 248 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 249 | - (id)notShared { |
Ted Kremenek | 043254a | 2009-02-07 22:55:48 +0000 | [diff] [blame] | 250 | return [[SharedClass alloc] _init]; // expected-warning{{leak}} |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 253 | + (id)sharedInstance { |
| 254 | static SharedClass *_sharedInstance = 0; |
| 255 | if (!_sharedInstance) { |
| 256 | _sharedInstance = [[SharedClass alloc] _init]; |
| 257 | } |
| 258 | return _sharedInstance; // no-warning |
| 259 | } |
| 260 | @end |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 261 | |
| 262 | id testSharedClassFromFunction() { |
| 263 | return [[SharedClass alloc] _init]; // no-warning |
| 264 | } |
| 265 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 266 | // Test OSCompareAndSwap |
| 267 | _Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue ); |
Ted Kremenek | 9810472 | 2009-05-01 17:37:31 +0000 | [diff] [blame] | 268 | _Bool OSAtomicCompareAndSwap32Barrier( int32_t __oldValue, int32_t __newValue, volatile int32_t *__theValue ); |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 269 | extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 270 | |
| 271 | void testOSCompareAndSwap() { |
| 272 | NSString *old = 0; |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 273 | NSString *s = [[NSString alloc] init]; // no-warning |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 274 | if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old)) |
| 275 | [s release]; |
| 276 | else |
| 277 | [old release]; |
| 278 | } |
| 279 | |
Ted Kremenek | a3f4540 | 2009-04-29 16:03:59 +0000 | [diff] [blame] | 280 | void testOSCompareAndSwap32Barrier() { |
| 281 | NSString *old = 0; |
| 282 | NSString *s = [[NSString alloc] init]; // no-warning |
| 283 | if (!OSAtomicCompareAndSwap32Barrier((int32_t) 0, (int32_t) s, (int32_t*) &old)) |
| 284 | [s release]; |
| 285 | else |
| 286 | [old release]; |
| 287 | } |
| 288 | |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 289 | void test_objc_atomicCompareAndSwap() { |
| 290 | NSString *old = 0; |
| 291 | NSString *s = [[NSString alloc] init]; // no-warning |
| 292 | if (!objc_atomicCompareAndSwapPtr(0, s, &old)) |
| 293 | [s release]; |
| 294 | else |
| 295 | [old release]; |
| 296 | } |
| 297 | |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 298 | // Test stringWithFormat (<rdar://problem/6815234>) |
| 299 | void test_stringWithFormat() { |
| 300 | NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain]; |
| 301 | [string release]; |
Ted Kremenek | f9df136 | 2009-04-23 21:25:57 +0000 | [diff] [blame] | 302 | [string release]; // expected-warning{{Incorrect decrement of the reference count}} |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 305 | // Test isTrackedObjectType(). |
Ted Kremenek | 97d095f | 2009-04-23 22:11:07 +0000 | [diff] [blame] | 306 | typedef NSString* WonkyTypedef; |
| 307 | @interface TestIsTracked |
| 308 | + (WonkyTypedef)newString; |
| 309 | @end |
| 310 | |
| 311 | void test_isTrackedObjectType(void) { |
| 312 | NSString *str = [TestIsTracked newString]; // expected-warning{{Potential leak}} |
| 313 | } |
Ted Kremenek | b3b0b36 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 315 | // Test isTrackedCFObjectType(). |
| 316 | @interface TestIsCFTracked |
| 317 | + (CFStringRef) badNewCFString; |
| 318 | + (CFStringRef) newCFString; |
| 319 | @end |
| 320 | |
| 321 | @implementation TestIsCFTracked |
| 322 | + (CFStringRef) newCFString { |
| 323 | return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // no-warning |
| 324 | } |
| 325 | + (CFStringRef) badNewCFString { |
| 326 | return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // expected-warning{{leak}} |
| 327 | } |
| 328 | |
Ted Kremenek | b3b0b36 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 329 | // Test @synchronized |
| 330 | void test_synchronized(id x) { |
| 331 | @synchronized(x) { |
| 332 | NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain]; // expected-warning {{leak}} |
| 333 | } |
| 334 | } |
| 335 | |
Ted Kremenek | 2033a95 | 2009-05-13 07:12:33 +0000 | [diff] [blame] | 336 | |