blob: c6a05c0031ca9bda5473e95ac78b0c6528a11963 [file] [log] [blame]
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001// RUN: clang -analyze -checker-cfref -verify %s &&
Ted Kremenekb5351812009-02-17 04:27:41 +00002// RUN: clang -analyze -checker-cfref -analyzer-store=region -verify %s
Ted Kremenekfe32cc02009-01-21 06:57:53 +00003
Ted Kremenek7e904222009-01-12 21:45:02 +00004
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 including Foundation.h directly makes this test case both svelte and
11// portable to non-Mac platforms.
12//===----------------------------------------------------------------------===//
13
14typedef unsigned int __darwin_natural_t;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000015typedef struct {} div_t;
Ted Kremenek7e904222009-01-12 21:45:02 +000016typedef unsigned long UInt32;
17typedef signed long CFIndex;
18typedef const void * CFTypeRef;
19typedef const struct __CFString * CFStringRef;
20typedef const struct __CFAllocator * CFAllocatorRef;
21extern const CFAllocatorRef kCFAllocatorDefault;
22extern CFTypeRef CFRetain(CFTypeRef cf);
23extern void CFRelease(CFTypeRef cf);
24typedef struct {
25}
26CFArrayCallBacks;
27extern const CFArrayCallBacks kCFTypeArrayCallBacks;
28typedef const struct __CFArray * CFArrayRef;
29typedef struct __CFArray * CFMutableArrayRef;
30extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
31extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
Ted Kremenek3987bbe2009-03-09 22:28:18 +000032extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
Ted Kremenek7e904222009-01-12 21:45:02 +000033typedef const struct __CFDictionary * CFDictionaryRef;
34typedef UInt32 CFStringEncoding;
35enum {
36kCFStringEncodingMacRoman = 0, kCFStringEncodingWindowsLatin1 = 0x0500, kCFStringEncodingISOLatin1 = 0x0201, kCFStringEncodingNextStepLatin = 0x0B01, kCFStringEncodingASCII = 0x0600, kCFStringEncodingUnicode = 0x0100, kCFStringEncodingUTF8 = 0x08000100, kCFStringEncodingNonLossyASCII = 0x0BFF , kCFStringEncodingUTF16 = 0x0100, kCFStringEncodingUTF16BE = 0x10000100, kCFStringEncodingUTF16LE = 0x14000100, kCFStringEncodingUTF32 = 0x0c000100, kCFStringEncodingUTF32BE = 0x18000100, kCFStringEncodingUTF32LE = 0x1c000100 };
37extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
38typedef double CFTimeInterval;
39typedef CFTimeInterval CFAbsoluteTime;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000040extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
Ted Kremenek7e904222009-01-12 21:45:02 +000041typedef const struct __CFDate * CFDateRef;
42extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
43extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
44typedef __darwin_natural_t natural_t;
45typedef natural_t mach_port_name_t;
46typedef mach_port_name_t mach_port_t;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000047typedef struct {
48}
49CFRunLoopObserverContext;
Ted Kremenek7e904222009-01-12 21:45:02 +000050typedef signed char BOOL;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000051typedef unsigned int NSUInteger;
52@class NSString, Protocol;
53extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
Ted Kremenek7e904222009-01-12 21:45:02 +000054typedef struct _NSZone NSZone;
55@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
56@protocol NSObject - (BOOL)isEqual:(id)object;
57- (id)retain;
58- (oneway void)release;
Ted Kremenekf6758642009-01-28 21:20:48 +000059- (id)autorelease;
Ted Kremenek7e904222009-01-12 21:45:02 +000060@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000061@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
Ted Kremenek7e904222009-01-12 21:45:02 +000062@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
Ted Kremenek340fd2d2009-03-13 20:27:06 +000063@end
64@interface NSObject <NSObject> {}
Ted Kremenek3987bbe2009-03-09 22:28:18 +000065+ (id)alloc;
Ted Kremenek340fd2d2009-03-13 20:27:06 +000066+ (id)allocWithZone:(NSZone *)zone;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000067@end typedef float CGFloat;
68@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
69- (const char *)UTF8String;
70- (id)initWithUTF8String:(const char *)nullTerminatedCString;
71+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
72@end extern NSString * const NSCurrentLocaleDidChangeNotification ;
73@protocol NSLocking - (void)lock;
74@end extern NSString * const NSUndoManagerCheckpointNotification;
75typedef enum {
76ACL_READ_DATA = (1<<1), ACL_LIST_DIRECTORY = (1<<1), ACL_WRITE_DATA = (1<<2), ACL_ADD_FILE = (1<<2), ACL_EXECUTE = (1<<3), ACL_SEARCH = (1<<3), ACL_DELETE = (1<<4), ACL_APPEND_DATA = (1<<5), ACL_ADD_SUBDIRECTORY = (1<<5), ACL_DELETE_CHILD = (1<<6), ACL_READ_ATTRIBUTES = (1<<7), ACL_WRITE_ATTRIBUTES = (1<<8), ACL_READ_EXTATTRIBUTES = (1<<9), ACL_WRITE_EXTATTRIBUTES = (1<<10), ACL_READ_SECURITY = (1<<11), ACL_WRITE_SECURITY = (1<<12), ACL_CHANGE_OWNER = (1<<13) }
77acl_entry_id_t;
Ted Kremenek7e904222009-01-12 21:45:02 +000078typedef int kern_return_t;
79typedef kern_return_t mach_error_t;
80typedef mach_port_t io_object_t;
81typedef io_object_t io_service_t;
82typedef struct __DASession * DASessionRef;
83extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
84typedef struct __DADisk * DADiskRef;
85extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
86extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
87extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
88extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
Ted Kremenek3987bbe2009-03-09 22:28:18 +000089@interface NSResponder : NSObject <NSCoding> {
90}
91@end @class NSColor, NSFont, NSNotification;
92typedef struct __CFlags {
93}
94_CFlags;
95@interface NSCell : NSObject <NSCopying, NSCoding> {
96}
97@end @class NSDate, NSDictionary, NSError, NSException, NSNotification;
98@interface NSManagedObjectContext : NSObject <NSCoding, NSLocking> {
Ted Kremenek7e904222009-01-12 21:45:02 +000099}
100@end enum {
101kDAReturnSuccess = 0, kDAReturnError = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x01, kDAReturnBusy = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x02, kDAReturnBadArgument = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x03, kDAReturnExclusiveAccess = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x04, kDAReturnNoResources = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x05, kDAReturnNotFound = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x06, kDAReturnNotMounted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x07, kDAReturnNotPermitted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x08, kDAReturnNotPrivileged = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x09, kDAReturnNotReady = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0A, kDAReturnNotWritable = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0B, kDAReturnUnsupported = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0C };
102typedef mach_error_t DAReturn;
103typedef const struct __DADissenter * DADissenterRef;
104extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
105
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000106
107
108
Ted Kremenek7e904222009-01-12 21:45:02 +0000109//===----------------------------------------------------------------------===//
110// Test cases.
111//===----------------------------------------------------------------------===//
112
113CFAbsoluteTime f1() {
114 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
115 CFDateRef date = CFDateCreate(0, t);
116 CFRetain(date);
117 CFRelease(date);
118 CFDateGetAbsoluteTime(date); // no-warning
119 CFRelease(date);
120 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
121 return t;
122}
123
124CFAbsoluteTime f2() {
125 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
126 CFDateRef date = CFDateCreate(0, t);
127 [((NSDate*) date) retain];
128 CFRelease(date);
129 CFDateGetAbsoluteTime(date); // no-warning
130 [((NSDate*) date) release];
131 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
132 return t;
133}
134
135
136NSDate* global_x;
137
138// Test to see if we supresss an error when we store the pointer
139// to a global.
140
141CFAbsoluteTime f3() {
142 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
143 CFDateRef date = CFDateCreate(0, t);
144 [((NSDate*) date) retain];
145 CFRelease(date);
146 CFDateGetAbsoluteTime(date); // no-warning
147 global_x = (NSDate*) date;
148 [((NSDate*) date) release];
149 t = CFDateGetAbsoluteTime(date); // no-warning
150 return t;
151}
152
Ted Kremenekfe32cc02009-01-21 06:57:53 +0000153//---------------------------------------------------------------------------
154// Test case 'f4' differs for region store and basic store. See
155// retain-release-region-store.m and retain-release-basic-store.m.
156//---------------------------------------------------------------------------
Ted Kremenek7e904222009-01-12 21:45:02 +0000157
158// Test a leak.
159
160CFAbsoluteTime f5(int x) {
161 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000162 CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000163
164 if (x)
165 CFRelease(date);
166
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000167 return t;
Ted Kremenek7e904222009-01-12 21:45:02 +0000168}
169
170// Test a leak involving the return.
171
172CFDateRef f6(int x) {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000173 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000174 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000175 return date;
Ted Kremenek7e904222009-01-12 21:45:02 +0000176}
177
178// Test a leak involving an overwrite.
179
180CFDateRef f7() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000181 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); //expected-warning{{leak}}
Ted Kremenekf08ac272009-01-24 00:55:43 +0000182 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000183 date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
Ted Kremenek7e904222009-01-12 21:45:02 +0000184 return date;
185}
186
187// Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and
188// has the word create.
189CFDateRef MyDateCreate();
190
191CFDateRef f8() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000192 CFDateRef date = MyDateCreate(); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000193 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000194 return date;
Ted Kremenek7e904222009-01-12 21:45:02 +0000195}
196
197CFDateRef f9() {
198 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
199 int *p = 0;
Ted Kremenek0b891a32009-03-09 22:46:49 +0000200 // When allocations fail, CFDateCreate can return null.
201 if (!date) *p = 1; // expected-warning{{null}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000202 return date;
203}
204
205// Handle DiskArbitration API:
206//
207// http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/
208//
209void f10(io_service_t media, DADiskRef d, CFStringRef s) {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000210 DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}}
211 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000212
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000213 disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}}
214 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000215
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000216 CFDictionaryRef dict = DADiskCopyDescription(d); // expected-warning{{leak}}
217 if (dict) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000218
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000219 disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}}
220 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000221
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000222 DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000223 kDAReturnSuccess, s);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000224 if (dissenter) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000225
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000226 DASessionRef session = DASessionCreate(kCFAllocatorDefault); // expected-warning{{leak}}
227 if (session) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000228}
229
230// Test retain/release checker with CFString and CFMutableArray.
231void f11() {
232 // Create the array.
233 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
234
235 // Create a string.
236 CFStringRef s1 = CFStringCreateWithCString(0, "hello world",
237 kCFStringEncodingUTF8);
238
239 // Add the string to the array.
240 CFArrayAppendValue(A, s1);
241
242 // Decrement the reference count.
243 CFRelease(s1); // no-warning
244
245 // Get the string. We don't own it.
246 s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0);
247
248 // Release the array.
249 CFRelease(A); // no-warning
250
251 // Release the string. This is a bug.
252 CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}}
253}
254
Ted Kremenek86afde32009-01-16 18:40:33 +0000255// PR 3337: Handle functions declared using typedefs.
256typedef CFTypeRef CREATEFUN();
257CREATEFUN MyCreateFun;
258
259void f12() {
260 CFTypeRef o = MyCreateFun(); // expected-warning {{leak}}
261}
Ted Kremenekf6758642009-01-28 21:20:48 +0000262
263void f13_autorelease() {
264 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
265 [(id) A autorelease]; // no-warning
266}
Ted Kremenekbea465ae2009-02-19 18:20:28 +0000267
268// This case exercises the logic where the leak site is the same as the allocation site.
269void f14_leakimmediately() {
270 CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
271}
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000272
273// Test basic tracking of ivars associated with 'self'. For the retain/release
274// checker we currently do not want to flag leaks associated with stores
275// of tracked objects to ivars.
276@interface SelfIvarTest : NSObject {
277 id myObj;
278}
279- (void)test_self_tracking;
280@end
281
282@implementation SelfIvarTest
283- (void)test_self_tracking {
284 myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
285}
286@end
287
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000288// <rdar://problem/6659160>
289int isFoo(char c);
290
291static void rdar_6659160(char *inkind, char *inname)
292{
293 // We currently expect that [NSObject alloc] cannot fail. This
294 // will be a toggled flag in the future. It can indeed return null, but
295 // Cocoa programmers generally aren't expected to reason about out-of-memory
296 // conditions.
297 NSString *kind = [[NSString alloc] initWithUTF8String:inkind]; // expected-warning{{leak}}
298
299 // We do allow stringWithUTF8String to fail. This isn't really correct, as
300 // far as returning nil. In most error conditions it will throw an exception.
301 // If allocation fails it could return nil, but again this
302 // isn't expected.
303 NSString *name = [NSString stringWithUTF8String:inname];
304 if(!name)
305 return;
306
307 const char *kindC = 0;
308 const char *nameC = 0;
309
310 // In both cases, we cannot reach a point down below where we
311 // dereference kindC or nameC with either being null. This is because
312 // we assume that [NSObject alloc] doesn't fail and that we have the guard
313 // up above.
314
315 if(kind)
316 kindC = [kind UTF8String];
317 if(name)
318 nameC = [name UTF8String];
Ted Kremenek0b891a32009-03-09 22:46:49 +0000319 if(!isFoo(kindC[0])) // expected-warning{{null}}
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000320 return;
321 if(!isFoo(nameC[0])) // no-warning
322 return;
323
324 [kind release];
325 [name release];
326}
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000327
Ted Kremenek340fd2d2009-03-13 20:27:06 +0000328// PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming
329// conventions with respect to 'return'ing ownership.
330@interface PR3677: NSObject @end
331@implementation PR3677
332+ (id)allocWithZone:(NSZone *)inZone {
333 return [super allocWithZone:inZone]; // no-warning
334}
335@end
336