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