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 | e1cea75 | 2009-07-06 21:58:46 +0000 | [diff] [blame] | 2 | // 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] | 3 | // RUN: clang-cc -triple i386-pc-linux-gnu -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s && |
| 4 | // 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] | 5 | |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | // The following code is reduced using delta-debugging from |
| 8 | // Foundation.h (Mac OS X). |
| 9 | // |
| 10 | // It includes the basic definitions for the test cases below. |
| 11 | // Not directly including Foundation.h directly makes this test case |
| 12 | // both svelte and portable to non-Mac platforms. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | a3f4540 | 2009-04-29 16:03:59 +0000 | [diff] [blame] | 15 | typedef int int32_t; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 16 | typedef const void * CFTypeRef; |
| 17 | typedef const struct __CFString * CFStringRef; |
| 18 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 19 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 20 | extern CFTypeRef CFRetain(CFTypeRef cf); |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame] | 21 | void CFRelease(CFTypeRef cf); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 22 | typedef const struct __CFDictionary * CFDictionaryRef; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 23 | const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 24 | extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...); |
| 25 | typedef signed char BOOL; |
| 26 | typedef int NSInteger; |
| 27 | typedef unsigned int NSUInteger; |
| 28 | @class NSString, Protocol; |
| 29 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 30 | typedef NSInteger NSComparisonResult; |
| 31 | typedef struct _NSZone NSZone; |
| 32 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 33 | @protocol NSObject |
| 34 | - (BOOL)isEqual:(id)object; |
| 35 | - (oneway void)release; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 36 | - (id)retain; |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 37 | - (id)autorelease; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 38 | @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 |
| 52 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 53 | typedef struct {} NSFastEnumerationState; |
| 54 | @protocol NSFastEnumeration |
| 55 | - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; |
| 56 | @end |
| 57 | @class NSString; |
| 58 | typedef 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 |
| 66 | typedef unsigned short unichar; |
| 67 | @class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale; |
| 68 | typedef 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 Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 76 | + (id)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2))); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 77 | @end |
| 78 | @interface NSSimpleCString : NSString {} @end |
| 79 | @interface NSConstantString : NSSimpleCString @end |
| 80 | extern void *_NSConstantStringClassReference; |
| 81 | |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | // Test cases. |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
| 86 | NSComparisonResult f1(NSString* s) { |
| 87 | NSString *aString = 0; |
| 88 | return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil.}} |
| 89 | } |
| 90 | |
| 91 | NSComparisonResult f2(NSString* s) { |
| 92 | NSString *aString = 0; |
| 93 | return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil.}} |
| 94 | } |
| 95 | |
| 96 | NSComparisonResult 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 | |
| 101 | NSComparisonResult 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 | |
| 106 | NSComparisonResult 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 Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 111 | NSArray *f6(NSString* s) { |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 112 | return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil.}} |
| 113 | } |
| 114 | |
| 115 | NSString* f7(NSString* s1, NSString* s2, NSString* s3) { |
| 116 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 117 | NSString* s4 = (NSString*) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 118 | CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}} |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 119 | (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"), |
| 120 | s1, s2, s3); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 121 | |
| 122 | CFRetain(s4); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 123 | return s4; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | NSMutableArray* 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 | |
| 135 | void 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 | |
| 143 | NSString* f10() { |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 144 | static NSString* s = 0; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 145 | if (!s) s = [[NSString alloc] init]; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 146 | return s; // no-warning |
| 147 | } |
| 148 | |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 149 | // 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). |
| 152 | NSString* f11(CFDictionaryRef dict, const char* key) { |
| 153 | NSString* s = (NSString*) CFDictionaryGetValue(dict, key); |
| 154 | [s retain]; |
| 155 | if (s) { |
| 156 | [s release]; |
| 157 | } |
Mike Stump | 4393b3f | 2009-07-21 18:46:15 +0000 | [diff] [blame] | 158 | return 0; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 161 | // 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] | 162 | // don't understand. |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 163 | void unknown_function_f12(NSString** s); |
| 164 | void f12() { |
| 165 | NSString *string = [[NSString alloc] init]; |
| 166 | unknown_function_f12(&string); // no-warning |
| 167 | } |
| 168 | |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame] | 169 | // Test double release of CFString (PR 4014). |
| 170 | void 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 Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 176 | // Test regular use of -autorelease |
| 177 | @interface TestAutorelease |
| 178 | -(NSString*) getString; |
| 179 | @end |
| 180 | @implementation TestAutorelease |
| 181 | -(NSString*) getString { |
| 182 | NSString *str = [[NSString alloc] init]; |
| 183 | return [str autorelease]; // no-warning |
| 184 | } |
Ted Kremenek | 2033a95 | 2009-05-13 07:12:33 +0000 | [diff] [blame] | 185 | - (void)m1 |
| 186 | { |
| 187 | NSString *s = [[NSString alloc] init]; // expected-warning{{leak}} |
| 188 | [s retain]; |
| 189 | [s autorelease]; |
| 190 | } |
| 191 | - (void)m2 |
| 192 | { |
| 193 | NSString *s = [[[NSString alloc] init] autorelease]; // expected-warning{{leak}} |
| 194 | [s retain]; |
| 195 | } |
| 196 | - (void)m3 |
| 197 | { |
| 198 | NSString *s = [[[NSString alloc] init] autorelease]; |
| 199 | [s retain]; |
| 200 | [s autorelease]; |
| 201 | } |
| 202 | - (void)m4 |
| 203 | { |
| 204 | NSString *s = [[NSString alloc] init]; // expected-warning{{leak}} |
| 205 | [s retain]; |
| 206 | } |
| 207 | - (void)m5 |
| 208 | { |
| 209 | NSString *s = [[NSString alloc] init]; |
| 210 | [s autorelease]; |
| 211 | } |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 212 | @end |
| 213 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 214 | @interface C1 : NSObject {} |
| 215 | - (NSString*) getShared; |
| 216 | + (C1*) sharedInstance; |
| 217 | @end |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 218 | @implementation C1 : NSObject {} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 219 | - (NSString*) getShared { |
| 220 | static NSString* s = 0; |
| 221 | if (!s) s = [[NSString alloc] init]; |
| 222 | return s; // no-warning |
| 223 | } |
| 224 | + (C1 *)sharedInstance { |
| 225 | static C1 *sharedInstance = 0; |
| 226 | if (!sharedInstance) { |
| 227 | sharedInstance = [[C1 alloc] init]; |
| 228 | } |
| 229 | return sharedInstance; // no-warning |
| 230 | } |
| 231 | @end |
| 232 | |
| 233 | @interface SharedClass : NSObject |
| 234 | + (id)sharedInstance; |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 235 | - (id)notShared; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 236 | @end |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 238 | @implementation SharedClass |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 239 | |
| 240 | - (id)_init { |
| 241 | if ((self = [super init])) { |
| 242 | NSLog(@"Bar"); |
| 243 | } |
| 244 | return self; |
| 245 | } |
| 246 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 247 | - (id)notShared { |
Ted Kremenek | 043254a | 2009-02-07 22:55:48 +0000 | [diff] [blame] | 248 | return [[SharedClass alloc] _init]; // expected-warning{{leak}} |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 251 | + (id)sharedInstance { |
| 252 | static SharedClass *_sharedInstance = 0; |
| 253 | if (!_sharedInstance) { |
| 254 | _sharedInstance = [[SharedClass alloc] _init]; |
| 255 | } |
| 256 | return _sharedInstance; // no-warning |
| 257 | } |
| 258 | @end |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 259 | |
| 260 | id testSharedClassFromFunction() { |
| 261 | return [[SharedClass alloc] _init]; // no-warning |
| 262 | } |
| 263 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 264 | // Test OSCompareAndSwap |
| 265 | _Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue ); |
Ted Kremenek | 9810472 | 2009-05-01 17:37:31 +0000 | [diff] [blame] | 266 | _Bool OSAtomicCompareAndSwap32Barrier( int32_t __oldValue, int32_t __newValue, volatile int32_t *__theValue ); |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 267 | extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 268 | |
| 269 | void testOSCompareAndSwap() { |
| 270 | NSString *old = 0; |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 271 | NSString *s = [[NSString alloc] init]; // no-warning |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 272 | if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old)) |
| 273 | [s release]; |
| 274 | else |
| 275 | [old release]; |
| 276 | } |
| 277 | |
Ted Kremenek | a3f4540 | 2009-04-29 16:03:59 +0000 | [diff] [blame] | 278 | void testOSCompareAndSwap32Barrier() { |
| 279 | NSString *old = 0; |
| 280 | NSString *s = [[NSString alloc] init]; // no-warning |
| 281 | if (!OSAtomicCompareAndSwap32Barrier((int32_t) 0, (int32_t) s, (int32_t*) &old)) |
| 282 | [s release]; |
| 283 | else |
| 284 | [old release]; |
| 285 | } |
| 286 | |
Ted Kremenek | 0aeaf5a | 2009-07-29 18:18:25 +0000 | [diff] [blame] | 287 | int testOSCompareAndSwap32Barrier_id(Class myclass, id xclass) { |
| 288 | if (OSAtomicCompareAndSwap32Barrier(0, (int32_t) myclass, (int32_t*) &xclass)) |
| 289 | return 1; |
| 290 | return 0; |
| 291 | } |
| 292 | |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 293 | void test_objc_atomicCompareAndSwap() { |
| 294 | NSString *old = 0; |
| 295 | NSString *s = [[NSString alloc] init]; // no-warning |
| 296 | if (!objc_atomicCompareAndSwapPtr(0, s, &old)) |
| 297 | [s release]; |
| 298 | else |
| 299 | [old release]; |
| 300 | } |
| 301 | |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 302 | // Test stringWithFormat (<rdar://problem/6815234>) |
| 303 | void test_stringWithFormat() { |
| 304 | NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain]; |
| 305 | [string release]; |
Ted Kremenek | f9df136 | 2009-04-23 21:25:57 +0000 | [diff] [blame] | 306 | [string release]; // expected-warning{{Incorrect decrement of the reference count}} |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 309 | // Test isTrackedObjectType(). |
Ted Kremenek | 97d095f | 2009-04-23 22:11:07 +0000 | [diff] [blame] | 310 | typedef NSString* WonkyTypedef; |
| 311 | @interface TestIsTracked |
| 312 | + (WonkyTypedef)newString; |
| 313 | @end |
| 314 | |
| 315 | void test_isTrackedObjectType(void) { |
| 316 | NSString *str = [TestIsTracked newString]; // expected-warning{{Potential leak}} |
| 317 | } |
Ted Kremenek | b3b0b36 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 318 | |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 319 | // Test isTrackedCFObjectType(). |
| 320 | @interface TestIsCFTracked |
| 321 | + (CFStringRef) badNewCFString; |
| 322 | + (CFStringRef) newCFString; |
| 323 | @end |
| 324 | |
| 325 | @implementation TestIsCFTracked |
| 326 | + (CFStringRef) newCFString { |
| 327 | return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // no-warning |
| 328 | } |
| 329 | + (CFStringRef) badNewCFString { |
| 330 | return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // expected-warning{{leak}} |
| 331 | } |
| 332 | |
Ted Kremenek | b3b0b36 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 333 | // Test @synchronized |
| 334 | void test_synchronized(id x) { |
| 335 | @synchronized(x) { |
| 336 | NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain]; // expected-warning {{leak}} |
| 337 | } |
| 338 | } |
| 339 | |
Ted Kremenek | 2033a95 | 2009-05-13 07:12:33 +0000 | [diff] [blame] | 340 | |