blob: ed9f66170479f2769d14d3c915f5e88059fb7b6d [file] [log] [blame]
Daniel Dunbara45cf5b2009-03-24 02:24:46 +00001// RUN: clang-cc -analyze -checker-cfref -verify %s &&
2// RUN: clang-cc -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 Kremenekebc6d912009-04-29 00:41:31 +000015typedef unsigned int UInt32;
Ted Kremenek7e904222009-01-12 21:45:02 +000016typedef signed long CFIndex;
17typedef const void * CFTypeRef;
18typedef const struct __CFString * CFStringRef;
19typedef const struct __CFAllocator * CFAllocatorRef;
20extern const CFAllocatorRef kCFAllocatorDefault;
21extern CFTypeRef CFRetain(CFTypeRef cf);
22extern void CFRelease(CFTypeRef cf);
23typedef struct {
24}
25CFArrayCallBacks;
26extern const CFArrayCallBacks kCFTypeArrayCallBacks;
27typedef const struct __CFArray * CFArrayRef;
28typedef struct __CFArray * CFMutableArrayRef;
29extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
30extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
Ted Kremenek3987bbe2009-03-09 22:28:18 +000031extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
Ted Kremenek7e904222009-01-12 21:45:02 +000032typedef const struct __CFDictionary * CFDictionaryRef;
33typedef UInt32 CFStringEncoding;
34enum {
35kCFStringEncodingMacRoman = 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 };
36extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
37typedef double CFTimeInterval;
38typedef CFTimeInterval CFAbsoluteTime;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000039extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
Ted Kremenek7e904222009-01-12 21:45:02 +000040typedef const struct __CFDate * CFDateRef;
41extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
42extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
43typedef __darwin_natural_t natural_t;
44typedef natural_t mach_port_name_t;
45typedef mach_port_name_t mach_port_t;
Ted Kremenekebc6d912009-04-29 00:41:31 +000046typedef int kern_return_t;
47typedef kern_return_t mach_error_t;
48typedef struct objc_selector *SEL;
Ted Kremenek7e904222009-01-12 21:45:02 +000049typedef signed char BOOL;
Ted Kremenekebc6d912009-04-29 00:41:31 +000050typedef unsigned long NSUInteger;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000051@class NSString, Protocol;
52extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
Ted Kremenek7e904222009-01-12 21:45:02 +000053typedef struct _NSZone NSZone;
54@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
55@protocol NSObject - (BOOL)isEqual:(id)object;
56- (id)retain;
57- (oneway void)release;
Ted Kremenekf6758642009-01-28 21:20:48 +000058- (id)autorelease;
Ted Kremenek7e904222009-01-12 21:45:02 +000059@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000060@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
Ted Kremenek7e904222009-01-12 21:45:02 +000061@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
Ted Kremenekebc6d912009-04-29 00:41:31 +000062@end @interface NSObject <NSObject> {
63}
Ted Kremenek340fd2d2009-03-13 20:27:06 +000064+ (id)allocWithZone:(NSZone *)zone;
Ted Kremenekebc6d912009-04-29 00:41:31 +000065+ (id)alloc;
66- (void)dealloc;
67@end extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
68typedef struct {
69}
70NSFastEnumerationState;
71@protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
72@end @class NSString, NSDictionary;
73typedef double NSTimeInterval;
74@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate;
75@end typedef unsigned short unichar;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000076@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
Ted Kremenekebc6d912009-04-29 00:41:31 +000077- ( const char *)UTF8String;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000078- (id)initWithUTF8String:(const char *)nullTerminatedCString;
79+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
Ted Kremenekebc6d912009-04-29 00:41:31 +000080@end @class NSDictionary;
81@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count;
82@end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey;
83- (void)setObject:(id)anObject forKey:(id)aKey;
84@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems;
85@end @class NSString, NSDictionary, NSArray;
Ted Kremenek7e904222009-01-12 21:45:02 +000086typedef mach_port_t io_object_t;
87typedef io_object_t io_service_t;
88typedef struct __DASession * DASessionRef;
89extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
90typedef struct __DADisk * DADiskRef;
91extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
92extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
93extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
94extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
Ted Kremenekebc6d912009-04-29 00:41:31 +000095@interface NSTask : NSObject - (id)init;
96@end extern NSString * const NSTaskDidTerminateNotification;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000097@interface NSResponder : NSObject <NSCoding> {
Ted Kremenekebc6d912009-04-29 00:41:31 +000098struct __vaFlags {
99}
100_vaFlags;
101}
102@end @protocol NSAnimatablePropertyContainer - (id)animator;
103@end extern NSString *NSAnimationTriggerOrderIn ;
104@class NSBitmapImageRep, NSCursor, NSGraphicsContext, NSImage, NSPasteboard, NSScrollView, NSTextInputContext, NSWindow, NSAttributedString;
105@interface NSView : NSResponder <NSAnimatablePropertyContainer> {
106struct __VFlags2 {
107}
108_vFlags2;
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000109}
110@end @class NSColor, NSFont, NSNotification;
Ted Kremenekebc6d912009-04-29 00:41:31 +0000111@interface NSTextTab : NSObject <NSCopying, NSCoding> {
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000112}
Ted Kremenekebc6d912009-04-29 00:41:31 +0000113@end @protocol NSValidatedUserInterfaceItem - (SEL)action;
114@end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
115@end @class NSArray, NSError, NSImage, NSView, NSNotificationCenter, NSURL, NSScreen, NSRunningApplication;
116@interface NSApplication : NSResponder <NSUserInterfaceValidations> {
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000117}
Ted Kremenekebc6d912009-04-29 00:41:31 +0000118@end enum {
119NSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 };
120typedef NSUInteger NSApplicationTerminateReply;
121@protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
122@end enum {
123NSUserInterfaceLayoutDirectionLeftToRight = 0, NSUserInterfaceLayoutDirectionRightToLeft = 1 };
124@interface NSManagedObject : NSObject {
Ted Kremenek7e904222009-01-12 21:45:02 +0000125}
126@end enum {
127kDAReturnSuccess = 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 };
128typedef mach_error_t DAReturn;
129typedef const struct __DADissenter * DADissenterRef;
130extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
131
Ted Kremenek7e7ed522009-05-11 17:45:06 +0000132 @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
133 - (NSUInteger)count;
134 + (id)array;
135 @end
136
137@interface NSAutoreleasePool : NSObject {}
138+ (void)addObject:(id)anObject;
139- (void)addObject:(id)anObject;
140- (void)drain;
141@end
142
Ted Kremenekf9fa3cb2009-05-14 21:29:16 +0000143@interface NSData : NSObject {}
144+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
145+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
146@end
147
Ted Kremenek7e904222009-01-12 21:45:02 +0000148//===----------------------------------------------------------------------===//
149// Test cases.
150//===----------------------------------------------------------------------===//
151
152CFAbsoluteTime f1() {
153 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
154 CFDateRef date = CFDateCreate(0, t);
155 CFRetain(date);
156 CFRelease(date);
157 CFDateGetAbsoluteTime(date); // no-warning
158 CFRelease(date);
159 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
160 return t;
161}
162
163CFAbsoluteTime f2() {
164 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
165 CFDateRef date = CFDateCreate(0, t);
166 [((NSDate*) date) retain];
167 CFRelease(date);
168 CFDateGetAbsoluteTime(date); // no-warning
169 [((NSDate*) date) release];
170 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
171 return t;
172}
173
174
175NSDate* global_x;
176
177// Test to see if we supresss an error when we store the pointer
178// to a global.
179
180CFAbsoluteTime f3() {
181 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
182 CFDateRef date = CFDateCreate(0, t);
183 [((NSDate*) date) retain];
184 CFRelease(date);
185 CFDateGetAbsoluteTime(date); // no-warning
186 global_x = (NSDate*) date;
187 [((NSDate*) date) release];
188 t = CFDateGetAbsoluteTime(date); // no-warning
189 return t;
190}
191
Ted Kremenekfe32cc02009-01-21 06:57:53 +0000192//---------------------------------------------------------------------------
193// Test case 'f4' differs for region store and basic store. See
194// retain-release-region-store.m and retain-release-basic-store.m.
195//---------------------------------------------------------------------------
Ted Kremenek7e904222009-01-12 21:45:02 +0000196
197// Test a leak.
198
199CFAbsoluteTime f5(int x) {
200 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000201 CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000202
203 if (x)
204 CFRelease(date);
205
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000206 return t;
Ted Kremenek7e904222009-01-12 21:45:02 +0000207}
208
209// Test a leak involving the return.
210
211CFDateRef f6(int x) {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000212 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000213 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000214 return date;
Ted Kremenek7e904222009-01-12 21:45:02 +0000215}
216
217// Test a leak involving an overwrite.
218
219CFDateRef f7() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000220 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); //expected-warning{{leak}}
Ted Kremenekf08ac272009-01-24 00:55:43 +0000221 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000222 date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
Ted Kremenek7e904222009-01-12 21:45:02 +0000223 return date;
224}
225
226// Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and
227// has the word create.
228CFDateRef MyDateCreate();
229
230CFDateRef f8() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000231 CFDateRef date = MyDateCreate(); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000232 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000233 return date;
Ted Kremenek7e904222009-01-12 21:45:02 +0000234}
235
236CFDateRef f9() {
237 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
238 int *p = 0;
Ted Kremenek0b891a32009-03-09 22:46:49 +0000239 // When allocations fail, CFDateCreate can return null.
240 if (!date) *p = 1; // expected-warning{{null}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000241 return date;
242}
243
244// Handle DiskArbitration API:
245//
246// http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/
247//
248void f10(io_service_t media, DADiskRef d, CFStringRef s) {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000249 DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}}
250 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000251
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000252 disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}}
253 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000254
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000255 CFDictionaryRef dict = DADiskCopyDescription(d); // expected-warning{{leak}}
256 if (dict) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000257
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000258 disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}}
259 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000260
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000261 DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000262 kDAReturnSuccess, s);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000263 if (dissenter) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000264
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000265 DASessionRef session = DASessionCreate(kCFAllocatorDefault); // expected-warning{{leak}}
266 if (session) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000267}
268
269// Test retain/release checker with CFString and CFMutableArray.
270void f11() {
271 // Create the array.
272 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
273
274 // Create a string.
275 CFStringRef s1 = CFStringCreateWithCString(0, "hello world",
276 kCFStringEncodingUTF8);
277
278 // Add the string to the array.
279 CFArrayAppendValue(A, s1);
280
281 // Decrement the reference count.
282 CFRelease(s1); // no-warning
283
284 // Get the string. We don't own it.
285 s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0);
286
287 // Release the array.
288 CFRelease(A); // no-warning
289
290 // Release the string. This is a bug.
291 CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}}
292}
293
Ted Kremenek86afde32009-01-16 18:40:33 +0000294// PR 3337: Handle functions declared using typedefs.
295typedef CFTypeRef CREATEFUN();
296CREATEFUN MyCreateFun;
297
298void f12() {
299 CFTypeRef o = MyCreateFun(); // expected-warning {{leak}}
300}
Ted Kremenekf6758642009-01-28 21:20:48 +0000301
302void f13_autorelease() {
Ted Kremenek2d0ff622009-05-09 01:50:57 +0000303 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
Ted Kremenekf6758642009-01-28 21:20:48 +0000304 [(id) A autorelease]; // no-warning
305}
Ted Kremenekbea465ae2009-02-19 18:20:28 +0000306
Ted Kremenek2d0ff622009-05-09 01:50:57 +0000307void f13_autorelease_b() {
308 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
309 [(id) A autorelease];
Ted Kremenek3978f792009-05-10 05:11:21 +0000310 [(id) A autorelease]; // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek2d0ff622009-05-09 01:50:57 +0000311}
312
313CFMutableArrayRef f13_autorelease_c() {
314 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
315 [(id) A autorelease];
316 [(id) A autorelease];
Ted Kremenek3978f792009-05-10 05:11:21 +0000317 return A; // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek2d0ff622009-05-09 01:50:57 +0000318}
319
320CFMutableArrayRef f13_autorelease_d() {
321 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
322 [(id) A autorelease];
323 [(id) A autorelease];
Ted Kremenek3978f792009-05-10 05:11:21 +0000324 CFMutableArrayRef B = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek2d0ff622009-05-09 01:50:57 +0000325 CFRelease(B); // no-warning
326}
327
328
Ted Kremenekbea465ae2009-02-19 18:20:28 +0000329// This case exercises the logic where the leak site is the same as the allocation site.
330void f14_leakimmediately() {
331 CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
332}
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000333
Ted Kremenek25db1f32009-04-07 05:33:18 +0000334// Test that we track an allocated object beyond the point where the *name*
335// of the variable storing the reference is no longer live.
336void f15() {
337 // Create the array.
338 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
339 CFMutableArrayRef *B = &A;
340 // At this point, the name 'A' is no longer live.
341 CFRelease(*B); // no-warning
342}
343
344
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000345// Test basic tracking of ivars associated with 'self'. For the retain/release
346// checker we currently do not want to flag leaks associated with stores
347// of tracked objects to ivars.
348@interface SelfIvarTest : NSObject {
349 id myObj;
350}
351- (void)test_self_tracking;
352@end
353
354@implementation SelfIvarTest
355- (void)test_self_tracking {
356 myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
357}
358@end
359
Ted Kremenekdee56e32009-05-10 06:25:57 +0000360// Test return of non-owned objects in contexts where an owned object
361// is expected.
362@interface TestReturnNotOwnedWhenExpectedOwned
363- (NSString*)newString;
364@end
365
366@implementation TestReturnNotOwnedWhenExpectedOwned
367- (NSString*)newString {
368 NSString *s = [NSString stringWithUTF8String:"hello"];
369 return s; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}}
370}
371@end
372
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000373// <rdar://problem/6659160>
374int isFoo(char c);
375
376static void rdar_6659160(char *inkind, char *inname)
377{
378 // We currently expect that [NSObject alloc] cannot fail. This
379 // will be a toggled flag in the future. It can indeed return null, but
380 // Cocoa programmers generally aren't expected to reason about out-of-memory
381 // conditions.
382 NSString *kind = [[NSString alloc] initWithUTF8String:inkind]; // expected-warning{{leak}}
383
384 // We do allow stringWithUTF8String to fail. This isn't really correct, as
Ted Kremenekb294d192009-03-23 17:10:25 +0000385 // far as returning 0. In most error conditions it will throw an exception.
386 // If allocation fails it could return 0, but again this
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000387 // isn't expected.
388 NSString *name = [NSString stringWithUTF8String:inname];
389 if(!name)
390 return;
391
392 const char *kindC = 0;
393 const char *nameC = 0;
394
395 // In both cases, we cannot reach a point down below where we
396 // dereference kindC or nameC with either being null. This is because
397 // we assume that [NSObject alloc] doesn't fail and that we have the guard
398 // up above.
399
400 if(kind)
401 kindC = [kind UTF8String];
402 if(name)
403 nameC = [name UTF8String];
Ted Kremenek0b891a32009-03-09 22:46:49 +0000404 if(!isFoo(kindC[0])) // expected-warning{{null}}
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000405 return;
406 if(!isFoo(nameC[0])) // no-warning
407 return;
408
409 [kind release];
Ted Kremenek0a1f9c42009-04-23 21:25:57 +0000410 [name release]; // expected-warning{{Incorrect decrement of the reference count}}
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000411}
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000412
Ted Kremenek340fd2d2009-03-13 20:27:06 +0000413// PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming
414// conventions with respect to 'return'ing ownership.
415@interface PR3677: NSObject @end
416@implementation PR3677
417+ (id)allocWithZone:(NSZone *)inZone {
418 return [super allocWithZone:inZone]; // no-warning
419}
420@end
421
Ted Kremenek67a3bb72009-03-19 19:50:58 +0000422// PR 3820 - Reason about calls to -dealloc
423void pr3820_DeallocInsteadOfRelease(void)
424{
425 id foo = [[NSString alloc] init]; // no-warning
426 [foo dealloc];
427 // foo is not leaked, since it has been deallocated.
428}
429
430void pr3820_ReleaseAfterDealloc(void)
431{
432 id foo = [[NSString alloc] init];
433 [foo dealloc];
434 [foo release]; // expected-warning{{used after it is release}}
435 // NSInternalInconsistencyException: message sent to deallocated object
436}
437
438void pr3820_DeallocAfterRelease(void)
439{
440 NSLog(@"\n\n[%s]", __FUNCTION__);
441 id foo = [[NSString alloc] init];
442 [foo release];
443 [foo dealloc]; // expected-warning{{used after it is released}}
444 // message sent to released object
445}
Ted Kremenekb294d192009-03-23 17:10:25 +0000446
447// From <rdar://problem/6704930>. The problem here is that 'length' binds to
448// '($0 - 1)' after '--length', but SimpleConstraintManager doesn't know how to
449// reason about '($0 - 1) > constant'. As a temporary hack, we drop the value
450// of '($0 - 1)' and conjure a new symbol.
451void rdar6704930(unsigned char *s, unsigned int length) {
452 NSString* name = 0;
453 if (s != 0) {
454 if (length > 0) {
455 while (length > 0) {
456 if (*s == ':') {
457 ++s;
458 --length;
459 name = [[NSString alloc] init]; // no-warning
460 break;
461 }
462 ++s;
463 --length;
464 }
465 if ((length == 0) && (name != 0)) {
466 [name release];
467 name = 0;
468 }
469 if (length == 0) { // no ':' found -> use it all as name
470 name = [[NSString alloc] init]; // no-warning
471 }
472 }
473 }
474
475 if (name != 0) {
476 [name release];
477 }
478}
479
Ted Kremenek44e662c2009-04-24 23:09:54 +0000480//===----------------------------------------------------------------------===//
Ted Kremenekebc6d912009-04-29 00:41:31 +0000481// <rdar://problem/6833332>
482// One build of the analyzer accidentally stopped tracking the allocated
483// object after the 'retain'.
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000484//===----------------------------------------------------------------------===//
Ted Kremenekebc6d912009-04-29 00:41:31 +0000485
486@interface rdar_6833332 : NSObject <NSApplicationDelegate> {
487 NSWindow *window;
488}
489@property (nonatomic, retain) NSWindow *window;
490@end
491
492@implementation rdar_6833332
493@synthesize window;
494- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
495 NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}}
496
497 [dict setObject:@"foo" forKey:@"bar"];
498
499 NSLog(@"%@", dict);
500}
501- (void)dealloc {
502 [window release];
503 [super dealloc];
504}
505@end
506
Ted Kremenekd73cfc72009-05-09 03:10:32 +0000507//===----------------------------------------------------------------------===//
Ted Kremenek7e7ed522009-05-11 17:45:06 +0000508// <rdar://problem/6257780> clang checker fails to catch use-after-release
509//===----------------------------------------------------------------------===//
510
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000511int rdar_6257780_Case1() {
Ted Kremenek7e7ed522009-05-11 17:45:06 +0000512 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
513 NSArray *array = [NSArray array];
514 [array release]; // expected-warning{{Incorrect decrement of the reference count of an object is not owned at this point by the caller}}
515 [pool drain];
516 return 0;
517}
518
519//===----------------------------------------------------------------------===//
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000520// <rdar://problem/6866843> Checker should understand new/setObject:/release constructs
521//===----------------------------------------------------------------------===//
522
523void rdar_6866843() {
524 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
525 NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
526 NSArray* array = [[NSArray alloc] init];
527 [dictionary setObject:array forKey:@"key"];
528 [array release];
529 // Using 'array' here should be fine
530 NSLog(@"array = %@\n", array); // no-warning
531 // Now the array is released
532 [dictionary release];
533 [pool drain];
534}
535
Ted Kremenek95d18192009-05-12 04:53:03 +0000536
537//===----------------------------------------------------------------------===//
538// <rdar://problem/6877235> Classes typedef-ed to CF objects should get the same treatment as CF objects
539//===----------------------------------------------------------------------===//
540
541typedef CFTypeRef OtherRef;
542
543@interface RDar6877235 : NSObject {}
544- (CFTypeRef)_copyCFTypeRef;
545- (OtherRef)_copyOtherRef;
546@end
547
548@implementation RDar6877235
549- (CFTypeRef)_copyCFTypeRef {
550 return [[NSString alloc] init]; // no-warning
551}
552- (OtherRef)_copyOtherRef {
553 return [[NSString alloc] init]; // no-warning
554}
555@end
556
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000557//===----------------------------------------------------------------------===//
Ted Kremenek1272f702009-05-12 20:06:54 +0000558//<rdar://problem/6320065> false positive - init method returns an object owned by caller
559//===----------------------------------------------------------------------===//
560
561@interface RDar6320065 : NSObject {
562 NSString *_foo;
563}
564- (id)initReturningNewClass;
565- (id)initReturningNewClassBad;
566- (id)initReturningNewClassBad2;
567@end
568
569@interface RDar6320065Subclass : RDar6320065
570@end
571
572@implementation RDar6320065
573- (id)initReturningNewClass {
574 [self release];
575 self = [[RDar6320065Subclass alloc] init]; // no-warning
576 return self;
577}
578- (id)initReturningNewClassBad {
579 [self release];
580 [[RDar6320065Subclass alloc] init]; // expected-warning {{leak}}
581 return self;
582}
583- (id)initReturningNewClassBad2 {
584 [self release];
585 self = [[RDar6320065Subclass alloc] init];
586 return [self autorelease]; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}}
587}
588
589@end
590
591@implementation RDar6320065Subclass
592@end
593
594int RDar6320065_test() {
595 RDar6320065 *test = [[RDar6320065 alloc] init]; // no-warning
596 [test release];
597 return 0;
598}
599
600//===----------------------------------------------------------------------===//
Ted Kremenekf9fa3cb2009-05-14 21:29:16 +0000601// <rdar://problem/6859457> [NSData dataWithBytesNoCopy] does not return a retained object
602//===----------------------------------------------------------------------===//
603
604@interface RDar6859457 : NSObject {}
605- (NSString*) NoCopyString;
606- (NSString*) noCopyString;
607@end
608
609@implementation RDar6859457
610- (NSString*) NoCopyString { return [[NSString alloc] init]; } // no-warning
611- (NSString*) noCopyString { return [[NSString alloc] init]; } // no-warning
612@end
613
614void test_RDar6859457(RDar6859457 *x, void *bytes, NSUInteger dataLength) {
615 [x NoCopyString]; // expected-warning{{leak}}
616 [x noCopyString]; // expected-warning{{leak}}
617 [NSData dataWithBytesNoCopy:bytes length:dataLength]; // no-warning
618 [NSData dataWithBytesNoCopy:bytes length:dataLength freeWhenDone:1]; // no-warning
619}
620
621//===----------------------------------------------------------------------===//
Ted Kremenek32819772009-05-15 15:49:00 +0000622// Method name that has a null IdentifierInfo* for its first selector slot.
623// This test just makes sure that we handle it.
624//===----------------------------------------------------------------------===//
625
626@interface TestNullIdentifier
627@end
628
629@implementation TestNullIdentifier
630+ (id):(int)x, ... {
631 return [[NSString alloc] init]; // expected-warning{{leak}}
632}
633
634//===----------------------------------------------------------------------===//
Ted Kremenekd73cfc72009-05-09 03:10:32 +0000635// Tests of ownership attributes.
636//===----------------------------------------------------------------------===//
637
Ted Kremenek3b204e42009-05-13 21:07:32 +0000638typedef NSString* MyStringTy;
639
Ted Kremenekd73cfc72009-05-09 03:10:32 +0000640@interface TestOwnershipAttr : NSObject
Ted Kremenek3b204e42009-05-13 21:07:32 +0000641- (NSString*) returnsAnOwnedString __attribute__((ns_returns_retained)); // no-warning
642- (NSString*) returnsAnOwnedCFString __attribute__((cf_returns_retained)); // no-warning
643- (MyStringTy) returnsAnOwnedTypedString __attribute__((ns_returns_retained)); // no-warning
644- (int) returnsAnOwnedInt __attribute__((ns_returns_retained)); // expected-warning{{'ns_returns_retained' attribute only applies to functions or methods that return a pointer or Objective-C object}}
Ted Kremenekd73cfc72009-05-09 03:10:32 +0000645@end
646
Ted Kremenek3b204e42009-05-13 21:07:32 +0000647static int ownership_attribute_doesnt_go_here __attribute__((ns_returns_retained)); // expected-warning{{'ns_returns_retained' attribute only applies to function or method types}}
648
Ted Kremenekd73cfc72009-05-09 03:10:32 +0000649void test_attr_1(TestOwnershipAttr *X) {
650 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
651}
652
653void test_attr_1b(TestOwnershipAttr *X) {
654 NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
655}
656