retain-release.m revision 1c87980ef18dbf4669c7194d60138ff9747d7ab7
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease,osx.cocoa.RetainCount -analyzer-store=region -fblocks -verify %s
2// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease,osx.cocoa.RetainCount -analyzer-store=region -fblocks -verify -x objective-c++ %s
3
4#if __has_feature(attribute_ns_returns_retained)
5#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
6#endif
7#if __has_feature(attribute_cf_returns_retained)
8#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
9#endif
10#if __has_feature(attribute_ns_returns_not_retained)
11#define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
12#endif
13#if __has_feature(attribute_cf_returns_not_retained)
14#define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))
15#endif
16#if __has_feature(attribute_ns_consumes_self)
17#define NS_CONSUMES_SELF __attribute__((ns_consumes_self))
18#endif
19#if __has_feature(attribute_ns_consumed)
20#define NS_CONSUMED __attribute__((ns_consumed))
21#endif
22#if __has_feature(attribute_cf_consumed)
23#define CF_CONSUMED __attribute__((cf_consumed))
24#endif
25
26//===----------------------------------------------------------------------===//
27// The following code is reduced using delta-debugging from Mac OS X headers:
28//
29// #include <Cocoa/Cocoa.h>
30// #include <CoreFoundation/CoreFoundation.h>
31// #include <DiskArbitration/DiskArbitration.h>
32// #include <QuartzCore/QuartzCore.h>
33// #include <Quartz/Quartz.h>
34// #include <IOKit/IOKitLib.h>
35//
36// It includes the basic definitions for the test cases below.
37//===----------------------------------------------------------------------===//
38
39typedef unsigned int __darwin_natural_t;
40typedef unsigned long uintptr_t;
41typedef unsigned int uint32_t;
42typedef unsigned long long uint64_t;
43typedef unsigned int UInt32;
44typedef signed long CFIndex;
45typedef CFIndex CFByteOrder;
46typedef struct {
47    CFIndex location;
48    CFIndex length;
49} CFRange;
50static __inline__ __attribute__((always_inline)) CFRange CFRangeMake(CFIndex loc, CFIndex len) {
51    CFRange range;
52    range.location = loc;
53    range.length = len;
54    return range;
55}
56typedef const void * CFTypeRef;
57typedef const struct __CFString * CFStringRef;
58typedef const struct __CFAllocator * CFAllocatorRef;
59extern const CFAllocatorRef kCFAllocatorDefault;
60extern CFTypeRef CFRetain(CFTypeRef cf);
61extern void CFRelease(CFTypeRef cf);
62typedef struct {
63}
64CFArrayCallBacks;
65extern const CFArrayCallBacks kCFTypeArrayCallBacks;
66typedef const struct __CFArray * CFArrayRef;
67typedef struct __CFArray * CFMutableArrayRef;
68extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
69extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
70extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
71typedef struct {
72}
73CFDictionaryKeyCallBacks;
74extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks;
75typedef struct {
76}
77CFDictionaryValueCallBacks;
78extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks;
79typedef const struct __CFDictionary * CFDictionaryRef;
80typedef struct __CFDictionary * CFMutableDictionaryRef;
81extern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks);
82typedef UInt32 CFStringEncoding;
83enum {
84kCFStringEncodingMacRoman = 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  };
85extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
86typedef double CFTimeInterval;
87typedef CFTimeInterval CFAbsoluteTime;
88extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
89typedef const struct __CFDate * CFDateRef;
90extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
91extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
92typedef __darwin_natural_t natural_t;
93typedef natural_t mach_port_name_t;
94typedef mach_port_name_t mach_port_t;
95typedef int kern_return_t;
96typedef kern_return_t mach_error_t;
97enum {
98kCFNumberSInt8Type = 1,     kCFNumberSInt16Type = 2,     kCFNumberSInt32Type = 3,     kCFNumberSInt64Type = 4,     kCFNumberFloat32Type = 5,     kCFNumberFloat64Type = 6,      kCFNumberCharType = 7,     kCFNumberShortType = 8,     kCFNumberIntType = 9,     kCFNumberLongType = 10,     kCFNumberLongLongType = 11,     kCFNumberFloatType = 12,     kCFNumberDoubleType = 13,      kCFNumberCFIndexType = 14,      kCFNumberNSIntegerType = 15,     kCFNumberCGFloatType = 16,     kCFNumberMaxType = 16    };
99typedef CFIndex CFNumberType;
100typedef const struct __CFNumber * CFNumberRef;
101extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr);
102typedef const struct __CFAttributedString *CFAttributedStringRef;
103typedef struct __CFAttributedString *CFMutableAttributedStringRef;
104extern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ;
105extern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ;
106extern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ;
107typedef signed char BOOL;
108typedef unsigned long NSUInteger;
109@class NSString, Protocol;
110extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
111typedef struct _NSZone NSZone;
112@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
113@protocol NSObject
114- (BOOL)isEqual:(id)object;
115- (id)retain;
116- (oneway void)release;
117- (id)autorelease;
118- (id)init;
119@end  @protocol NSCopying  - (id)copyWithZone:(NSZone *)zone;
120@end  @protocol NSMutableCopying  - (id)mutableCopyWithZone:(NSZone *)zone;
121@end  @protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder;
122@end
123@interface NSObject <NSObject> {}
124+ (id)allocWithZone:(NSZone *)zone;
125+ (id)alloc;
126- (void)dealloc;
127@end
128@interface NSObject (NSCoderMethods)
129- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;
130@end
131extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
132typedef struct {
133}
134NSFastEnumerationState;
135@protocol NSFastEnumeration  - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
136@end           @class NSString, NSDictionary;
137@interface NSValue : NSObject <NSCopying, NSCoding>  - (void)getValue:(void *)value;
138@end  @interface NSNumber : NSValue  - (char)charValue;
139- (id)initWithInt:(int)value;
140@end   @class NSString;
141@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>  - (NSUInteger)count;
142@end  @interface NSArray (NSArrayCreation)  + (id)array;
143@end       @interface NSAutoreleasePool : NSObject {
144}
145- (void)drain;
146@end extern NSString * const NSBundleDidLoadNotification;
147typedef double NSTimeInterval;
148@interface NSDate : NSObject <NSCopying, NSCoding>  - (NSTimeInterval)timeIntervalSinceReferenceDate;
149@end            typedef unsigned short unichar;
150@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
151- (NSUInteger)length;
152- (NSString *)stringByAppendingString:(NSString *)aString;
153- ( const char *)UTF8String;
154- (id)initWithUTF8String:(const char *)nullTerminatedCString;
155+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
156@end        @class NSString, NSURL, NSError;
157@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding>  - (NSUInteger)length;
158+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
159+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
160@end   @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary;
161@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>  - (NSUInteger)count;
162@end    @interface NSMutableDictionary : NSDictionary  - (void)removeObjectForKey:(id)aKey;
163- (void)setObject:(id)anObject forKey:(id)aKey;
164@end  @interface NSMutableDictionary (NSMutableDictionaryCreation)  + (id)dictionaryWithCapacity:(NSUInteger)numItems;
165@end  typedef double CGFloat;
166struct CGSize {
167};
168typedef struct CGSize CGSize;
169struct CGRect {
170};
171typedef struct CGRect CGRect;
172typedef mach_port_t io_object_t;
173typedef char io_name_t[128];
174typedef io_object_t io_iterator_t;
175typedef io_object_t io_service_t;
176typedef struct IONotificationPort * IONotificationPortRef;
177typedef void (*IOServiceMatchingCallback)(  void * refcon,  io_iterator_t iterator );
178io_service_t IOServiceGetMatchingService(  mach_port_t masterPort,  CFDictionaryRef matching );
179kern_return_t IOServiceGetMatchingServices(  mach_port_t masterPort,  CFDictionaryRef matching,  io_iterator_t * existing );
180kern_return_t IOServiceAddNotification(  mach_port_t masterPort,  const io_name_t notificationType,  CFDictionaryRef matching,  mach_port_t wakePort,  uintptr_t reference,  io_iterator_t * notification ) __attribute__((deprecated));
181kern_return_t IOServiceAddMatchingNotification(  IONotificationPortRef notifyPort,  const io_name_t notificationType,  CFDictionaryRef matching,         IOServiceMatchingCallback callback,         void * refCon,  io_iterator_t * notification );
182CFMutableDictionaryRef IOServiceMatching(  const char * name );
183CFMutableDictionaryRef IOServiceNameMatching(  const char * name );
184CFMutableDictionaryRef IOBSDNameMatching(  mach_port_t masterPort,  uint32_t options,  const char * bsdName );
185CFMutableDictionaryRef IOOpenFirmwarePathMatching(  mach_port_t masterPort,  uint32_t options,  const char * path );
186CFMutableDictionaryRef IORegistryEntryIDMatching(  uint64_t entryID );
187typedef struct __DASession * DASessionRef;
188extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
189typedef struct __DADisk * DADiskRef;
190extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
191extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
192extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
193extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
194@interface NSTask : NSObject - (id)init;
195@end                    typedef struct CGColorSpace *CGColorSpaceRef;
196typedef struct CGImage *CGImageRef;
197typedef struct CGLayer *CGLayerRef;
198@interface NSResponder : NSObject <NSCoding> {
199}
200@end    @protocol NSAnimatablePropertyContainer      - (id)animator;
201@end  extern NSString *NSAnimationTriggerOrderIn ;
202@interface NSView : NSResponder  <NSAnimatablePropertyContainer>  {
203}
204@end @protocol NSValidatedUserInterfaceItem - (SEL)action;
205@end   @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
206@end  @class NSDate, NSDictionary, NSError, NSException, NSNotification;
207@interface NSApplication : NSResponder <NSUserInterfaceValidations> {
208}
209@end   enum {
210NSTerminateCancel = 0,         NSTerminateNow = 1,         NSTerminateLater = 2 };
211typedef NSUInteger NSApplicationTerminateReply;
212@protocol NSApplicationDelegate <NSObject> @optional        - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
213@end  @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView;
214@interface NSCell : NSObject <NSCopying, NSCoding> {
215}
216@end @class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError;
217typedef struct {
218}
219CVTimeStamp;
220@interface CIImage : NSObject <NSCoding, NSCopying> {
221}
222typedef int CIFormat;
223@end  enum {
224kDAReturnSuccess = 0,     kDAReturnError = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x01,     kDAReturnBusy = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x02,     kDAReturnBadArgument = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x03,     kDAReturnExclusiveAccess = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x04,     kDAReturnNoResources = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x05,     kDAReturnNotFound = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x06,     kDAReturnNotMounted = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x07,     kDAReturnNotPermitted = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x08,     kDAReturnNotPrivileged = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x09,     kDAReturnNotReady = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0A,     kDAReturnNotWritable = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0B,     kDAReturnUnsupported = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0C };
225typedef mach_error_t DAReturn;
226typedef const struct __DADissenter * DADissenterRef;
227extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
228@interface CIContext: NSObject {
229}
230- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r;
231- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r     format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs;
232- (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d;
233@end extern NSString* const QCRendererEventKey;
234@protocol QCCompositionRenderer - (NSDictionary*) attributes;
235@end   @interface QCRenderer : NSObject <QCCompositionRenderer> {
236}
237- (id) createSnapshotImageOfType:(NSString*)type;
238@end  extern NSString* const QCViewDidStartRenderingNotification;
239@interface QCView : NSView <QCCompositionRenderer> {
240}
241- (id) createSnapshotImageOfType:(NSString*)type;
242@end    enum {
243ICEXIFOrientation1 = 1,     ICEXIFOrientation2 = 2,     ICEXIFOrientation3 = 3,     ICEXIFOrientation4 = 4,     ICEXIFOrientation5 = 5,     ICEXIFOrientation6 = 6,     ICEXIFOrientation7 = 7,     ICEXIFOrientation8 = 8, };
244@class ICDevice;
245@protocol ICDeviceDelegate <NSObject>  @required      - (void)didRemoveDevice:(ICDevice*)device;
246@end extern NSString *const ICScannerStatusWarmingUp;
247@class ICScannerDevice;
248@protocol ICScannerDeviceDelegate <ICDeviceDelegate>  @optional       - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
249@end
250
251typedef long unsigned int __darwin_size_t;
252typedef __darwin_size_t size_t;
253typedef unsigned long CFTypeID;
254struct CGPoint {
255  CGFloat x;
256  CGFloat y;
257};
258typedef struct CGPoint CGPoint;
259typedef struct CGGradient *CGGradientRef;
260typedef uint32_t CGGradientDrawingOptions;
261extern CFTypeID CGGradientGetTypeID(void);
262extern CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef
263  space, const CGFloat components[], const CGFloat locations[], size_t count);
264extern CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space,
265  CFArrayRef colors, const CGFloat locations[]);
266extern CGGradientRef CGGradientRetain(CGGradientRef gradient);
267extern void CGGradientRelease(CGGradientRef gradient);
268typedef struct CGContext *CGContextRef;
269extern void CGContextDrawLinearGradient(CGContextRef context,
270    CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint,
271    CGGradientDrawingOptions options);
272extern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void);
273
274@interface NSMutableArray : NSObject
275- (void)addObject:(id)object;
276+ (id)array;
277@end
278
279
280//===----------------------------------------------------------------------===//
281// Test cases.
282//===----------------------------------------------------------------------===//
283
284CFAbsoluteTime f1() {
285  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
286  CFDateRef date = CFDateCreate(0, t);
287  CFRetain(date);
288  CFRelease(date);
289  CFDateGetAbsoluteTime(date); // no-warning
290  CFRelease(date);
291  t = CFDateGetAbsoluteTime(date);   // expected-warning{{Reference-counted object is used after it is released}}
292  return t;
293}
294
295CFAbsoluteTime f2() {
296  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
297  CFDateRef date = CFDateCreate(0, t);  
298  [((NSDate*) date) retain];
299  CFRelease(date);
300  CFDateGetAbsoluteTime(date); // no-warning
301  [((NSDate*) date) release];
302  t = CFDateGetAbsoluteTime(date);   // expected-warning{{Reference-counted object is used after it is released}}
303  return t;
304}
305
306
307NSDate* global_x;
308
309// Test to see if we supresss an error when we store the pointer
310// to a global.
311
312CFAbsoluteTime f3() {
313  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
314  CFDateRef date = CFDateCreate(0, t);  
315  [((NSDate*) date) retain];
316  CFRelease(date);
317  CFDateGetAbsoluteTime(date); // no-warning
318  global_x = (NSDate*) date;  
319  [((NSDate*) date) release];
320  t = CFDateGetAbsoluteTime(date);   // no-warning
321  return t;
322}
323
324//---------------------------------------------------------------------------
325// Test case 'f4' differs for region store and basic store.  See
326// retain-release-region-store.m and retain-release-basic-store.m.
327//---------------------------------------------------------------------------
328
329// Test a leak.
330
331CFAbsoluteTime f5(int x) {  
332  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
333  CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}}
334  
335  if (x)
336    CFRelease(date);
337  
338  return t;
339}
340
341// Test a leak involving the return.
342
343CFDateRef f6(int x) {  
344  CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());  // expected-warning{{leak}}
345  CFRetain(date);
346  return date;
347}
348
349// Test a leak involving an overwrite.
350
351CFDateRef f7() {
352  CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());  //expected-warning{{leak}}
353  CFRetain(date);
354  date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning {{leak}}
355  return date;
356}
357
358// Generalization of Create rule.  MyDateCreate returns a CFXXXTypeRef, and
359// has the word create.
360CFDateRef MyDateCreate();
361
362CFDateRef f8() {
363  CFDateRef date = MyDateCreate(); // expected-warning{{leak}}
364  CFRetain(date);  
365  return date;
366}
367
368__attribute__((cf_returns_retained)) CFDateRef f9() {
369  CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // no-warning
370  int *p = 0;
371  // When allocations fail, CFDateCreate can return null.
372  if (!date) *p = 1; // expected-warning{{null}}
373  return date;
374}
375
376// Handle DiskArbitration API:
377//
378// http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/
379//
380void f10(io_service_t media, DADiskRef d, CFStringRef s) {
381  DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}}
382  if (disk) NSLog(@"ok");
383  
384  disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}}
385  if (disk) NSLog(@"ok");
386
387  CFDictionaryRef dict = DADiskCopyDescription(d);  // expected-warning{{leak}}
388  if (dict) NSLog(@"ok"); 
389  
390  disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}}
391  if (disk) NSLog(@"ok");
392    
393  DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault,   // expected-warning{{leak}}
394                                                kDAReturnSuccess, s);
395  if (dissenter) NSLog(@"ok");
396  
397  DASessionRef session = DASessionCreate(kCFAllocatorDefault);  // expected-warning{{leak}}
398  if (session) NSLog(@"ok");
399}
400
401// Test retain/release checker with CFString and CFMutableArray.
402void f11() {
403  // Create the array.
404  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
405
406  // Create a string.
407  CFStringRef s1 = CFStringCreateWithCString(0, "hello world",
408                                             kCFStringEncodingUTF8);
409
410  // Add the string to the array.
411  CFArrayAppendValue(A, s1);
412  
413  // Decrement the reference count.
414  CFRelease(s1); // no-warning
415  
416  // Get the string.  We don't own it.
417  s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0);
418  
419  // Release the array.
420  CFRelease(A); // no-warning
421  
422  // Release the string.  This is a bug.
423  CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}}
424}
425
426// PR 3337: Handle functions declared using typedefs.
427typedef CFTypeRef CREATEFUN();
428CREATEFUN MyCreateFun;
429
430void f12() {
431  CFTypeRef o = MyCreateFun(); // expected-warning {{leak}}
432}
433
434void f13_autorelease() {
435  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
436  [(id) A autorelease]; // no-warning
437}
438
439void f13_autorelease_b() {
440  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
441  [(id) A autorelease];
442  [(id) A autorelease]; // expected-warning{{Object sent -autorelease too many times}}
443}
444
445CFMutableArrayRef f13_autorelease_c() {
446  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
447  [(id) A autorelease];
448  [(id) A autorelease]; 
449  return A; // expected-warning{{Object sent -autorelease too many times}}
450}
451
452CFMutableArrayRef f13_autorelease_d() {
453  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
454  [(id) A autorelease];
455  [(id) A autorelease]; 
456  CFMutableArrayRef B = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{Object sent -autorelease too many times}}
457  CFRelease(B); // no-warning
458  while (1) {}
459}
460
461
462// This case exercises the logic where the leak site is the same as the allocation site.
463void f14_leakimmediately() {
464  CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
465}
466
467// Test that we track an allocated object beyond the point where the *name*
468// of the variable storing the reference is no longer live.
469void f15() {
470  // Create the array.
471  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
472  CFMutableArrayRef *B = &A;
473  // At this point, the name 'A' is no longer live.
474  CFRelease(*B);  // no-warning
475}
476
477// Test when we pass NULL to CFRetain/CFRelease.
478void f16(int x, CFTypeRef p) {
479  if (p)
480    return;
481
482  if (x) {
483    CFRelease(p); // expected-warning{{Null pointer argument in call to CFRelease}}
484  }
485  else {
486    CFRetain(p); // expected-warning{{Null pointer argument in call to CFRetain}}
487  }
488}
489
490// Test that an object is non-null after being CFRetained/CFReleased.
491void f17(int x, CFTypeRef p) {
492  if (x) {
493    CFRelease(p);
494    if (!p)
495      CFRelease(0); // no-warning
496  }
497  else {
498    CFRetain(p);
499    if (!p)
500      CFRetain(0); // no-warning
501  }
502}
503
504// Test basic tracking of ivars associated with 'self'.  For the retain/release
505// checker we currently do not want to flag leaks associated with stores
506// of tracked objects to ivars.
507@interface SelfIvarTest : NSObject {
508  id myObj;
509}
510- (void)test_self_tracking;
511@end
512
513@implementation SelfIvarTest
514- (void)test_self_tracking {
515  myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
516}
517@end
518
519// Test return of non-owned objects in contexts where an owned object
520// is expected.
521@interface TestReturnNotOwnedWhenExpectedOwned
522- (NSString*)newString;
523@end
524
525@implementation TestReturnNotOwnedWhenExpectedOwned
526- (NSString*)newString {
527  NSString *s = [NSString stringWithUTF8String:"hello"];
528  return s; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}}
529}
530@end
531
532// <rdar://problem/6659160>
533int isFoo(char c);
534
535static void rdar_6659160(char *inkind, char *inname)
536{
537  // We currently expect that [NSObject alloc] cannot fail.  This
538  // will be a toggled flag in the future.  It can indeed return null, but
539  // Cocoa programmers generally aren't expected to reason about out-of-memory
540  // conditions.
541  NSString *kind = [[NSString alloc] initWithUTF8String:inkind];  // expected-warning{{leak}}
542  
543  // We do allow stringWithUTF8String to fail.  This isn't really correct, as
544  // far as returning 0.  In most error conditions it will throw an exception.
545  // If allocation fails it could return 0, but again this
546  // isn't expected.
547  NSString *name = [NSString stringWithUTF8String:inname];
548  if(!name)
549    return;
550
551  const char *kindC = 0;
552  const char *nameC = 0;
553  
554  // In both cases, we cannot reach a point down below where we
555  // dereference kindC or nameC with either being null.  This is because
556  // we assume that [NSObject alloc] doesn't fail and that we have the guard
557  // up above.
558  
559  if(kind)
560    kindC = [kind UTF8String];
561  if(name)
562    nameC = [name UTF8String];
563  if(!isFoo(kindC[0])) // expected-warning{{null}}
564    return;
565  if(!isFoo(nameC[0])) // no-warning
566    return;
567
568  [kind release];
569  [name release]; // expected-warning{{Incorrect decrement of the reference count}}
570}
571
572// PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming
573//  conventions with respect to 'return'ing ownership.
574@interface PR3677: NSObject @end
575@implementation PR3677
576+ (id)allocWithZone:(NSZone *)inZone {
577  return [super allocWithZone:inZone];  // no-warning
578}
579@end
580
581// PR 3820 - Reason about calls to -dealloc
582void pr3820_DeallocInsteadOfRelease(void)
583{
584  id foo = [[NSString alloc] init]; // no-warning
585  [foo dealloc];
586  // foo is not leaked, since it has been deallocated.
587}
588
589void pr3820_ReleaseAfterDealloc(void)
590{
591  id foo = [[NSString alloc] init];
592  [foo dealloc];
593  [foo release];  // expected-warning{{used after it is release}}
594  // NSInternalInconsistencyException: message sent to deallocated object
595}
596
597void pr3820_DeallocAfterRelease(void)
598{
599  NSLog(@"\n\n[%s]", __FUNCTION__);
600  id foo = [[NSString alloc] init];
601  [foo release];
602  [foo dealloc]; // expected-warning{{used after it is released}}
603  // message sent to released object
604}
605
606// From <rdar://problem/6704930>.  The problem here is that 'length' binds to
607// '($0 - 1)' after '--length', but SimpleConstraintManager doesn't know how to
608// reason about '($0 - 1) > constant'.  As a temporary hack, we drop the value
609// of '($0 - 1)' and conjure a new symbol.
610void rdar6704930(unsigned char *s, unsigned int length) {
611  NSString* name = 0;
612  if (s != 0) {
613    if (length > 0) {
614      while (length > 0) {
615        if (*s == ':') {
616          ++s;
617          --length;
618          name = [[NSString alloc] init]; // no-warning
619          break;
620        }
621        ++s;
622        --length;
623      }
624      if ((length == 0) && (name != 0)) {
625        [name release];
626        name = 0;
627      }
628      if (length == 0) { // no ':' found -> use it all as name
629        name = [[NSString alloc] init]; // no-warning
630      }
631    }
632  }
633
634  if (name != 0) {
635    [name release];
636  }
637}
638
639//===----------------------------------------------------------------------===//
640// <rdar://problem/6833332>
641// One build of the analyzer accidentally stopped tracking the allocated
642// object after the 'retain'.
643//===----------------------------------------------------------------------===//
644
645@interface rdar_6833332 : NSObject <NSApplicationDelegate> {
646    NSWindow *window;
647}
648@property (nonatomic, retain) NSWindow *window;
649@end
650
651@implementation rdar_6833332
652@synthesize window;
653- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
654 NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}}
655
656 [dict setObject:@"foo" forKey:@"bar"];
657
658 NSLog(@"%@", dict);
659}
660- (void)dealloc {
661    [window release];
662    [super dealloc];
663}
664
665- (void)radar10102244 {
666 NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}} 
667 if (window) 
668   NSLog(@"%@", window);    
669}
670@end
671
672//===----------------------------------------------------------------------===//
673// <rdar://problem/6257780> clang checker fails to catch use-after-release
674//===----------------------------------------------------------------------===//
675                                 
676int rdar_6257780_Case1() {
677  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
678  NSArray *array = [NSArray array];
679  [array release]; // expected-warning{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}}
680  [pool drain];
681  return 0;
682}
683
684//===----------------------------------------------------------------------===//
685// <rdar://problem/6866843> Checker should understand new/setObject:/release constructs
686//===----------------------------------------------------------------------===//
687
688void rdar_6866843() {
689 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
690 NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
691 NSArray* array = [[NSArray alloc] init];
692 [dictionary setObject:array forKey:@"key"];
693 [array release];
694 // Using 'array' here should be fine
695 NSLog(@"array = %@\n", array); // no-warning
696 // Now the array is released
697 [dictionary release];
698 [pool drain];
699}
700
701
702//===----------------------------------------------------------------------===//
703// <rdar://problem/6877235> Classes typedef-ed to CF objects should get the same treatment as CF objects
704//===----------------------------------------------------------------------===//
705
706typedef CFTypeRef OtherRef;
707
708@interface RDar6877235 : NSObject {}
709- (CFTypeRef)_copyCFTypeRef;
710- (OtherRef)_copyOtherRef;
711@end
712
713@implementation RDar6877235
714- (CFTypeRef)_copyCFTypeRef {
715  return [[NSString alloc] init]; // no-warning
716}
717- (OtherRef)_copyOtherRef {
718  return [[NSString alloc] init]; // no-warning
719}
720@end
721
722//===----------------------------------------------------------------------===//
723//<rdar://problem/6320065> false positive - init method returns an object
724// owned by caller
725//===----------------------------------------------------------------------===//
726
727@interface RDar6320065 : NSObject {
728  NSString *_foo;
729}
730- (id)initReturningNewClass;
731- (id)_initReturningNewClassBad;
732- (id)initReturningNewClassBad2;
733@end
734
735@interface RDar6320065Subclass : RDar6320065
736@end
737
738@implementation RDar6320065
739- (id)initReturningNewClass {
740  [self release];
741  self = [[RDar6320065Subclass alloc] init]; // no-warning
742  return self;
743}
744- (id)_initReturningNewClassBad {
745  [self release];
746  [[RDar6320065Subclass alloc] init]; // expected-warning {{leak}}
747  return self;
748}
749- (id)initReturningNewClassBad2 {
750  [self release];
751  self = [[RDar6320065Subclass alloc] init];
752  return [self autorelease]; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}}
753}
754
755@end
756
757@implementation RDar6320065Subclass
758@end
759
760int RDar6320065_test() {
761  RDar6320065 *test = [[RDar6320065 alloc] init]; // no-warning
762  [test release];
763  return 0;
764}
765
766//===----------------------------------------------------------------------===//
767// <rdar://problem/7129086> -awakeAfterUsingCoder: returns an owned object 
768//  and claims the receiver
769//===----------------------------------------------------------------------===//
770
771@interface RDar7129086 : NSObject {} @end
772@implementation RDar7129086
773- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder {
774  [self release]; // no-warning
775  return [NSString alloc];  // no-warning
776}
777@end
778
779//===----------------------------------------------------------------------===//
780// <rdar://problem/6859457> [NSData dataWithBytesNoCopy] does not return a
781//  retained object
782//===----------------------------------------------------------------------===//
783
784@interface RDar6859457 : NSObject {}
785- (NSString*) NoCopyString;
786- (NSString*) noCopyString;
787@end
788
789@implementation RDar6859457 
790- (NSString*) NoCopyString { return [[NSString alloc] init]; } // expected-warning{{leak}}
791- (NSString*) noCopyString { return [[NSString alloc] init]; } // expected-warning{{leak}}
792@end
793
794void test_RDar6859457(RDar6859457 *x, void *bytes, NSUInteger dataLength) {
795  [x NoCopyString]; // no-warning
796  [x noCopyString]; // no-warning
797  [NSData dataWithBytesNoCopy:bytes length:dataLength];  // no-warning
798  [NSData dataWithBytesNoCopy:bytes length:dataLength freeWhenDone:1]; // no-warning
799}
800
801//===----------------------------------------------------------------------===//
802// PR 4230 - an autorelease pool is not necessarily leaked during a premature
803//  return
804//===----------------------------------------------------------------------===//
805
806static void PR4230(void)
807{
808  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // no-warning
809  NSString *object = [[[NSString alloc] init] autorelease]; // no-warning
810  return;
811}
812
813//===----------------------------------------------------------------------===//
814// Method name that has a null IdentifierInfo* for its first selector slot.
815// This test just makes sure that we handle it.
816//===----------------------------------------------------------------------===//
817
818@interface TestNullIdentifier
819@end
820
821@implementation TestNullIdentifier
822+ (id):(int)x, ... {
823  return [[NSString alloc] init]; // expected-warning{{leak}}
824}
825@end
826
827//===----------------------------------------------------------------------===//
828// <rdar://problem/6893565> don't flag leaks for return types that cannot be 
829//                          determined to be CF types
830//===----------------------------------------------------------------------===//
831
832// We don't know if 'struct s6893565' represents a Core Foundation type, so
833// we shouldn't emit an error here.
834typedef struct s6893565* TD6893565;
835
836@interface RDar6893565 {}
837-(TD6893565)newThing;
838@end
839
840@implementation RDar6893565
841-(TD6893565)newThing {  
842  return (TD6893565) [[NSString alloc] init]; // no-warning
843}
844@end
845
846//===----------------------------------------------------------------------===//
847// <rdar://problem/6902710> clang: false positives w/QC and CoreImage methods
848//===----------------------------------------------------------------------===//
849
850void rdar6902710(QCView *view, QCRenderer *renderer, CIContext *context,
851                 NSString *str, CIImage *img, CGRect rect,
852                 CIFormat form, CGColorSpaceRef cs) {
853  [view createSnapshotImageOfType:str]; // expected-warning{{leak}}
854  [renderer createSnapshotImageOfType:str]; // expected-warning{{leak}}
855  [context createCGImage:img fromRect:rect]; // expected-warning{{leak}}
856  [context createCGImage:img fromRect:rect format:form colorSpace:cs]; // expected-warning{{leak}}
857}
858
859//===----------------------------------------------------------------------===//
860// <rdar://problem/6945561> -[CIContext createCGLayerWithSize:info:]
861//                           misinterpreted by clang scan-build
862//===----------------------------------------------------------------------===//
863
864void rdar6945561(CIContext *context, CGSize size, CFDictionaryRef d) {
865  [context createCGLayerWithSize:size info:d]; // expected-warning{{leak}}
866}
867
868//===----------------------------------------------------------------------===//
869// <rdar://problem/6961230> add knowledge of IOKit functions to retain/release 
870//                          checker
871//===----------------------------------------------------------------------===//
872
873void IOBSDNameMatching_wrapper(mach_port_t masterPort, uint32_t options,  const char * bsdName) {  
874  IOBSDNameMatching(masterPort, options, bsdName); // expected-warning{{leak}}
875}
876
877void IOServiceMatching_wrapper(const char * name) {
878  IOServiceMatching(name); // expected-warning{{leak}}
879}
880
881void IOServiceNameMatching_wrapper(const char * name) {
882  IOServiceNameMatching(name); // expected-warning{{leak}}
883}
884
885CF_RETURNS_RETAINED CFDictionaryRef CreateDict();
886
887void IOServiceAddNotification_wrapper(mach_port_t masterPort, const io_name_t notificationType,
888  mach_port_t wakePort, uintptr_t reference, io_iterator_t * notification ) {
889
890  CFDictionaryRef matching = CreateDict();
891  CFRelease(matching);
892  IOServiceAddNotification(masterPort, notificationType, matching, // expected-warning{{used after it is released}} expected-warning{{deprecated}}
893                           wakePort, reference, notification);
894}
895
896void IORegistryEntryIDMatching_wrapper(uint64_t entryID ) {
897  IORegistryEntryIDMatching(entryID); // expected-warning{{leak}}
898}
899
900void IOOpenFirmwarePathMatching_wrapper(mach_port_t masterPort, uint32_t options,
901                                        const char * path) {
902  IOOpenFirmwarePathMatching(masterPort, options, path); // expected-warning{{leak}}
903}
904
905void IOServiceGetMatchingService_wrapper(mach_port_t masterPort) {
906  CFDictionaryRef matching = CreateDict();
907  IOServiceGetMatchingService(masterPort, matching);
908  CFRelease(matching); // expected-warning{{used after it is released}}
909}
910
911void IOServiceGetMatchingServices_wrapper(mach_port_t masterPort, io_iterator_t *existing) {
912  CFDictionaryRef matching = CreateDict();
913  IOServiceGetMatchingServices(masterPort, matching, existing);
914  CFRelease(matching); // expected-warning{{used after it is released}}
915}
916
917void IOServiceAddMatchingNotification_wrapper(IONotificationPortRef notifyPort, const io_name_t notificationType, 
918  IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification) {
919    
920  CFDictionaryRef matching = CreateDict();
921  IOServiceAddMatchingNotification(notifyPort, notificationType, matching, callback, refCon, notification);
922  CFRelease(matching); // expected-warning{{used after it is released}}
923}
924
925//===----------------------------------------------------------------------===//
926// Test of handling objects whose references "escape" to containers.
927//===----------------------------------------------------------------------===//
928
929void CFDictionaryAddValue(CFMutableDictionaryRef, void *, void *);
930
931// <rdar://problem/6539791>
932void rdar_6539791(CFMutableDictionaryRef y, void* key, void* val_key) {
933  CFMutableDictionaryRef x = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
934  CFDictionaryAddValue(y, key, x);
935  CFRelease(x); // the dictionary keeps a reference, so the object isn't deallocated yet
936  signed z = 1;
937  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);
938  if (value) {
939    CFDictionaryAddValue(x, val_key, (void*)value); // no-warning
940    CFRelease(value);
941    CFDictionaryAddValue(y, val_key, (void*)value); // no-warning
942  }
943}
944
945// <rdar://problem/6560661>
946// Same issue, except with "AppendValue" functions.
947void rdar_6560661(CFMutableArrayRef x) {
948  signed z = 1;
949  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);
950  // CFArrayAppendValue keeps a reference to value.
951  CFArrayAppendValue(x, value);
952  CFRelease(value);
953  CFRetain(value);
954  CFRelease(value); // no-warning
955}
956
957// <rdar://problem/7152619>
958// Same issue, excwept with "CFAttributeStringSetAttribute".
959void rdar_7152619(CFStringRef str) {
960  CFAttributedStringRef string = CFAttributedStringCreate(kCFAllocatorDefault, str, 0);
961  CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutableCopy(kCFAllocatorDefault, 100, string);
962  CFRelease(string);
963  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
964  CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 1), str, number);
965  [number release];
966  [number retain];
967  CFRelease(attrString);  
968}
969
970//===----------------------------------------------------------------------===//
971// Test of handling CGGradientXXX functions.
972//===----------------------------------------------------------------------===//
973
974void rdar_7184450(CGContextRef myContext, CGFloat x, CGPoint myStartPoint,
975                  CGPoint myEndPoint) {
976  size_t num_locations = 6;
977  CGFloat locations[6] = { 0.0, 0.265, 0.28, 0.31, 0.36, 1.0 };
978  CGFloat components[28] = { 239.0/256.0, 167.0/256.0, 170.0/256.0,
979     x,  // Start color
980    207.0/255.0, 39.0/255.0, 39.0/255.0, x,
981    147.0/255.0, 21.0/255.0, 22.0/255.0, x,
982    175.0/255.0, 175.0/255.0, 175.0/255.0, x,
983    255.0/255.0,255.0/255.0, 255.0/255.0, x,
984    255.0/255.0,255.0/255.0, 255.0/255.0, x
985  }; // End color
986  
987  CGGradientRef myGradient =
988    CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), // expected-warning{{leak}}
989      components, locations, num_locations);
990
991  CGContextDrawLinearGradient(myContext, myGradient, myStartPoint, myEndPoint,
992                              0);
993  CGGradientRelease(myGradient);
994}
995
996void rdar_7184450_pos(CGContextRef myContext, CGFloat x, CGPoint myStartPoint,
997                  CGPoint myEndPoint) {
998  size_t num_locations = 6;
999  CGFloat locations[6] = { 0.0, 0.265, 0.28, 0.31, 0.36, 1.0 };
1000  CGFloat components[28] = { 239.0/256.0, 167.0/256.0, 170.0/256.0,
1001     x,  // Start color
1002    207.0/255.0, 39.0/255.0, 39.0/255.0, x,
1003    147.0/255.0, 21.0/255.0, 22.0/255.0, x,
1004    175.0/255.0, 175.0/255.0, 175.0/255.0, x,
1005    255.0/255.0,255.0/255.0, 255.0/255.0, x,
1006    255.0/255.0,255.0/255.0, 255.0/255.0, x
1007  }; // End color
1008  
1009  CGGradientRef myGradient =
1010   CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, num_locations); // expected-warning 2 {{leak}}
1011
1012  CGContextDrawLinearGradient(myContext, myGradient, myStartPoint, myEndPoint,
1013                              0);
1014}
1015
1016//===----------------------------------------------------------------------===//
1017// <rdar://problem/7299394> clang false positive: retained instance passed to
1018//                          thread in pthread_create marked as leak
1019//
1020// Until we have full IPA, the analyzer should stop tracking the reference
1021// count of objects passed to pthread_create.
1022//
1023//===----------------------------------------------------------------------===//
1024
1025struct _opaque_pthread_t {};
1026struct _opaque_pthread_attr_t {};
1027typedef struct _opaque_pthread_t *__darwin_pthread_t;
1028typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t;
1029typedef __darwin_pthread_t pthread_t;
1030typedef __darwin_pthread_attr_t pthread_attr_t;
1031
1032int pthread_create(pthread_t *, const pthread_attr_t *,
1033                   void *(*)(void *), void *);
1034
1035void *rdar_7299394_start_routine(void *p) {
1036  [((id) p) release];
1037  return 0;
1038}
1039void rdar_7299394(pthread_attr_t *attr, pthread_t *thread, void *args) {
1040  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1041  pthread_create(thread, attr, rdar_7299394_start_routine, number);
1042}
1043void rdar_7299394_positive(pthread_attr_t *attr, pthread_t *thread) {
1044  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1045}
1046
1047//===----------------------------------------------------------------------===//
1048// <rdar://problem/7283567> False leak associated with call to 
1049//                          CVPixelBufferCreateWithBytes ()
1050//
1051// According to the Core Video Reference (ADC), CVPixelBufferCreateWithBytes and
1052// CVPixelBufferCreateWithPlanarBytes can release (via a callback) the
1053// pixel buffer object.  These test cases show how the analyzer stops tracking
1054// the reference count for the objects passed for this argument.  This
1055// could be made smarter.
1056//===----------------------------------------------------------------------===//
1057
1058typedef int int32_t;
1059typedef UInt32 FourCharCode;
1060typedef FourCharCode OSType;
1061typedef uint64_t CVOptionFlags;
1062typedef int32_t CVReturn;
1063typedef struct __CVBuffer *CVBufferRef;
1064typedef CVBufferRef CVImageBufferRef;
1065typedef CVImageBufferRef CVPixelBufferRef;
1066typedef void (*CVPixelBufferReleaseBytesCallback)( void *releaseRefCon, const void *baseAddress );
1067
1068extern CVReturn CVPixelBufferCreateWithBytes(CFAllocatorRef allocator,
1069            size_t width,
1070            size_t height,
1071            OSType pixelFormatType,
1072            void *baseAddress,
1073            size_t bytesPerRow,
1074            CVPixelBufferReleaseBytesCallback releaseCallback,
1075            void *releaseRefCon,
1076            CFDictionaryRef pixelBufferAttributes,
1077                   CVPixelBufferRef *pixelBufferOut) ;
1078
1079typedef void (*CVPixelBufferReleasePlanarBytesCallback)( void *releaseRefCon, const void *dataPtr, size_t dataSize, size_t numberOfPlanes, const void *planeAddresses[] );
1080
1081extern CVReturn CVPixelBufferCreateWithPlanarBytes(CFAllocatorRef allocator,
1082        size_t width,
1083        size_t height,
1084        OSType pixelFormatType,
1085        void *dataPtr,
1086        size_t dataSize,
1087        size_t numberOfPlanes,
1088        void *planeBaseAddress[],
1089        size_t planeWidth[],
1090        size_t planeHeight[],
1091        size_t planeBytesPerRow[],
1092        CVPixelBufferReleasePlanarBytesCallback releaseCallback,
1093        void *releaseRefCon,
1094        CFDictionaryRef pixelBufferAttributes,
1095        CVPixelBufferRef *pixelBufferOut) ;
1096
1097extern CVReturn CVPixelBufferCreateWithBytes(CFAllocatorRef allocator,
1098            size_t width,
1099            size_t height,
1100            OSType pixelFormatType,
1101            void *baseAddress,
1102            size_t bytesPerRow,
1103            CVPixelBufferReleaseBytesCallback releaseCallback,
1104            void *releaseRefCon,
1105            CFDictionaryRef pixelBufferAttributes,
1106                   CVPixelBufferRef *pixelBufferOut) ;
1107
1108CVReturn rdar_7283567(CFAllocatorRef allocator, size_t width, size_t height,
1109                      OSType pixelFormatType, void *baseAddress,
1110                      size_t bytesPerRow,
1111                      CVPixelBufferReleaseBytesCallback releaseCallback,
1112                      CFDictionaryRef pixelBufferAttributes,
1113                      CVPixelBufferRef *pixelBufferOut) {
1114
1115  // For the allocated object, it doesn't really matter what type it is
1116  // for the purpose of this test.  All we want to show is that
1117  // this is freed later by the callback.
1118  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1119  
1120  return CVPixelBufferCreateWithBytes(allocator, width, height, pixelFormatType,
1121                                baseAddress, bytesPerRow, releaseCallback,
1122                                number, // potentially released by callback
1123                                pixelBufferAttributes, pixelBufferOut) ;
1124}
1125
1126CVReturn rdar_7283567_2(CFAllocatorRef allocator, size_t width, size_t height,
1127        OSType pixelFormatType, void *dataPtr, size_t dataSize,
1128        size_t numberOfPlanes, void *planeBaseAddress[],
1129        size_t planeWidth[], size_t planeHeight[], size_t planeBytesPerRow[],
1130        CVPixelBufferReleasePlanarBytesCallback releaseCallback,
1131        CFDictionaryRef pixelBufferAttributes,
1132        CVPixelBufferRef *pixelBufferOut) {
1133    
1134    // For the allocated object, it doesn't really matter what type it is
1135    // for the purpose of this test.  All we want to show is that
1136    // this is freed later by the callback.
1137    NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1138
1139    return CVPixelBufferCreateWithPlanarBytes(allocator,
1140              width, height, pixelFormatType, dataPtr, dataSize,
1141              numberOfPlanes, planeBaseAddress, planeWidth,
1142              planeHeight, planeBytesPerRow, releaseCallback,
1143              number, // potentially released by callback
1144              pixelBufferAttributes, pixelBufferOut) ;
1145}
1146
1147//===----------------------------------------------------------------------===//
1148// <rdar://problem/7358899> False leak associated with 
1149//  CGBitmapContextCreateWithData
1150//===----------------------------------------------------------------------===//
1151typedef uint32_t CGBitmapInfo;
1152typedef void (*CGBitmapContextReleaseDataCallback)(void *releaseInfo, void *data);
1153    
1154CGContextRef CGBitmapContextCreateWithData(void *data,
1155    size_t width, size_t height, size_t bitsPerComponent,
1156    size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo,
1157    CGBitmapContextReleaseDataCallback releaseCallback, void *releaseInfo);
1158
1159void rdar_7358899(void *data,
1160      size_t width, size_t height, size_t bitsPerComponent,
1161      size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo,
1162      CGBitmapContextReleaseDataCallback releaseCallback) {
1163
1164    // For the allocated object, it doesn't really matter what type it is
1165    // for the purpose of this test.  All we want to show is that
1166    // this is freed later by the callback.
1167    NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1168
1169  CGBitmapContextCreateWithData(data, width, height, bitsPerComponent, // expected-warning{{leak}}
1170    bytesPerRow, space, bitmapInfo, releaseCallback, number);
1171}
1172
1173//===----------------------------------------------------------------------===//
1174// <rdar://problem/7265711> allow 'new', 'copy', 'alloc', 'init' prefix to
1175//  start before '_' when determining Cocoa fundamental rule
1176//
1177// Previously the retain/release checker just skipped prefixes before the
1178// first '_' entirely.  Now the checker honors the prefix if it results in a
1179// recognizable naming convention (e.g., 'new', 'init').
1180//===----------------------------------------------------------------------===//
1181
1182@interface RDar7265711 {}
1183- (id) new_stuff;
1184@end
1185
1186void rdar7265711_a(RDar7265711 *x) {
1187  id y = [x new_stuff]; // expected-warning{{leak}}
1188}
1189
1190void rdar7265711_b(RDar7265711 *x) {
1191  id y = [x new_stuff]; // no-warning
1192  [y release];
1193}
1194
1195//===----------------------------------------------------------------------===//
1196// <rdar://problem/7306898> clang thinks [NSCursor dragCopyCursor] returns a
1197//                          retained reference
1198//===----------------------------------------------------------------------===//
1199
1200@interface NSCursor : NSObject
1201+ (NSCursor *)dragCopyCursor;
1202@end
1203
1204void rdar7306898(void) {
1205  // 'dragCopyCursor' does not follow Cocoa's fundamental rule.  It is a noun, not an sentence
1206  // implying a 'copy' of something.
1207  NSCursor *c =  [NSCursor dragCopyCursor]; // no-warning
1208  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1209}
1210
1211//===----------------------------------------------------------------------===//
1212// <rdar://problem/7252064> sending 'release', 'retain', etc. to a Class
1213// directly is not likely what the user intended
1214//===----------------------------------------------------------------------===//
1215
1216@interface RDar7252064 : NSObject @end
1217void rdar7252064(void) {
1218  [RDar7252064 release]; // expected-warning{{The 'release' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1219  [RDar7252064 retain]; // expected-warning{{The 'retain' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1220  [RDar7252064 autorelease]; // expected-warning{{The 'autorelease' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1221  [NSAutoreleasePool drain]; // expected-warning{{method '+drain' not found}} expected-warning{{The 'drain' message should be sent to instances of class 'NSAutoreleasePool' and not the class directly}}
1222}
1223
1224//===----------------------------------------------------------------------===//
1225// Tests of ownership attributes.
1226//===----------------------------------------------------------------------===//
1227
1228typedef NSString* MyStringTy;
1229
1230@protocol FooP;
1231
1232@interface TestOwnershipAttr : NSObject
1233- (NSString*) returnsAnOwnedString  NS_RETURNS_RETAINED; // no-warning
1234- (NSString*) returnsAnOwnedCFString  CF_RETURNS_RETAINED; // no-warning
1235- (MyStringTy) returnsAnOwnedTypedString NS_RETURNS_RETAINED; // no-warning
1236- (NSString*) newString NS_RETURNS_NOT_RETAINED; // no-warning
1237- (NSString*) newStringNoAttr;
1238- (int) returnsAnOwnedInt NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to methods that return an Objective-C object}}
1239- (id) pseudoInit NS_CONSUMES_SELF NS_RETURNS_RETAINED;
1240+ (void) consume:(id) NS_CONSUMED x;
1241+ (void) consume2:(id) CF_CONSUMED x;
1242@end
1243
1244static int ownership_attribute_doesnt_go_here NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to functions and methods}}
1245
1246void test_attr_1(TestOwnershipAttr *X) {
1247  NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
1248}
1249
1250void test_attr_1b(TestOwnershipAttr *X) {
1251  NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
1252}
1253
1254void test_attr1c(TestOwnershipAttr *X) {
1255  NSString *str = [X newString]; // no-warning
1256  NSString *str2 = [X newStringNoAttr]; // expected-warning{{leak}}
1257}
1258
1259void testattr2_a() {
1260  TestOwnershipAttr *x = [TestOwnershipAttr alloc]; // expected-warning{{leak}}
1261}
1262
1263void testattr2_b() {
1264  TestOwnershipAttr *x = [[TestOwnershipAttr alloc] pseudoInit];  // expected-warning{{leak}}
1265}
1266
1267void testattr2_c() {
1268  TestOwnershipAttr *x = [[TestOwnershipAttr alloc] pseudoInit]; // no-warning
1269  [x release];
1270}
1271
1272void testattr3() {
1273  TestOwnershipAttr *x = [TestOwnershipAttr alloc]; // no-warning
1274  [TestOwnershipAttr consume:x];
1275  TestOwnershipAttr *y = [TestOwnershipAttr alloc]; // no-warning
1276  [TestOwnershipAttr consume2:y];
1277}
1278
1279void consume_ns(id NS_CONSUMED x);
1280void consume_cf(id CF_CONSUMED x);
1281
1282void testattr4() {
1283  TestOwnershipAttr *x = [TestOwnershipAttr alloc]; // no-warning
1284  consume_ns(x);
1285  TestOwnershipAttr *y = [TestOwnershipAttr alloc]; // no-warning
1286  consume_cf(y);
1287}
1288
1289
1290@interface MyClassTestCFAttr : NSObject {}
1291- (NSDate*) returnsCFRetained CF_RETURNS_RETAINED;
1292- (CFDateRef) returnsCFRetainedAsCF CF_RETURNS_RETAINED;
1293- (CFDateRef) newCFRetainedAsCF CF_RETURNS_NOT_RETAINED;
1294- (CFDateRef) newCFRetainedAsCFNoAttr;
1295- (NSDate*) alsoReturnsRetained;
1296- (CFDateRef) alsoReturnsRetainedAsCF;
1297- (NSDate*) returnsNSRetained NS_RETURNS_RETAINED;
1298@end
1299
1300CF_RETURNS_RETAINED
1301CFDateRef returnsRetainedCFDate()  {
1302  return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
1303}
1304
1305@implementation MyClassTestCFAttr
1306- (NSDate*) returnsCFRetained {
1307  return (NSDate*) returnsRetainedCFDate(); // No leak.
1308}
1309
1310- (CFDateRef) returnsCFRetainedAsCF {
1311  return returnsRetainedCFDate(); // No leak.
1312}
1313
1314- (CFDateRef) newCFRetainedAsCF {
1315  return (CFDateRef)[(id)[self returnsCFRetainedAsCF] autorelease];
1316}
1317
1318- (CFDateRef) newCFRetainedAsCFNoAttr {
1319  return (CFDateRef)[(id)[self returnsCFRetainedAsCF] autorelease]; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}}
1320}
1321
1322- (NSDate*) alsoReturnsRetained {
1323  return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}}
1324}
1325
1326- (CFDateRef) alsoReturnsRetainedAsCF {
1327  return returnsRetainedCFDate(); // expected-warning{{leak}}
1328}
1329
1330
1331- (NSDate*) returnsNSRetained {
1332  return (NSDate*) returnsRetainedCFDate(); // no-warning
1333}
1334@end
1335
1336//===----------------------------------------------------------------------===//
1337// Test that leaks post-dominated by "panic" functions are not reported.
1338//
1339// <rdar://problem/5905851> do not report a leak when post-dominated by a call
1340// to a noreturn or panic function
1341//===----------------------------------------------------------------------===//
1342
1343void panic() __attribute__((noreturn));
1344void panic_not_in_hardcoded_list() __attribute__((noreturn));
1345
1346void test_panic_negative() {
1347  signed z = 1;
1348  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);  // expected-warning{{leak}}
1349}
1350
1351void test_panic_positive() {
1352  signed z = 1;
1353  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning
1354  panic();
1355}
1356
1357void test_panic_neg_2(int x) {
1358  signed z = 1;
1359  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // expected-warning{{leak}}
1360  if (x)
1361    panic();
1362}
1363
1364void test_panic_pos_2(int x) {
1365  signed z = 1;
1366  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning
1367  if (x)
1368    panic();
1369  if (!x) {
1370    // This showed up in <rdar://problem/7796563>, where we silently missed checking
1371    // the function type for noreturn.  "panic()" is a hard-coded known panic function
1372    // that isn't always noreturn.
1373    panic_not_in_hardcoded_list();
1374  }
1375}
1376
1377//===----------------------------------------------------------------------===//
1378// Test uses of blocks (closures)
1379//===----------------------------------------------------------------------===//
1380
1381void test_blocks_1_pos(void) {
1382  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1383  ^{}();
1384}
1385
1386void test_blocks_1_indirect_release(void) {
1387  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1388  ^{ [number release]; }();
1389}
1390
1391void test_blocks_1_indirect_retain(void) {
1392  // Eventually this should be reported as a leak.
1393  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1394  ^{ [number retain]; }();
1395}
1396
1397void test_blocks_1_indirect_release_via_call(void) {
1398  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1399  ^(NSObject *o){ [o release]; }(number);
1400}
1401
1402void test_blocks_1_indirect_retain_via_call(void) {
1403  // Eventually this should be reported as a leak.
1404  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1405  ^(NSObject *o){ [o retain]; }(number);
1406}
1407
1408//===--------------------------------------------------------------------===//
1409// Test sending message to super that returns an object alias.  Previously
1410// this caused a crash in the analyzer.
1411//===--------------------------------------------------------------------===//
1412
1413@interface Rdar8015556 : NSObject {} @end
1414@implementation Rdar8015556
1415- (id)retain {
1416  return [super retain];
1417}
1418@end
1419
1420// <rdar://problem/8272168> - Correcly handle Class<...> in Cocoa Conventions
1421// detector.
1422
1423@protocol Prot_R8272168 @end
1424Class <Prot_R8272168> GetAClassThatImplementsProt_R8272168();
1425void r8272168() {
1426  GetAClassThatImplementsProt_R8272168();
1427}
1428
1429// Test case for <rdar://problem/8356342>, which in the past triggered
1430// a false positive.
1431@interface RDar8356342
1432- (NSDate*) rdar8356342:(NSDate *)inValue;
1433@end
1434
1435@implementation RDar8356342
1436- (NSDate*) rdar8356342:(NSDate*)inValue {
1437  NSDate *outValue = inValue;
1438  if (outValue == 0)
1439    outValue = [[NSDate alloc] init]; // no-warning
1440
1441  if (outValue != inValue)
1442    [outValue autorelease];
1443
1444  return outValue;
1445}
1446@end
1447
1448// <rdar://problem/8724287> - This test case previously crashed because
1449// of a bug in BugReporter.
1450extern const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key);
1451typedef struct __CFError * CFErrorRef;
1452extern const CFStringRef kCFErrorUnderlyingErrorKey;
1453extern CFDictionaryRef CFErrorCopyUserInfo(CFErrorRef err);
1454static void rdar_8724287(CFErrorRef error)
1455{
1456    CFErrorRef error_to_dump;
1457
1458    error_to_dump = error;
1459    while (error_to_dump != ((void*)0)) {
1460        CFDictionaryRef info;
1461
1462        info = CFErrorCopyUserInfo(error_to_dump); // expected-warning{{Potential leak of an object allocated on line}}
1463
1464        if (info != ((void*)0)) {
1465        }
1466
1467        error_to_dump = (CFErrorRef) CFDictionaryGetValue(info, kCFErrorUnderlyingErrorKey);
1468    }
1469}
1470
1471// <rdar://problem/9234108> - Make sure the model applies cf_consumed
1472// correctly in argument positions besides the first.
1473extern void *CFStringCreate(void);
1474extern void rdar_9234108_helper(void *key, void * CF_CONSUMED value);
1475void rdar_9234108() {
1476  rdar_9234108_helper(0, CFStringCreate());
1477}
1478
1479// <rdar://problem/9726279> - Make sure that objc_method_family works
1480// to override naming conventions.
1481struct TwoDoubles {
1482  double one;
1483  double two;
1484};
1485typedef struct TwoDoubles TwoDoubles;
1486
1487@interface NSValue (Mine)
1488- (id)_prefix_initWithTwoDoubles:(TwoDoubles)twoDoubles __attribute__((objc_method_family(init)));
1489@end
1490
1491@implementation NSValue (Mine)
1492- (id)_prefix_initWithTwoDoubles:(TwoDoubles)twoDoubles
1493{
1494  return [self init];
1495}
1496@end
1497
1498void rdar9726279() {
1499  TwoDoubles twoDoubles = { 0.0, 0.0 };
1500  NSValue *value = [[NSValue alloc] _prefix_initWithTwoDoubles:twoDoubles];
1501  [value release];
1502}
1503
1504// <rdar://problem/9732321>
1505// Test camelcase support for CF conventions.  While Core Foundation APIs
1506// don't use camel casing, other code is allowed to use it.
1507CFArrayRef camelcase_create_1() {
1508  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1509}
1510
1511CFArrayRef camelcase_createno() {
1512  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}}
1513}
1514
1515CFArrayRef camelcase_copy() {
1516  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1517}
1518
1519CFArrayRef camelcase_copying() {
1520  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}}
1521}
1522
1523CFArrayRef copyCamelCase() {
1524  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1525}
1526
1527CFArrayRef __copyCamelCase() {
1528  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1529}
1530
1531CFArrayRef __createCamelCase() {
1532  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1533}
1534
1535CFArrayRef camel_create() {
1536  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1537}
1538
1539
1540CFArrayRef camel_creat() {
1541  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}}
1542}
1543
1544CFArrayRef camel_copy() {
1545  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1546}
1547
1548CFArrayRef camel_copyMachine() {
1549  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1550}
1551
1552CFArrayRef camel_copymachine() {
1553  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}}
1554}
1555
1556// rdar://problem/8024350
1557@protocol F18P
1558- (id) clone;
1559@end
1560@interface F18 : NSObject<F18P> @end
1561@interface F18(Cat)
1562- (id) clone NS_RETURNS_RETAINED;
1563@end
1564
1565@implementation F18
1566- (id) clone {
1567  return [F18 alloc];
1568}
1569@end
1570
1571// Radar 6582778.
1572void rdar6582778(void) {
1573  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
1574  CFTypeRef vals[] = { CFDateCreate(0, t) }; // expected-warning {{leak}}
1575}
1576
1577CFTypeRef global;
1578
1579void rdar6582778_2(void) {
1580  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
1581  global = CFDateCreate(0, t); // no-warning
1582}
1583
1584// <rdar://problem/10232019> - Test that objects passed to containers
1585// are marked "escaped".
1586
1587void rdar10232019() {
1588  NSMutableArray *array = [NSMutableArray array];
1589
1590  NSString *string = [[NSString alloc] initWithUTF8String:"foo"];
1591  [array addObject:string];
1592  [string release];
1593
1594  NSString *otherString = [string stringByAppendingString:@"bar"]; // no-warning
1595  NSLog(@"%@", otherString);
1596}
1597
1598void rdar10232019_positive() {
1599  NSMutableArray *array = [NSMutableArray array];
1600
1601  NSString *string = [[NSString alloc] initWithUTF8String:"foo"];
1602  [string release];
1603
1604  NSString *otherString = [string stringByAppendingString:@"bar"]; // expected-warning {{Reference-counted object is used after it is release}}
1605  NSLog(@"%@", otherString);
1606}
1607
1608// RetainCountChecker support for XPC.
1609// <rdar://problem/9658496>
1610typedef void * xpc_object_t;
1611xpc_object_t _CFXPCCreateXPCObjectFromCFObject(CFTypeRef cf);
1612void xpc_release(xpc_object_t object);
1613
1614void rdar9658496() {
1615  CFStringRef cf;
1616  xpc_object_t xpc;
1617  cf = CFStringCreateWithCString( ((CFAllocatorRef)0), "test", kCFStringEncodingUTF8 ); // no-warning
1618  xpc = _CFXPCCreateXPCObjectFromCFObject( cf );
1619  CFRelease(cf);
1620  xpc_release(xpc);
1621}
1622
1623