retain-release.m revision fae664ac57991485a6235c2e27eaf089d5f54846
1// RUN: clang-cc -analyze -checker-cfref -verify %s && 2// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -verify %s 3 4 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; 15typedef unsigned int UInt32; 16typedef 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); 31extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); 32typedef 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; 39extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); 40typedef 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; 46typedef int kern_return_t; 47typedef kern_return_t mach_error_t; 48typedef struct objc_selector *SEL; 49typedef signed char BOOL; 50typedef unsigned long NSUInteger; 51@class NSString, Protocol; 52extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); 53typedef struct _NSZone NSZone; 54@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 55@protocol NSObject - (BOOL)isEqual:(id)object; 56- (id)retain; 57- (oneway void)release; 58- (id)autorelease; 59@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; 60@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; 61@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; 62@end @interface NSObject <NSObject> { 63} 64+ (id)allocWithZone:(NSZone *)zone; 65+ (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; 76@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; 77- ( const char *)UTF8String; 78- (id)initWithUTF8String:(const char *)nullTerminatedCString; 79+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; 80@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; 86typedef 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 ); 95@interface NSTask : NSObject - (id)init; 96@end extern NSString * const NSTaskDidTerminateNotification; 97@interface NSResponder : NSObject <NSCoding> { 98struct __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; 109} 110@end @class NSColor, NSFont, NSNotification; 111@interface NSTextTab : NSObject <NSCopying, NSCoding> { 112} 113@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> { 117} 118@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 { 125} 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 @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 143@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 148//===----------------------------------------------------------------------===// 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 192//--------------------------------------------------------------------------- 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//--------------------------------------------------------------------------- 196 197// Test a leak. 198 199CFAbsoluteTime f5(int x) { 200 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 201 CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}} 202 203 if (x) 204 CFRelease(date); 205 206 return t; 207} 208 209// Test a leak involving the return. 210 211CFDateRef f6(int x) { 212 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning{{leak}} 213 CFRetain(date); 214 return date; 215} 216 217// Test a leak involving an overwrite. 218 219CFDateRef f7() { 220 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); //expected-warning{{leak}} 221 CFRetain(date); 222 date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); 223 return date; 224} 225 226// Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and 227// has the word create. 228CFDateRef MyDateCreate(); 229 230CFDateRef f8() { 231 CFDateRef date = MyDateCreate(); // expected-warning{{leak}} 232 CFRetain(date); 233 return date; 234} 235 236CFDateRef f9() { 237 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); 238 int *p = 0; 239 // When allocations fail, CFDateCreate can return null. 240 if (!date) *p = 1; // expected-warning{{null}} 241 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) { 249 DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}} 250 if (disk) NSLog(@"ok"); 251 252 disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}} 253 if (disk) NSLog(@"ok"); 254 255 CFDictionaryRef dict = DADiskCopyDescription(d); // expected-warning{{leak}} 256 if (dict) NSLog(@"ok"); 257 258 disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}} 259 if (disk) NSLog(@"ok"); 260 261 DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, // expected-warning{{leak}} 262 kDAReturnSuccess, s); 263 if (dissenter) NSLog(@"ok"); 264 265 DASessionRef session = DASessionCreate(kCFAllocatorDefault); // expected-warning{{leak}} 266 if (session) NSLog(@"ok"); 267} 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 294// PR 3337: Handle functions declared using typedefs. 295typedef CFTypeRef CREATEFUN(); 296CREATEFUN MyCreateFun; 297 298void f12() { 299 CFTypeRef o = MyCreateFun(); // expected-warning {{leak}} 300} 301 302void f13_autorelease() { 303 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 304 [(id) A autorelease]; // no-warning 305} 306 307void f13_autorelease_b() { 308 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); 309 [(id) A autorelease]; 310 [(id) A autorelease]; // expected-warning{{Object sent -autorelease too many times}} 311} 312 313CFMutableArrayRef f13_autorelease_c() { 314 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); 315 [(id) A autorelease]; 316 [(id) A autorelease]; 317 return A; // expected-warning{{Object sent -autorelease too many times}} 318} 319 320CFMutableArrayRef f13_autorelease_d() { 321 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); 322 [(id) A autorelease]; 323 [(id) A autorelease]; 324 CFMutableArrayRef B = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{Object sent -autorelease too many times}} 325 CFRelease(B); // no-warning 326} 327 328 329// 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} 333 334// 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 345// 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 360// 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 373// <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 385 // far as returning 0. In most error conditions it will throw an exception. 386 // If allocation fails it could return 0, but again this 387 // 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]; 404 if(!isFoo(kindC[0])) // expected-warning{{null}} 405 return; 406 if(!isFoo(nameC[0])) // no-warning 407 return; 408 409 [kind release]; 410 [name release]; // expected-warning{{Incorrect decrement of the reference count}} 411} 412 413// 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 422// 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} 446 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 480//===----------------------------------------------------------------------===// 481// <rdar://problem/6833332> 482// One build of the analyzer accidentally stopped tracking the allocated 483// object after the 'retain'. 484//===----------------------------------------------------------------------===// 485 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 507//===----------------------------------------------------------------------===// 508// <rdar://problem/6257780> clang checker fails to catch use-after-release 509//===----------------------------------------------------------------------===// 510 511int rdar_6257780_Case1() { 512 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//===----------------------------------------------------------------------===// 520// <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 536 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 557//===----------------------------------------------------------------------===// 558//<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//===----------------------------------------------------------------------===// 601// <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//===----------------------------------------------------------------------===// 622// 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//===----------------------------------------------------------------------===// 635// <rdar://problem/6893565> don't flag leaks for return types that cannot be 636// determined to be CF types 637//===----------------------------------------------------------------------===// 638 639// We don't know if 'struct s6893565' represents a Core Foundation type, so 640// we shouldn't emit an error here. 641typedef struct s6893565* TD6893565; 642 643@interface RDar6893565 {} 644-(TD6893565)newThing; 645@end 646 647@implementation RDar6893565 648-(TD6893565)newThing { 649 return (TD6893565) [[NSString alloc] init]; // no-warning 650} 651@end 652 653//===----------------------------------------------------------------------===// 654// Tests of ownership attributes. 655//===----------------------------------------------------------------------===// 656 657typedef NSString* MyStringTy; 658 659@interface TestOwnershipAttr : NSObject 660- (NSString*) returnsAnOwnedString __attribute__((ns_returns_retained)); // no-warning 661- (NSString*) returnsAnOwnedCFString __attribute__((cf_returns_retained)); // no-warning 662- (MyStringTy) returnsAnOwnedTypedString __attribute__((ns_returns_retained)); // no-warning 663- (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}} 664@end 665 666static int ownership_attribute_doesnt_go_here __attribute__((ns_returns_retained)); // expected-warning{{'ns_returns_retained' attribute only applies to function or method types}} 667 668void test_attr_1(TestOwnershipAttr *X) { 669 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}} 670} 671 672void test_attr_1b(TestOwnershipAttr *X) { 673 NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}} 674} 675 676