Ted Kremenek | 9457a80 | 2009-02-17 23:32:18 +0000 | [diff] [blame] | 1 | // RUN: clang -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s && |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 2 | // RUN: clang -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s && |
Ted Kremenek | 9457a80 | 2009-02-17 23:32:18 +0000 | [diff] [blame] | 3 | // RUN: clang -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s && |
| 4 | // RUN: clang -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 | |
| 15 | typedef const void * CFTypeRef; |
| 16 | typedef const struct __CFString * CFStringRef; |
| 17 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 18 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 19 | extern CFTypeRef CFRetain(CFTypeRef cf); |
| 20 | typedef const struct __CFDictionary * CFDictionaryRef; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 21 | const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 22 | extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...); |
| 23 | typedef signed char BOOL; |
| 24 | typedef int NSInteger; |
| 25 | typedef unsigned int NSUInteger; |
| 26 | @class NSString, Protocol; |
| 27 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 28 | typedef NSInteger NSComparisonResult; |
| 29 | typedef struct _NSZone NSZone; |
| 30 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 31 | @protocol NSObject |
| 32 | - (BOOL)isEqual:(id)object; |
| 33 | - (oneway void)release; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 34 | - (id)retain; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 35 | @end |
| 36 | @protocol NSCopying |
| 37 | - (id)copyWithZone:(NSZone *)zone; |
| 38 | @end |
| 39 | @protocol NSMutableCopying |
| 40 | - (id)mutableCopyWithZone:(NSZone *)zone; |
| 41 | @end |
| 42 | @protocol NSCoding |
| 43 | - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 44 | @end |
| 45 | @interface NSObject <NSObject> {} |
| 46 | - (id)init; |
| 47 | + (id)alloc; |
| 48 | @end |
| 49 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 50 | typedef struct {} NSFastEnumerationState; |
| 51 | @protocol NSFastEnumeration |
| 52 | - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; |
| 53 | @end |
| 54 | @class NSString; |
| 55 | typedef struct _NSRange {} NSRange; |
| 56 | @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> |
| 57 | - (NSUInteger)count; |
| 58 | @end |
| 59 | @interface NSMutableArray : NSArray |
| 60 | - (void)addObject:(id)anObject; |
| 61 | - (id)initWithCapacity:(NSUInteger)numItems; |
| 62 | @end |
| 63 | typedef unsigned short unichar; |
| 64 | @class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale; |
| 65 | typedef NSUInteger NSStringCompareOptions; |
| 66 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
| 67 | - (NSComparisonResult)compare:(NSString *)string; |
| 68 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask; |
| 69 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange; |
| 70 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; |
| 71 | - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string; |
| 72 | - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator; |
| 73 | @end |
| 74 | @interface NSSimpleCString : NSString {} @end |
| 75 | @interface NSConstantString : NSSimpleCString @end |
| 76 | extern void *_NSConstantStringClassReference; |
| 77 | |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | // Test cases. |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | |
| 82 | NSComparisonResult f1(NSString* s) { |
| 83 | NSString *aString = 0; |
| 84 | return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil.}} |
| 85 | } |
| 86 | |
| 87 | NSComparisonResult f2(NSString* s) { |
| 88 | NSString *aString = 0; |
| 89 | return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil.}} |
| 90 | } |
| 91 | |
| 92 | NSComparisonResult f3(NSString* s, NSStringCompareOptions op) { |
| 93 | NSString *aString = 0; |
| 94 | return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil.}} |
| 95 | } |
| 96 | |
| 97 | NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) { |
| 98 | NSString *aString = 0; |
| 99 | return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil.}} |
| 100 | } |
| 101 | |
| 102 | NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) { |
| 103 | NSString *aString = 0; |
| 104 | return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil.}} |
| 105 | } |
| 106 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 107 | NSArray *f6(NSString* s) { |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 108 | return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil.}} |
| 109 | } |
| 110 | |
| 111 | NSString* f7(NSString* s1, NSString* s2, NSString* s3) { |
| 112 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 113 | NSString* s4 = (NSString*) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 114 | CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}} |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 115 | (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"), |
| 116 | s1, s2, s3); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 117 | |
| 118 | CFRetain(s4); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 119 | return s4; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | NSMutableArray* f8() { |
| 123 | |
| 124 | NSString* s = [[NSString alloc] init]; |
| 125 | NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2]; |
| 126 | [a addObject:s]; |
| 127 | [s release]; // no-warning |
| 128 | return a; |
| 129 | } |
| 130 | |
| 131 | void f9() { |
| 132 | |
| 133 | NSString* s = [[NSString alloc] init]; |
| 134 | NSString* q = s; |
| 135 | [s release]; |
| 136 | [q release]; // expected-warning {{used after it is released}} |
| 137 | } |
| 138 | |
| 139 | NSString* f10() { |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 140 | static NSString* s = 0; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 141 | if (!s) s = [[NSString alloc] init]; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 142 | return s; // no-warning |
| 143 | } |
| 144 | |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 145 | // Test case for regression reported in <rdar://problem/6452745>. |
| 146 | // Essentially 's' should not be considered allocated on the false branch. |
| 147 | // This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp). |
| 148 | NSString* f11(CFDictionaryRef dict, const char* key) { |
| 149 | NSString* s = (NSString*) CFDictionaryGetValue(dict, key); |
| 150 | [s retain]; |
| 151 | if (s) { |
| 152 | [s release]; |
| 153 | } |
| 154 | } |
| 155 | |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 156 | // Test case for passing a tracked object by-reference to a function we |
| 157 | // don't undersand. |
| 158 | void unknown_function_f12(NSString** s); |
| 159 | void f12() { |
| 160 | NSString *string = [[NSString alloc] init]; |
| 161 | unknown_function_f12(&string); // no-warning |
| 162 | } |
| 163 | |
| 164 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 165 | @interface C1 : NSObject {} |
| 166 | - (NSString*) getShared; |
| 167 | + (C1*) sharedInstance; |
| 168 | @end |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 169 | @implementation C1 : NSObject {} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 170 | - (NSString*) getShared { |
| 171 | static NSString* s = 0; |
| 172 | if (!s) s = [[NSString alloc] init]; |
| 173 | return s; // no-warning |
| 174 | } |
| 175 | + (C1 *)sharedInstance { |
| 176 | static C1 *sharedInstance = 0; |
| 177 | if (!sharedInstance) { |
| 178 | sharedInstance = [[C1 alloc] init]; |
| 179 | } |
| 180 | return sharedInstance; // no-warning |
| 181 | } |
| 182 | @end |
| 183 | |
| 184 | @interface SharedClass : NSObject |
| 185 | + (id)sharedInstance; |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 186 | - (id)notShared; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 187 | @end |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 189 | @implementation SharedClass |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 190 | |
| 191 | - (id)_init { |
| 192 | if ((self = [super init])) { |
| 193 | NSLog(@"Bar"); |
| 194 | } |
| 195 | return self; |
| 196 | } |
| 197 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 198 | - (id)notShared { |
Ted Kremenek | 043254a | 2009-02-07 22:55:48 +0000 | [diff] [blame] | 199 | return [[SharedClass alloc] _init]; // expected-warning{{leak}} |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 202 | + (id)sharedInstance { |
| 203 | static SharedClass *_sharedInstance = 0; |
| 204 | if (!_sharedInstance) { |
| 205 | _sharedInstance = [[SharedClass alloc] _init]; |
| 206 | } |
| 207 | return _sharedInstance; // no-warning |
| 208 | } |
| 209 | @end |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 210 | |
| 211 | id testSharedClassFromFunction() { |
| 212 | return [[SharedClass alloc] _init]; // no-warning |
| 213 | } |
| 214 | |