Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s && |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 2 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s |
| 3 | |
| 4 | |
| 5 | // NOTWORK: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s && |
| 6 | // NOTWORK: clang-cc -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 | |
| 17 | typedef const void * CFTypeRef; |
| 18 | typedef const struct __CFString * CFStringRef; |
| 19 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 20 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 21 | extern CFTypeRef CFRetain(CFTypeRef cf); |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame^] | 22 | void CFRelease(CFTypeRef cf); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 23 | typedef const struct __CFDictionary * CFDictionaryRef; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 24 | const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 25 | extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...); |
| 26 | typedef signed char BOOL; |
| 27 | typedef int NSInteger; |
| 28 | typedef unsigned int NSUInteger; |
| 29 | @class NSString, Protocol; |
| 30 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 31 | typedef NSInteger NSComparisonResult; |
| 32 | typedef struct _NSZone NSZone; |
| 33 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 34 | @protocol NSObject |
| 35 | - (BOOL)isEqual:(id)object; |
| 36 | - (oneway void)release; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 37 | - (id)retain; |
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; |
| 76 | @end |
| 77 | @interface NSSimpleCString : NSString {} @end |
| 78 | @interface NSConstantString : NSSimpleCString @end |
| 79 | extern void *_NSConstantStringClassReference; |
| 80 | |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | // Test cases. |
| 83 | //===----------------------------------------------------------------------===// |
| 84 | |
| 85 | NSComparisonResult f1(NSString* s) { |
| 86 | NSString *aString = 0; |
| 87 | return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil.}} |
| 88 | } |
| 89 | |
| 90 | NSComparisonResult f2(NSString* s) { |
| 91 | NSString *aString = 0; |
| 92 | return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil.}} |
| 93 | } |
| 94 | |
| 95 | NSComparisonResult f3(NSString* s, NSStringCompareOptions op) { |
| 96 | NSString *aString = 0; |
| 97 | return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil.}} |
| 98 | } |
| 99 | |
| 100 | NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) { |
| 101 | NSString *aString = 0; |
| 102 | return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil.}} |
| 103 | } |
| 104 | |
| 105 | NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) { |
| 106 | NSString *aString = 0; |
| 107 | return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil.}} |
| 108 | } |
| 109 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 110 | NSArray *f6(NSString* s) { |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 111 | return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil.}} |
| 112 | } |
| 113 | |
| 114 | NSString* f7(NSString* s1, NSString* s2, NSString* s3) { |
| 115 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 116 | NSString* s4 = (NSString*) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 117 | CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}} |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 118 | (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"), |
| 119 | s1, s2, s3); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 120 | |
| 121 | CFRetain(s4); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 122 | return s4; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | NSMutableArray* f8() { |
| 126 | |
| 127 | NSString* s = [[NSString alloc] init]; |
| 128 | NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2]; |
| 129 | [a addObject:s]; |
| 130 | [s release]; // no-warning |
| 131 | return a; |
| 132 | } |
| 133 | |
| 134 | void f9() { |
| 135 | |
| 136 | NSString* s = [[NSString alloc] init]; |
| 137 | NSString* q = s; |
| 138 | [s release]; |
| 139 | [q release]; // expected-warning {{used after it is released}} |
| 140 | } |
| 141 | |
| 142 | NSString* f10() { |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 143 | static NSString* s = 0; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 144 | if (!s) s = [[NSString alloc] init]; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 145 | return s; // no-warning |
| 146 | } |
| 147 | |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 148 | // Test case for regression reported in <rdar://problem/6452745>. |
| 149 | // Essentially 's' should not be considered allocated on the false branch. |
| 150 | // This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp). |
| 151 | NSString* f11(CFDictionaryRef dict, const char* key) { |
| 152 | NSString* s = (NSString*) CFDictionaryGetValue(dict, key); |
| 153 | [s retain]; |
| 154 | if (s) { |
| 155 | [s release]; |
| 156 | } |
| 157 | } |
| 158 | |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 159 | // 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^] | 160 | // don't understand. |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 161 | void unknown_function_f12(NSString** s); |
| 162 | void f12() { |
| 163 | NSString *string = [[NSString alloc] init]; |
| 164 | unknown_function_f12(&string); // no-warning |
| 165 | } |
| 166 | |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame^] | 167 | // Test double release of CFString (PR 4014). |
| 168 | void f13(void) { |
| 169 | CFStringRef ref = CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); |
| 170 | CFRelease(ref); |
| 171 | CFRelease(ref); // expected-warning{{Reference-counted object is used after it is released}} |
| 172 | } |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 174 | @interface C1 : NSObject {} |
| 175 | - (NSString*) getShared; |
| 176 | + (C1*) sharedInstance; |
| 177 | @end |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 178 | @implementation C1 : NSObject {} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 179 | - (NSString*) getShared { |
| 180 | static NSString* s = 0; |
| 181 | if (!s) s = [[NSString alloc] init]; |
| 182 | return s; // no-warning |
| 183 | } |
| 184 | + (C1 *)sharedInstance { |
| 185 | static C1 *sharedInstance = 0; |
| 186 | if (!sharedInstance) { |
| 187 | sharedInstance = [[C1 alloc] init]; |
| 188 | } |
| 189 | return sharedInstance; // no-warning |
| 190 | } |
| 191 | @end |
| 192 | |
| 193 | @interface SharedClass : NSObject |
| 194 | + (id)sharedInstance; |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 195 | - (id)notShared; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 196 | @end |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 198 | @implementation SharedClass |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 199 | |
| 200 | - (id)_init { |
| 201 | if ((self = [super init])) { |
| 202 | NSLog(@"Bar"); |
| 203 | } |
| 204 | return self; |
| 205 | } |
| 206 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 207 | - (id)notShared { |
Ted Kremenek | 043254a | 2009-02-07 22:55:48 +0000 | [diff] [blame] | 208 | return [[SharedClass alloc] _init]; // expected-warning{{leak}} |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 211 | + (id)sharedInstance { |
| 212 | static SharedClass *_sharedInstance = 0; |
| 213 | if (!_sharedInstance) { |
| 214 | _sharedInstance = [[SharedClass alloc] _init]; |
| 215 | } |
| 216 | return _sharedInstance; // no-warning |
| 217 | } |
| 218 | @end |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 219 | |
| 220 | id testSharedClassFromFunction() { |
| 221 | return [[SharedClass alloc] _init]; // no-warning |
| 222 | } |
| 223 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 224 | // Test OSCompareAndSwap |
| 225 | _Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue ); |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 226 | extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 227 | |
| 228 | void testOSCompareAndSwap() { |
| 229 | NSString *old = 0; |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 230 | NSString *s = [[NSString alloc] init]; // no-warning |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 231 | if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old)) |
| 232 | [s release]; |
| 233 | else |
| 234 | [old release]; |
| 235 | } |
| 236 | |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 237 | void test_objc_atomicCompareAndSwap() { |
| 238 | NSString *old = 0; |
| 239 | NSString *s = [[NSString alloc] init]; // no-warning |
| 240 | if (!objc_atomicCompareAndSwapPtr(0, s, &old)) |
| 241 | [s release]; |
| 242 | else |
| 243 | [old release]; |
| 244 | } |
| 245 | |