| 1 | // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease,osx.cocoa.RetainCount -analyzer-store=region -fblocks -verify %s |
| 2 | |
| 3 | #if __has_feature(attribute_ns_returns_retained) |
| 4 | #define NS_RETURNS_RETAINED __attribute__((ns_returns_retained)) |
| 5 | #endif |
| 6 | #if __has_feature(attribute_cf_returns_retained) |
| 7 | #define CF_RETURNS_RETAINED __attribute__((cf_returns_retained)) |
| 8 | #endif |
| 9 | #if __has_feature(attribute_ns_returns_not_retained) |
| 10 | #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained)) |
| 11 | #endif |
| 12 | #if __has_feature(attribute_cf_returns_not_retained) |
| 13 | #define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained)) |
| 14 | #endif |
| 15 | #if __has_feature(attribute_ns_consumes_self) |
| 16 | #define NS_CONSUMES_SELF __attribute__((ns_consumes_self)) |
| 17 | #endif |
| 18 | #if __has_feature(attribute_ns_consumed) |
| 19 | #define NS_CONSUMED __attribute__((ns_consumed)) |
| 20 | #endif |
| 21 | #if __has_feature(attribute_cf_consumed) |
| 22 | #define CF_CONSUMED __attribute__((cf_consumed)) |
| 23 | #endif |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // The following code is reduced using delta-debugging from Mac OS X headers: |
| 27 | // |
| 28 | // #include <Cocoa/Cocoa.h> |
| 29 | // #include <CoreFoundation/CoreFoundation.h> |
| 30 | // #include <DiskArbitration/DiskArbitration.h> |
| 31 | // #include <QuartzCore/QuartzCore.h> |
| 32 | // #include <Quartz/Quartz.h> |
| 33 | // #include <IOKit/IOKitLib.h> |
| 34 | // |
| 35 | // It includes the basic definitions for the test cases below. |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
| 38 | typedef unsigned int __darwin_natural_t; |
| 39 | typedef unsigned long uintptr_t; |
| 40 | typedef unsigned int uint32_t; |
| 41 | typedef unsigned long long uint64_t; |
| 42 | typedef unsigned int UInt32; |
| 43 | typedef signed long CFIndex; |
| 44 | typedef struct { |
| 45 | CFIndex location; |
| 46 | CFIndex length; |
| 47 | } CFRange; |
| 48 | static __inline__ __attribute__((always_inline)) CFRange CFRangeMake(CFIndex loc, CFIndex len) { |
| 49 | CFRange range; |
| 50 | range.location = loc; |
| 51 | range.length = len; |
| 52 | return range; |
| 53 | } |
| 54 | typedef const void * CFTypeRef; |
| 55 | typedef const struct __CFString * CFStringRef; |
| 56 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 57 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 58 | extern CFTypeRef CFRetain(CFTypeRef cf); |
| 59 | extern void CFRelease(CFTypeRef cf); |
| 60 | typedef struct { |
| 61 | } |
| 62 | CFArrayCallBacks; |
| 63 | extern const CFArrayCallBacks kCFTypeArrayCallBacks; |
| 64 | typedef const struct __CFArray * CFArrayRef; |
| 65 | typedef struct __CFArray * CFMutableArrayRef; |
| 66 | extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); |
| 67 | void abort(void) __attribute__((noreturn)); |
| 68 | CFArrayRef CFArrayCreate(CFAllocatorRef allocator, const void **values, CFIndex numValues, const CFArrayCallBacks *callBacks); |
| 69 | extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); |
| 70 | extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); |
| 71 | typedef struct { |
| 72 | } |
| 73 | CFDictionaryKeyCallBacks; |
| 74 | extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks; |
| 75 | typedef struct { |
| 76 | } |
| 77 | CFDictionaryValueCallBacks; |
| 78 | extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks; |
| 79 | typedef const struct __CFDictionary * CFDictionaryRef; |
| 80 | typedef struct __CFDictionary * CFMutableDictionaryRef; |
| 81 | extern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks); |
| 82 | typedef UInt32 CFStringEncoding; |
| 83 | enum { |
| 84 | kCFStringEncodingMacRoman = 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 }; |
| 85 | extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); |
| 86 | extern CFStringRef CFStringCreateCopy(CFAllocatorRef alloc, CFStringRef theString); |
| 87 | typedef double CFTimeInterval; |
| 88 | typedef CFTimeInterval CFAbsoluteTime; |
| 89 | extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); |
| 90 | typedef const struct __CFDate * CFDateRef; |
| 91 | extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); |
| 92 | extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); |
| 93 | typedef __darwin_natural_t natural_t; |
| 94 | typedef natural_t mach_port_name_t; |
| 95 | typedef mach_port_name_t mach_port_t; |
| 96 | typedef int kern_return_t; |
| 97 | typedef kern_return_t mach_error_t; |
| 98 | enum { |
| 99 | kCFNumberSInt8Type = 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 }; |
| 100 | typedef CFIndex CFNumberType; |
| 101 | typedef const struct __CFNumber * CFNumberRef; |
| 102 | extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr); |
| 103 | typedef const struct __CFAttributedString *CFAttributedStringRef; |
| 104 | typedef struct __CFAttributedString *CFMutableAttributedStringRef; |
| 105 | extern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ; |
| 106 | extern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ; |
| 107 | extern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ; |
| 108 | typedef signed char BOOL; |
| 109 | typedef unsigned long NSUInteger; |
| 110 | @class NSString, Protocol; |
| 111 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 112 | typedef struct _NSZone NSZone; |
| 113 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 114 | @protocol NSObject |
| 115 | - (BOOL)isEqual:(id)object; |
| 116 | - (id)retain; |
| 117 | - (id)copy; |
| 118 | - (oneway void)release; |
| 119 | - (id)autorelease; |
| 120 | @end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; |
| 121 | @end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; |
| 122 | @end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 123 | @end |
| 124 | @interface NSObject <NSObject> {} |
| 125 | + (id)allocWithZone:(NSZone *)zone; |
| 126 | + (id)alloc; |
| 127 | - (void)dealloc; |
| 128 | - (id)init; |
| 129 | @end |
| 130 | @interface NSObject (NSCoderMethods) |
| 131 | - (id)awakeAfterUsingCoder:(NSCoder *)aDecoder; |
| 132 | @end |
| 133 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 134 | typedef struct { |
| 135 | } |
| 136 | NSFastEnumerationState; |
| 137 | @protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; |
| 138 | @end @class NSString, NSDictionary; |
| 139 | @interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value; |
| 140 | @end @interface NSNumber : NSValue - (char)charValue; |
| 141 | - (id)initWithInt:(int)value; |
| 142 | @end @class NSString; |
| 143 | @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; |
| 144 | @end @interface NSArray (NSArrayCreation) + (id)array; |
| 145 | @end @interface NSAutoreleasePool : NSObject { |
| 146 | } |
| 147 | - (void)drain; |
| 148 | @end extern NSString * const NSBundleDidLoadNotification; |
| 149 | typedef double NSTimeInterval; |
| 150 | @interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; |
| 151 | @end typedef unsigned short unichar; |
| 152 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
| 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; |
| 166 | struct CGSize { |
| 167 | }; |
| 168 | typedef struct CGSize CGSize; |
| 169 | struct CGRect { |
| 170 | }; |
| 171 | typedef struct CGRect CGRect; |
| 172 | typedef mach_port_t io_object_t; |
| 173 | typedef char io_name_t[128]; |
| 174 | typedef io_object_t io_iterator_t; |
| 175 | typedef io_object_t io_service_t; |
| 176 | typedef struct IONotificationPort * IONotificationPortRef; |
| 177 | typedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator ); |
| 178 | io_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching ); |
| 179 | kern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing ); |
| 180 | kern_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)); |
| 181 | kern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification ); |
| 182 | CFMutableDictionaryRef IOServiceMatching( const char * name ); |
| 183 | CFMutableDictionaryRef IOServiceNameMatching( const char * name ); |
| 184 | CFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName ); |
| 185 | CFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path ); |
| 186 | CFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID ); |
| 187 | typedef struct __DASession * DASessionRef; |
| 188 | extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); |
| 189 | typedef struct __DADisk * DADiskRef; |
| 190 | extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); |
| 191 | extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); |
| 192 | extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); |
| 193 | extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); |
| 194 | @interface NSTask : NSObject - (id)init; |
| 195 | @end typedef struct CGColorSpace *CGColorSpaceRef; |
| 196 | typedef struct CGImage *CGImageRef; |
| 197 | typedef 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 { |
| 210 | NSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 }; |
| 211 | typedef 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; |
| 217 | typedef struct { |
| 218 | } |
| 219 | CVTimeStamp; |
| 220 | @interface CIImage : NSObject <NSCoding, NSCopying> { |
| 221 | } |
| 222 | typedef int CIFormat; |
| 223 | @end enum { |
| 224 | kDAReturnSuccess = 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 }; |
| 225 | typedef mach_error_t DAReturn; |
| 226 | typedef const struct __DADissenter * DADissenterRef; |
| 227 | extern 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 { |
| 243 | ICEXIFOrientation1 = 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 | |
| 251 | typedef long unsigned int __darwin_size_t; |
| 252 | typedef __darwin_size_t size_t; |
| 253 | typedef unsigned long CFTypeID; |
| 254 | struct CGPoint { |
| 255 | CGFloat x; |
| 256 | CGFloat y; |
| 257 | }; |
| 258 | typedef struct CGPoint CGPoint; |
| 259 | typedef struct CGGradient *CGGradientRef; |
| 260 | typedef uint32_t CGGradientDrawingOptions; |
| 261 | extern CFTypeID CGGradientGetTypeID(void); |
| 262 | extern CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef |
| 263 | space, const CGFloat components[], const CGFloat locations[], size_t count); |
| 264 | extern CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space, |
| 265 | CFArrayRef colors, const CGFloat locations[]); |
| 266 | extern CGGradientRef CGGradientRetain(CGGradientRef gradient); |
| 267 | extern void CGGradientRelease(CGGradientRef gradient); |
| 268 | typedef struct CGContext *CGContextRef; |
| 269 | extern void CGContextDrawLinearGradient(CGContextRef context, |
| 270 | CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint, |
| 271 | CGGradientDrawingOptions options); |
| 272 | extern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void); |
| 273 | //===----------------------------------------------------------------------===// |
| 274 | // Test cases. |
| 275 | //===----------------------------------------------------------------------===// |
| 276 | |
| 277 | class SmartPointer { |
| 278 | id x; |
| 279 | public: |
| 280 | SmartPointer(id x) : x(x) {} |
| 281 | ~SmartPointer() { [x release]; } |
| 282 | |
| 283 | void adopt(id x); |
| 284 | void noAdopt(id x); |
| 285 | }; |
| 286 | |
| 287 | void test_positive() { |
| 288 | id x = [[NSObject alloc] init]; // expected-warning {{leak}} |
| 289 | } |
| 290 | |
| 291 | void test_smartpointer_1() { |
| 292 | id x = [[NSObject alloc] init]; // no-warning |
| 293 | SmartPointer foo(x); |
| 294 | } |
| 295 | |
| 296 | void test_smartpointer_2() { |
| 297 | id x = [[NSObject alloc] init]; // no-warning |
| 298 | SmartPointer foo(0); |
| 299 | foo.adopt(x); |
| 300 | } |
| 301 | |
| 302 | // FIXME: Eventually we want annotations to say whether or not |
| 303 | // a C++ method claims ownership of an Objective-C object. |
| 304 | void test_smartpointer_3() { |
| 305 | id x = [[NSObject alloc] init]; // no-warning |
| 306 | SmartPointer foo(0); |
| 307 | foo.noAdopt(x); |
| 308 | } |
| 309 | |
| 310 | void test_smartpointer_4() { |
| 311 | id x = [[NSObject alloc] init]; // no-warning |
| 312 | SmartPointer *foo = new SmartPointer(x); |
| 313 | delete foo; |
| 314 | } |
| 315 | |
| 316 | extern CFStringRef ElectronMicroscopyEngage(void); |
| 317 | void test_microscopy() { |
| 318 | NSString *token = (NSString*) ElectronMicroscopyEngage(); |
| 319 | [token release]; // expected-warning {{object that is not owned}} |
| 320 | } |
| 321 | |
| 322 | extern CFStringRef Scopy(void); |
| 323 | void test_Scopy() { |
| 324 | NSString *token = (NSString*) Scopy(); |
| 325 | [token release]; // expected-warning {{object that is not owned}} |
| 326 | } |
| 327 | |
| 328 | //===----------------------------------------------------------------------===// |
| 329 | // Test handling of template functions used to do magic with |
| 330 | // tracked retained pointers. |
| 331 | //===----------------------------------------------------------------------===// |
| 332 | |
| 333 | template <typename T, typename U> T static_objc_cast(U* value) |
| 334 | { |
| 335 | // ...debugging code omitted... |
| 336 | return static_cast<T>(value); |
| 337 | } |
| 338 | |
| 339 | int rdar10553686(void) |
| 340 | { |
| 341 | NSObject* bar = static_objc_cast<NSObject*>([[NSObject alloc] init]); |
| 342 | [bar release]; |
| 343 | return 0; |
| 344 | } |
| 345 | int rdar10553686_positive(void) |
| 346 | { |
| 347 | NSObject* bar = static_objc_cast<NSObject*>([[NSObject alloc] init]); |
| 348 | [bar release]; |
| 349 | [bar retain]; // expected-warning {{used after it is released}} |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | @interface NSMapTable : NSObject <NSCopying, NSCoding, NSFastEnumeration> |
| 354 | @end |
| 355 | extern void *NSMapGet(NSMapTable *table, const void *key); |
| 356 | extern void NSMapInsert(NSMapTable *table, const void *key, const void *value); |
| 357 | extern void NSMapInsertKnownAbsent(NSMapTable *table, const void *key, const void *value); |
| 358 | char *strdup(const char *s); |
| 359 | |
| 360 | NSString * radar11152419(NSString *string1, NSString *key1, NSMapTable *map) { |
| 361 | NSString *string = ( NSString *)NSMapGet(map, key1); |
| 362 | if (!string) { |
| 363 | string = [string1 copy]; |
| 364 | NSString *key = [key1 copy]; |
| 365 | NSMapInsert(map, (void*) key, (void*)string); // no warning |
| 366 | NSMapInsertKnownAbsent(map, (void*)key, (void*)string); // no warning |
| 367 | } |
| 368 | return string; |
| 369 | } |
| 370 | |
| 371 | //===----------------------------------------------------------------------===// |
| 372 | // Don't crash on non-member functions with "callbacks" but without names. |
| 373 | //===----------------------------------------------------------------------===// |
| 374 | |
| 375 | struct IntWrapper { |
| 376 | int arg; |
| 377 | }; |
| 378 | |
| 379 | int operator>> (const IntWrapper &W, int (*f)(int)) { |
| 380 | return f(W.arg); |
| 381 | } |
| 382 | |
| 383 | void testCallback() { |
| 384 | IntWrapper val = { 42 }; |
| 385 | |
| 386 | extern int process(int); |
| 387 | val >> process; |
| 388 | } |
| 389 | |
| 390 | //===----------------------------------------------------------------------===// |
| 391 | // Test handling static initializers. |
| 392 | //===----------------------------------------------------------------------===// |
| 393 | |
| 394 | @interface radar13227740 : NSObject |
| 395 | @end |
| 396 | |
| 397 | @implementation radar13227740 |
| 398 | - (CFArrayRef)test { |
| 399 | static CFArrayRef array = ::CFArrayCreate(0, 0, 0, 0); |
| 400 | do { if (!((0 != array)/1)) { abort(); } } while (false); |
| 401 | return array; |
| 402 | } |
| 403 | |
| 404 | // Previously this reported a bogus leak. |
| 405 | - (void)test2 { |
| 406 | (void)[self test]; |
| 407 | (void)[self test]; |
| 408 | } |
| 409 | @end |
| 410 | |
| 411 | //===----------------------------------------------------------------------===// |
| 412 | // Don't crash on getting a null expression from CallEnter corresponding to a |
| 413 | // destructor. |
| 414 | //===----------------------------------------------------------------------===// |
| 415 | |
| 416 | template <typename X> |
| 417 | class Holder { |
| 418 | public: |
| 419 | Holder() throw(); |
| 420 | ~Holder() throw() {} |
| 421 | X* get() const throw(); |
| 422 | void reset(X* p) throw(); |
| 423 | private: |
| 424 | X* ptr_; |
| 425 | }; |
| 426 | |
| 427 | template<typename X> |
| 428 | inline |
| 429 | Holder<X>::Holder() throw() |
| 430 | : ptr_(0){} |
| 431 | |
| 432 | template <typename X> |
| 433 | inline |
| 434 | X* Holder<X>::get() const throw() { |
| 435 | return ptr_; |
| 436 | } |
| 437 | |
| 438 | template <typename X> |
| 439 | inline |
| 440 | void Holder<X>::reset(X* p) throw() { |
| 441 | if (ptr_ != p) { |
| 442 | if (ptr_ != 0) { |
| 443 | ::CFRelease( ptr_ ); |
| 444 | } |
| 445 | ptr_ = p; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | class radar13722286 { |
| 450 | public: |
| 451 | radar13722286() {} |
| 452 | private: |
| 453 | void PrepareBitmap(); |
| 454 | Holder<const struct __CFString> mStr; |
| 455 | }; |
| 456 | |
| 457 | void radar13722286::PrepareBitmap() { |
| 458 | if (mStr.get() != 0) { |
| 459 | Holder<const struct __CFString> str1; |
| 460 | mStr.reset( CFStringCreateCopy( 0, str1.get() ) ); //expected-warning {{Potential leak of an object}} |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // rdar://34210609 |
| 465 | void _() { _(); }; // no-warning |
| 466 | |
| 467 | // Do not assume that IOBSDNameMatching increments a reference counter, |
| 468 | // unless return type is CFMutableDictionaryRef. |
| 469 | void* IOBSDNameMatching(); |
| 470 | void rdar33832412() { |
| 471 | void* x = IOBSDNameMatching(); // no-warning |
| 472 | } |
| 473 | |
| 474 | namespace member_CFRetains { |
| 475 | class Foo { |
| 476 | public: |
| 477 | void CFRetain(const Foo &) {} |
| 478 | void CFRetain(int) {} |
| 479 | }; |
| 480 | |
| 481 | void bar() { |
| 482 | Foo foo; |
| 483 | foo.CFRetain(foo); // no-warning |
| 484 | foo.CFRetain(0); // no-warning |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | namespace cxx_method_escaping { |
| 489 | |
| 490 | struct S { |
| 491 | static CFArrayRef testGetNoTracking(); |
| 492 | CFArrayRef testGetNoTrackingMember(); |
| 493 | }; |
| 494 | |
| 495 | void test_cxx_static_method_escaping() { |
| 496 | CFArrayRef arr = S::testGetNoTracking(); |
| 497 | CFRelease(arr); |
| 498 | } |
| 499 | |
| 500 | void test_cxx_method_escaping(S *s) { |
| 501 | CFArrayRef arr = s->testGetNoTrackingMember(); |
| 502 | CFRelease(arr); |
| 503 | } |
| 504 | |
| 505 | } |
| 506 | |
| 507 | namespace yet_another_unexpected_signature_crash { |
| 508 | |
| 509 | CFTypeRef CFSomethingSomethingRetain(); |
| 510 | CFTypeRef CFSomethingSomethingAutorelease(); |
| 511 | |
| 512 | void foo() { |
| 513 | CFSomethingSomethingRetain(); // no-crash |
| 514 | CFSomethingSomethingAutorelease(); // no-crash |
| 515 | } |
| 516 | |
| 517 | } |
| 518 | |