retain-release-region-store.m revision 159c53dd832269cb95652eb77112420ae06bb19a
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 - (BOOL)isEqual:(id)object; 47- (id)retain; 48- (oneway void)release; 49@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; 50@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; 51@end @interface NSObject <NSObject> { 52} 53@end typedef float CGFloat; 54typedef double NSTimeInterval; 55@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; 56@end enum { 57NSObjCNoType = 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' } 58__attribute__((deprecated)); 59typedef int kern_return_t; 60typedef kern_return_t mach_error_t; 61typedef mach_port_t io_object_t; 62typedef io_object_t io_service_t; 63typedef struct __DASession * DASessionRef; 64extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); 65typedef struct __DADisk * DADiskRef; 66extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); 67extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); 68extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); 69extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); 70@interface NSAppleEventManager : NSObject { 71} 72@end enum { 73kDAReturnSuccess = 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 }; 74typedef mach_error_t DAReturn; 75typedef const struct __DADissenter * DADissenterRef; 76extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); 77 78//===----------------------------------------------------------------------===// 79// Test cases. 80//===----------------------------------------------------------------------===// 81 82// Test to see if we *issue* an error when we store the pointer 83// to a struct. This differs from basic store. 84 85struct foo { 86 NSDate* f; 87}; 88 89CFAbsoluteTime f4() { 90 struct foo x; 91 92 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 93 CFDateRef date = CFDateCreate(0, t); 94 [((NSDate*) date) retain]; 95 CFRelease(date); 96 CFDateGetAbsoluteTime(date); // no-warning 97 x.f = (NSDate*) date; 98 [((NSDate*) date) release]; 99 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} 100 return t; 101} 102 103// Test that assigning to an self.ivar loses track of an object. 104// This is a temporary hack to reduce false positives. 105@interface Test3 : NSObject { 106 id myObj; 107} 108- (void)test_self_assign_ivar; 109@end 110 111@implementation Test3 112- (void)test_self_assign_ivar { 113 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 114 CFDateRef date = CFDateCreate(0, t); // no-warning 115 myObj = (id) date; 116} 117@end 118 119//===----------------------------------------------------------------------===// 120// <rdar://problem/7257223> - False positive due to not invalidating the 121// reference count of a tracked region that was itself invalidated. 122//===----------------------------------------------------------------------===// 123 124typedef struct __rdar_7257223 { CFDateRef x; } RDar7257223; 125void rdar_7257223_aux(RDar7257223 *p); 126 127// THIS CASE CURRENTLY FAILS. 128CFDateRef rdar7257223_Create(void) { 129 RDar7257223 s; 130 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 131 s.x = CFDateCreate(0, t); // no-warning 132 rdar_7257223_aux(&s); 133 return s.x; 134} 135 136CFDateRef rdar7257223_Create_2(void) { 137 RDar7257223 s; 138 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 139 s.x = CFDateCreate(0, t); // no-warning 140 return s.x; 141} 142 143