Ted Kremenek | 42adacb | 2012-10-11 20:58:21 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,alpha.core -analyzer-store=region -analyzer-constraints=range -verify -Wno-objc-root-class %s |
| 2 | // RUN: %clang_cc1 -DTEST_64 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,alpha.core -analyzer-store=region -analyzer-constraints=range -verify -Wno-objc-root-class %s |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 3 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 4 | |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | // The following code is reduced using delta-debugging from |
| 7 | // Foundation.h (Mac OS X). |
| 8 | // |
| 9 | // It includes the basic definitions for the test cases below. |
| 10 | // Not directly including Foundation.h directly makes this test case |
| 11 | // both svelte and portable to non-Mac platforms. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 14 | #ifdef TEST_64 |
| 15 | typedef long long int64_t; |
| 16 | _Bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue ); |
| 17 | #define COMPARE_SWAP_BARRIER OSAtomicCompareAndSwap64Barrier |
| 18 | typedef int64_t intptr_t; |
| 19 | #else |
Ted Kremenek | a3f4540 | 2009-04-29 16:03:59 +0000 | [diff] [blame] | 20 | typedef int int32_t; |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 21 | _Bool OSAtomicCompareAndSwap32Barrier( int32_t __oldValue, int32_t __newValue, volatile int32_t *__theValue ); |
| 22 | #define COMPARE_SWAP_BARRIER OSAtomicCompareAndSwap32Barrier |
| 23 | typedef int32_t intptr_t; |
| 24 | #endif |
| 25 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 26 | typedef const void * CFTypeRef; |
| 27 | typedef const struct __CFString * CFStringRef; |
| 28 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 29 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 30 | extern CFTypeRef CFRetain(CFTypeRef cf); |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame] | 31 | void CFRelease(CFTypeRef cf); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 32 | typedef const struct __CFDictionary * CFDictionaryRef; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 33 | const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 34 | extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...); |
| 35 | typedef signed char BOOL; |
| 36 | typedef int NSInteger; |
| 37 | typedef unsigned int NSUInteger; |
| 38 | @class NSString, Protocol; |
| 39 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 40 | typedef NSInteger NSComparisonResult; |
| 41 | typedef struct _NSZone NSZone; |
| 42 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 43 | @protocol NSObject |
| 44 | - (BOOL)isEqual:(id)object; |
| 45 | - (oneway void)release; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 46 | - (id)retain; |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 47 | - (id)autorelease; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 48 | @end |
| 49 | @protocol NSCopying |
| 50 | - (id)copyWithZone:(NSZone *)zone; |
| 51 | @end |
| 52 | @protocol NSMutableCopying |
| 53 | - (id)mutableCopyWithZone:(NSZone *)zone; |
| 54 | @end |
| 55 | @protocol NSCoding |
| 56 | - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 57 | @end |
| 58 | @interface NSObject <NSObject> {} |
| 59 | - (id)init; |
| 60 | + (id)alloc; |
| 61 | @end |
| 62 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 63 | typedef struct {} NSFastEnumerationState; |
| 64 | @protocol NSFastEnumeration |
| 65 | - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; |
| 66 | @end |
| 67 | @class NSString; |
| 68 | typedef struct _NSRange {} NSRange; |
| 69 | @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> |
| 70 | - (NSUInteger)count; |
| 71 | @end |
| 72 | @interface NSMutableArray : NSArray |
| 73 | - (void)addObject:(id)anObject; |
| 74 | - (id)initWithCapacity:(NSUInteger)numItems; |
| 75 | @end |
| 76 | typedef unsigned short unichar; |
| 77 | @class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale; |
| 78 | typedef NSUInteger NSStringCompareOptions; |
| 79 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
| 80 | - (NSComparisonResult)compare:(NSString *)string; |
| 81 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask; |
| 82 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange; |
| 83 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; |
| 84 | - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string; |
| 85 | - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator; |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 86 | + (id)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2))); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 87 | @end |
| 88 | @interface NSSimpleCString : NSString {} @end |
| 89 | @interface NSConstantString : NSSimpleCString @end |
| 90 | extern void *_NSConstantStringClassReference; |
| 91 | |
| 92 | //===----------------------------------------------------------------------===// |
| 93 | // Test cases. |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | |
| 96 | NSComparisonResult f1(NSString* s) { |
| 97 | NSString *aString = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 98 | return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | NSComparisonResult f2(NSString* s) { |
| 102 | NSString *aString = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 103 | return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | NSComparisonResult f3(NSString* s, NSStringCompareOptions op) { |
| 107 | NSString *aString = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 108 | return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) { |
| 112 | NSString *aString = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 113 | return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) { |
| 117 | NSString *aString = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 118 | return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 121 | NSArray *f6(NSString* s) { |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 122 | return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | NSString* f7(NSString* s1, NSString* s2, NSString* s3) { |
| 126 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 127 | NSString* s4 = (NSString*) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 128 | CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}} |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 129 | (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"), |
| 130 | s1, s2, s3); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 131 | |
| 132 | CFRetain(s4); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 133 | return s4; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | NSMutableArray* f8() { |
| 137 | |
| 138 | NSString* s = [[NSString alloc] init]; |
| 139 | NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2]; |
| 140 | [a addObject:s]; |
| 141 | [s release]; // no-warning |
| 142 | return a; |
| 143 | } |
| 144 | |
| 145 | void f9() { |
| 146 | |
| 147 | NSString* s = [[NSString alloc] init]; |
| 148 | NSString* q = s; |
| 149 | [s release]; |
| 150 | [q release]; // expected-warning {{used after it is released}} |
| 151 | } |
| 152 | |
| 153 | NSString* f10() { |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 154 | static NSString* s = 0; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 155 | if (!s) s = [[NSString alloc] init]; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 156 | return s; // no-warning |
| 157 | } |
| 158 | |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 159 | // Test case for regression reported in <rdar://problem/6452745>. |
| 160 | // Essentially 's' should not be considered allocated on the false branch. |
| 161 | // This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp). |
| 162 | NSString* f11(CFDictionaryRef dict, const char* key) { |
| 163 | NSString* s = (NSString*) CFDictionaryGetValue(dict, key); |
| 164 | [s retain]; |
| 165 | if (s) { |
| 166 | [s release]; |
| 167 | } |
Mike Stump | 4393b3f | 2009-07-21 18:46:15 +0000 | [diff] [blame] | 168 | return 0; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 171 | // 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] | 172 | // don't understand. |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 173 | void unknown_function_f12(NSString** s); |
| 174 | void f12() { |
| 175 | NSString *string = [[NSString alloc] init]; |
| 176 | unknown_function_f12(&string); // no-warning |
| 177 | } |
| 178 | |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame] | 179 | // Test double release of CFString (PR 4014). |
| 180 | void f13(void) { |
| 181 | CFStringRef ref = CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); |
| 182 | CFRelease(ref); |
| 183 | CFRelease(ref); // expected-warning{{Reference-counted object is used after it is released}} |
| 184 | } |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 185 | |
Anders Carlsson | b62bdce | 2011-03-08 20:05:26 +0000 | [diff] [blame] | 186 | @interface MyString : NSString |
| 187 | @end |
| 188 | |
| 189 | void f14(MyString *s) { |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 190 | [s compare:0]; // expected-warning {{Argument to 'MyString' method 'compare:' cannot be nil}} |
Anders Carlsson | b62bdce | 2011-03-08 20:05:26 +0000 | [diff] [blame] | 191 | } |
| 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 | } |
Anna Zaks | 453cb85 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 407 | |
| 408 | @interface AlwaysInlineBodyFarmBodies : NSObject { |
| 409 | NSString *_value; |
| 410 | } |
| 411 | - (NSString *)_value; |
| 412 | - (void)callValue; |
| 413 | @end |
| 414 | |
| 415 | @implementation AlwaysInlineBodyFarmBodies |
| 416 | |
| 417 | - (NSString *)_value { |
| 418 | if (!_value) { |
| 419 | NSString *s = [[NSString alloc] init]; |
| 420 | if (!OSAtomicCompareAndSwapPtr(0, s, (void**)&_value)) { |
| 421 | [s release]; |
| 422 | } |
| 423 | } |
| 424 | return _value; |
| 425 | } |
| 426 | |
| 427 | - (void)callValue { |
| 428 | [self _value]; |
| 429 | } |
| 430 | @end |