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