blob: 602c611f7f22a90c512f187c196ff942d1ca63de [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
132//===----------------------------------------------------------------------===//
133// Test cases.
134//===----------------------------------------------------------------------===//
135
136CFAbsoluteTime f1() {
137 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
138 CFDateRef date = CFDateCreate(0, t);
139 CFRetain(date);
140 CFRelease(date);
141 CFDateGetAbsoluteTime(date); // no-warning
142 CFRelease(date);
143 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
144 return t;
145}
146
147CFAbsoluteTime f2() {
148 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
149 CFDateRef date = CFDateCreate(0, t);
150 [((NSDate*) date) retain];
151 CFRelease(date);
152 CFDateGetAbsoluteTime(date); // no-warning
153 [((NSDate*) date) release];
154 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
155 return t;
156}
157
158
159NSDate* global_x;
160
161// Test to see if we supresss an error when we store the pointer
162// to a global.
163
164CFAbsoluteTime f3() {
165 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
166 CFDateRef date = CFDateCreate(0, t);
167 [((NSDate*) date) retain];
168 CFRelease(date);
169 CFDateGetAbsoluteTime(date); // no-warning
170 global_x = (NSDate*) date;
171 [((NSDate*) date) release];
172 t = CFDateGetAbsoluteTime(date); // no-warning
173 return t;
174}
175
Ted Kremenekfe32cc02009-01-21 06:57:53 +0000176//---------------------------------------------------------------------------
177// Test case 'f4' differs for region store and basic store. See
178// retain-release-region-store.m and retain-release-basic-store.m.
179//---------------------------------------------------------------------------
Ted Kremenek7e904222009-01-12 21:45:02 +0000180
181// Test a leak.
182
183CFAbsoluteTime f5(int x) {
184 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000185 CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000186
187 if (x)
188 CFRelease(date);
189
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000190 return t;
Ted Kremenek7e904222009-01-12 21:45:02 +0000191}
192
193// Test a leak involving the return.
194
195CFDateRef f6(int x) {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000196 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000197 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000198 return date;
Ted Kremenek7e904222009-01-12 21:45:02 +0000199}
200
201// Test a leak involving an overwrite.
202
203CFDateRef f7() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000204 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); //expected-warning{{leak}}
Ted Kremenekf08ac272009-01-24 00:55:43 +0000205 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000206 date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
Ted Kremenek7e904222009-01-12 21:45:02 +0000207 return date;
208}
209
210// Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and
211// has the word create.
212CFDateRef MyDateCreate();
213
214CFDateRef f8() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000215 CFDateRef date = MyDateCreate(); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000216 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000217 return date;
Ted Kremenek7e904222009-01-12 21:45:02 +0000218}
219
220CFDateRef f9() {
221 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
222 int *p = 0;
Ted Kremenek0b891a32009-03-09 22:46:49 +0000223 // When allocations fail, CFDateCreate can return null.
224 if (!date) *p = 1; // expected-warning{{null}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000225 return date;
226}
227
228// Handle DiskArbitration API:
229//
230// http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/
231//
232void f10(io_service_t media, DADiskRef d, CFStringRef s) {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000233 DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}}
234 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000235
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000236 disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}}
237 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000238
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000239 CFDictionaryRef dict = DADiskCopyDescription(d); // expected-warning{{leak}}
240 if (dict) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000241
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000242 disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}}
243 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000244
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000245 DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000246 kDAReturnSuccess, s);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000247 if (dissenter) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000248
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000249 DASessionRef session = DASessionCreate(kCFAllocatorDefault); // expected-warning{{leak}}
250 if (session) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000251}
252
253// Test retain/release checker with CFString and CFMutableArray.
254void f11() {
255 // Create the array.
256 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
257
258 // Create a string.
259 CFStringRef s1 = CFStringCreateWithCString(0, "hello world",
260 kCFStringEncodingUTF8);
261
262 // Add the string to the array.
263 CFArrayAppendValue(A, s1);
264
265 // Decrement the reference count.
266 CFRelease(s1); // no-warning
267
268 // Get the string. We don't own it.
269 s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0);
270
271 // Release the array.
272 CFRelease(A); // no-warning
273
274 // Release the string. This is a bug.
275 CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}}
276}
277
Ted Kremenek86afde32009-01-16 18:40:33 +0000278// PR 3337: Handle functions declared using typedefs.
279typedef CFTypeRef CREATEFUN();
280CREATEFUN MyCreateFun;
281
282void f12() {
283 CFTypeRef o = MyCreateFun(); // expected-warning {{leak}}
284}
Ted Kremenekf6758642009-01-28 21:20:48 +0000285
286void f13_autorelease() {
287 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
288 [(id) A autorelease]; // no-warning
289}
Ted Kremenekbea465ae2009-02-19 18:20:28 +0000290
291// This case exercises the logic where the leak site is the same as the allocation site.
292void f14_leakimmediately() {
293 CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
294}
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000295
Ted Kremenek25db1f32009-04-07 05:33:18 +0000296// Test that we track an allocated object beyond the point where the *name*
297// of the variable storing the reference is no longer live.
298void f15() {
299 // Create the array.
300 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
301 CFMutableArrayRef *B = &A;
302 // At this point, the name 'A' is no longer live.
303 CFRelease(*B); // no-warning
304}
305
306
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000307// Test basic tracking of ivars associated with 'self'. For the retain/release
308// checker we currently do not want to flag leaks associated with stores
309// of tracked objects to ivars.
310@interface SelfIvarTest : NSObject {
311 id myObj;
312}
313- (void)test_self_tracking;
314@end
315
316@implementation SelfIvarTest
317- (void)test_self_tracking {
318 myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
319}
320@end
321
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000322// <rdar://problem/6659160>
323int isFoo(char c);
324
325static void rdar_6659160(char *inkind, char *inname)
326{
327 // We currently expect that [NSObject alloc] cannot fail. This
328 // will be a toggled flag in the future. It can indeed return null, but
329 // Cocoa programmers generally aren't expected to reason about out-of-memory
330 // conditions.
331 NSString *kind = [[NSString alloc] initWithUTF8String:inkind]; // expected-warning{{leak}}
332
333 // We do allow stringWithUTF8String to fail. This isn't really correct, as
Ted Kremenekb294d192009-03-23 17:10:25 +0000334 // far as returning 0. In most error conditions it will throw an exception.
335 // If allocation fails it could return 0, but again this
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000336 // isn't expected.
337 NSString *name = [NSString stringWithUTF8String:inname];
338 if(!name)
339 return;
340
341 const char *kindC = 0;
342 const char *nameC = 0;
343
344 // In both cases, we cannot reach a point down below where we
345 // dereference kindC or nameC with either being null. This is because
346 // we assume that [NSObject alloc] doesn't fail and that we have the guard
347 // up above.
348
349 if(kind)
350 kindC = [kind UTF8String];
351 if(name)
352 nameC = [name UTF8String];
Ted Kremenek0b891a32009-03-09 22:46:49 +0000353 if(!isFoo(kindC[0])) // expected-warning{{null}}
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000354 return;
355 if(!isFoo(nameC[0])) // no-warning
356 return;
357
358 [kind release];
Ted Kremenek0a1f9c42009-04-23 21:25:57 +0000359 [name release]; // expected-warning{{Incorrect decrement of the reference count}}
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000360}
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000361
Ted Kremenek340fd2d2009-03-13 20:27:06 +0000362// PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming
363// conventions with respect to 'return'ing ownership.
364@interface PR3677: NSObject @end
365@implementation PR3677
366+ (id)allocWithZone:(NSZone *)inZone {
367 return [super allocWithZone:inZone]; // no-warning
368}
369@end
370
Ted Kremenek67a3bb72009-03-19 19:50:58 +0000371// PR 3820 - Reason about calls to -dealloc
372void pr3820_DeallocInsteadOfRelease(void)
373{
374 id foo = [[NSString alloc] init]; // no-warning
375 [foo dealloc];
376 // foo is not leaked, since it has been deallocated.
377}
378
379void pr3820_ReleaseAfterDealloc(void)
380{
381 id foo = [[NSString alloc] init];
382 [foo dealloc];
383 [foo release]; // expected-warning{{used after it is release}}
384 // NSInternalInconsistencyException: message sent to deallocated object
385}
386
387void pr3820_DeallocAfterRelease(void)
388{
389 NSLog(@"\n\n[%s]", __FUNCTION__);
390 id foo = [[NSString alloc] init];
391 [foo release];
392 [foo dealloc]; // expected-warning{{used after it is released}}
393 // message sent to released object
394}
Ted Kremenekb294d192009-03-23 17:10:25 +0000395
396// From <rdar://problem/6704930>. The problem here is that 'length' binds to
397// '($0 - 1)' after '--length', but SimpleConstraintManager doesn't know how to
398// reason about '($0 - 1) > constant'. As a temporary hack, we drop the value
399// of '($0 - 1)' and conjure a new symbol.
400void rdar6704930(unsigned char *s, unsigned int length) {
401 NSString* name = 0;
402 if (s != 0) {
403 if (length > 0) {
404 while (length > 0) {
405 if (*s == ':') {
406 ++s;
407 --length;
408 name = [[NSString alloc] init]; // no-warning
409 break;
410 }
411 ++s;
412 --length;
413 }
414 if ((length == 0) && (name != 0)) {
415 [name release];
416 name = 0;
417 }
418 if (length == 0) { // no ':' found -> use it all as name
419 name = [[NSString alloc] init]; // no-warning
420 }
421 }
422 }
423
424 if (name != 0) {
425 [name release];
426 }
427}
428
Ted Kremenek44e662c2009-04-24 23:09:54 +0000429//===----------------------------------------------------------------------===//
430// Tests of ownership attributes.
431//===----------------------------------------------------------------------===//
432
433@interface TestOwnershipAttr : NSObject
434- (NSString*) returnsAnOwnedString __attribute__((objc_ownership_returns));
Ted Kremeneke75de952009-04-25 01:21:50 +0000435- (void) myRetain:(id)__attribute__((objc_ownership_retain))obj;
Ted Kremenekebbef7d2009-04-27 18:27:22 +0000436- (void) myCFRetain:(id)__attribute__((objc_ownership_cfretain))obj;
Ted Kremenek84bfa2c2009-04-27 19:36:56 +0000437- (void) myRelease:(id)__attribute__((objc_ownership_release))obj;
438- (void) myCFRelease:(id)__attribute__((objc_ownership_cfrelease))obj;
Ted Kremenek44e662c2009-04-24 23:09:54 +0000439@end
440
441void test_attr_1(TestOwnershipAttr *X) {
Ted Kremenekb97d0932009-04-24 23:32:32 +0000442 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
Ted Kremenek44e662c2009-04-24 23:09:54 +0000443}
444
Ted Kremenek2cfd2642009-04-25 00:17:17 +0000445void test_attr_2(TestOwnershipAttr *X) {
Ted Kremeneke75de952009-04-25 01:21:50 +0000446 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
Ted Kremenek2cfd2642009-04-25 00:17:17 +0000447 [X myRetain:str];
448 [str release];
449}
450
Ted Kremenekebbef7d2009-04-27 18:27:22 +0000451void test_attr_3(TestOwnershipAttr *X) {
452 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
453 [X myCFRetain:str];
454 [str release];
455}
Ted Kremenek2cfd2642009-04-25 00:17:17 +0000456
Ted Kremenek89c38612009-04-28 21:43:40 +0000457void test_attr_4a(TestOwnershipAttr *X) {
458 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
459}
460
461void test_attr_4b(TestOwnershipAttr *X) {
462 NSString *str = [X returnsAnOwnedString]; // no-warning
463 [X myRelease:str];
464}
465
466void test_attr_4c(TestOwnershipAttr *X) {
Ted Kremenek84bfa2c2009-04-27 19:36:56 +0000467 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
468 [X myRetain:str];
469 [X myRelease:str];
470}
471
Ted Kremenek89c38612009-04-28 21:43:40 +0000472void test_attr_4d(TestOwnershipAttr *X) {
473 NSString *str = [X returnsAnOwnedString];
474 [X myRelease:str];
475 [X myRelease:str]; // expected-warning{{Reference-counted object is used after it is released}}
476}
477
478void test_attr_5a(TestOwnershipAttr *X) {
479 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
480}
481
482void test_attr_5b(TestOwnershipAttr *X) {
483 NSString *str = [X returnsAnOwnedString]; // no-warning
484 [X myCFRelease:str];
485}
486
487void test_attr_5c(TestOwnershipAttr *X) {
Ted Kremenek84bfa2c2009-04-27 19:36:56 +0000488 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
489 [X myCFRetain:str];
490 [X myCFRelease:str];
491}
492
Ted Kremenekebc6d912009-04-29 00:41:31 +0000493//===----------------------------------------------------------------------===//
494// <rdar://problem/6833332>
495// One build of the analyzer accidentally stopped tracking the allocated
496// object after the 'retain'.
497//===----------------------------------------------------------------------===//
498
499@interface rdar_6833332 : NSObject <NSApplicationDelegate> {
500 NSWindow *window;
501}
502@property (nonatomic, retain) NSWindow *window;
503@end
504
505@implementation rdar_6833332
506@synthesize window;
507- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
508 NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}}
509
510 [dict setObject:@"foo" forKey:@"bar"];
511
512 NSLog(@"%@", dict);
513}
514- (void)dealloc {
515 [window release];
516 [super dealloc];
517}
518@end
519