retain-release-region-store.m revision 47dcd06e113c5a3b6621166acdb163734a1cfa33
1// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -verify %s 2// XFAIL 3 4//===----------------------------------------------------------------------===// 5// The following code is reduced using delta-debugging from 6// Foundation.h (Mac OS X). 7// 8// It includes the basic definitions for the test cases below. 9// Not including Foundation.h directly makes this test case both svelte and 10// portable to non-Mac platforms. 11//===----------------------------------------------------------------------===// 12 13typedef unsigned int __darwin_natural_t; 14typedef unsigned long UInt32; 15typedef signed long CFIndex; 16typedef const void * CFTypeRef; 17typedef const struct __CFString * CFStringRef; 18typedef const struct __CFAllocator * CFAllocatorRef; 19extern const CFAllocatorRef kCFAllocatorDefault; 20extern CFTypeRef CFRetain(CFTypeRef cf); 21extern void CFRelease(CFTypeRef cf); 22typedef struct { 23} 24CFArrayCallBacks; 25extern const CFArrayCallBacks kCFTypeArrayCallBacks; 26typedef const struct __CFArray * CFArrayRef; 27typedef struct __CFArray * CFMutableArrayRef; 28extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); 29extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); 30typedef const struct __CFDictionary * CFDictionaryRef; 31typedef UInt32 CFStringEncoding; 32enum { 33kCFStringEncodingMacRoman = 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 }; 34extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); 35typedef double CFTimeInterval; 36typedef CFTimeInterval CFAbsoluteTime; 37typedef const struct __CFDate * CFDateRef; 38extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); 39extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); 40typedef __darwin_natural_t natural_t; 41typedef natural_t mach_port_name_t; 42typedef mach_port_name_t mach_port_t; 43typedef signed char BOOL; 44typedef struct _NSZone NSZone; 45@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 46@protocol NSObject 47- (BOOL)isEqual:(id)object; 48- (id)retain; 49- (oneway void)release; 50@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; 51@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; 52@end 53@interface NSObject <NSObject> {} 54+ (id)allocWithZone:(NSZone *)zone; 55+ (id)alloc; 56- (void)dealloc; 57@end 58typedef float CGFloat; 59typedef double NSTimeInterval; 60@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; 61@end enum { 62NSObjCNoType = 0, NSObjCVoidType = 'v', NSObjCCharType = 'c', NSObjCShortType = 's', NSObjCLongType = 'l', NSObjCLonglongType = 'q', NSObjCFloatType = 'f', NSObjCDoubleType = 'd', NSObjCBoolType = 'B', NSObjCSelectorType = ':', NSObjCObjectType = '@', NSObjCStructType = '{', NSObjCPointerType = '^', NSObjCStringType = '*', NSObjCArrayType = '[', NSObjCUnionType = '(', NSObjCBitfield = 'b' } 63__attribute__((deprecated)); 64typedef int kern_return_t; 65typedef kern_return_t mach_error_t; 66typedef mach_port_t io_object_t; 67typedef io_object_t io_service_t; 68typedef struct __DASession * DASessionRef; 69extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); 70typedef struct __DADisk * DADiskRef; 71extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); 72extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); 73extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); 74extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); 75@interface NSAppleEventManager : NSObject { 76} 77@end enum { 78kDAReturnSuccess = 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 }; 79typedef mach_error_t DAReturn; 80typedef const struct __DADissenter * DADissenterRef; 81extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); 82@interface NSNumber : NSObject 83 - (id)initWithInt:(int)value; 84 @end 85 86//===----------------------------------------------------------------------===// 87// Test cases. 88//===----------------------------------------------------------------------===// 89 90// Test to see if we *issue* an error when we store the pointer 91// to a struct. This differs from basic store. 92 93struct foo { 94 NSDate* f; 95}; 96 97CFAbsoluteTime f4() { 98 struct foo x; 99 100 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 101 CFDateRef date = CFDateCreate(0, t); 102 [((NSDate*) date) retain]; 103 CFRelease(date); 104 CFDateGetAbsoluteTime(date); // no-warning 105 x.f = (NSDate*) date; 106 [((NSDate*) date) release]; 107 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} 108 return t; 109} 110 111// Test that assigning to an self.ivar loses track of an object. 112// This is a temporary hack to reduce false positives. 113@interface Test3 : NSObject { 114 id myObj; 115} 116- (void)test_self_assign_ivar; 117@end 118 119@implementation Test3 120- (void)test_self_assign_ivar { 121 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 122 CFDateRef date = CFDateCreate(0, t); // no-warning 123 myObj = (id) date; 124} 125@end 126 127//===----------------------------------------------------------------------===// 128// <rdar://problem/7257223> - False positive due to not invalidating the 129// reference count of a tracked region that was itself invalidated. 130//===----------------------------------------------------------------------===// 131 132typedef struct __rdar_7257223 { CFDateRef x; } RDar7257223; 133void rdar_7257223_aux(RDar7257223 *p); 134 135// THIS CASE CURRENTLY FAILS. 136CFDateRef rdar7257223_Create(void) { 137 RDar7257223 s; 138 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 139 s.x = CFDateCreate(0, t); // no-warning 140 rdar_7257223_aux(&s); 141 return s.x; 142} 143 144CFDateRef rdar7257223_Create_2(void) { 145 RDar7257223 s; 146 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 147 s.x = CFDateCreate(0, t); // no-warning 148 return s.x; 149} 150 151//===----------------------------------------------------------------------===// 152// <rdar://problem/7283470> 153//===----------------------------------------------------------------------===// 154 155void rdar7283470(void) { 156 NSNumber *numbers[] = { 157 [[NSNumber alloc] initWithInt:1], // no-warning 158 [[NSNumber alloc] initWithInt:2], // no-warning 159 [[NSNumber alloc] initWithInt:3], // no-warning 160 [[NSNumber alloc] initWithInt:4], // no-warning 161 [[NSNumber alloc] initWithInt:5] // no-warning 162 }; 163 164 for (unsigned i = 0 ; i < sizeof(numbers) / sizeof(numbers[0]) ; ++i) 165 [numbers[i] release]; 166} 167 168void rdar7283470_positive(void) { 169 NSNumber *numbers[] = { 170 [[NSNumber alloc] initWithInt:1], // expected-warning{{leak}} 171 [[NSNumber alloc] initWithInt:2], // expected-warning{{leak}} 172 [[NSNumber alloc] initWithInt:3], // expected-warning{{leak}} 173 [[NSNumber alloc] initWithInt:4], // expected-warning{{leak}} 174 [[NSNumber alloc] initWithInt:5] // expected-warning{{leak}} 175 }; 176} 177 178