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