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