Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 1 | //>>SLICER |
Ted Kremenek | e1cea75 | 2009-07-06 21:58:46 +0000 | [diff] [blame] | 2 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -verify %s && |
Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 3 | // RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -verify %s |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 4 | |
Ted Kremenek | 71ef5d6 | 2009-07-17 00:19:33 +0000 | [diff] [blame] | 5 | #if __has_feature(attribute_ns_returns_retained) |
| 6 | #define NS_RETURNS_RETAINED __attribute__((ns_returns_retained)) |
| 7 | #endif |
| 8 | #if __has_feature(attribute_cf_returns_retained) |
| 9 | #define CF_RETURNS_RETAINED __attribute__((cf_returns_retained)) |
| 10 | #endif |
| 11 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 13 | // The following code is reduced using delta-debugging from Mac OS X headers: |
| 14 | // |
| 15 | // #include <Cocoa/Cocoa.h> |
| 16 | // #include <CoreFoundation/CoreFoundation.h> |
| 17 | // #include <DiskArbitration/DiskArbitration.h> |
| 18 | // #include <QuartzCore/QuartzCore.h> |
| 19 | // #include <Quartz/Quartz.h> |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 20 | // #include <IOKit/IOKitLib.h> |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 21 | // |
| 22 | // It includes the basic definitions for the test cases below. |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | typedef unsigned int __darwin_natural_t; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 26 | typedef unsigned long uintptr_t; |
| 27 | typedef unsigned int uint32_t; |
| 28 | typedef unsigned long long uint64_t; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 29 | typedef unsigned int UInt32; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 30 | typedef signed long CFIndex; |
| 31 | typedef const void * CFTypeRef; |
| 32 | typedef const struct __CFString * CFStringRef; |
| 33 | typedef const struct __CFAllocator * CFAllocatorRef; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 34 | extern const CFAllocatorRef kCFAllocatorDefault; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 35 | extern CFTypeRef CFRetain(CFTypeRef cf); |
| 36 | extern void CFRelease(CFTypeRef cf); |
| 37 | typedef struct { |
| 38 | } |
| 39 | CFArrayCallBacks; |
| 40 | extern const CFArrayCallBacks kCFTypeArrayCallBacks; |
| 41 | typedef const struct __CFArray * CFArrayRef; |
| 42 | typedef struct __CFArray * CFMutableArrayRef; |
| 43 | extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); |
| 44 | extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 45 | extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 46 | typedef const struct __CFDictionary * CFDictionaryRef; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 47 | typedef struct __CFDictionary * CFMutableDictionaryRef; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 48 | typedef UInt32 CFStringEncoding; |
| 49 | enum { |
| 50 | 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 }; |
| 51 | extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); |
| 52 | typedef double CFTimeInterval; |
| 53 | typedef CFTimeInterval CFAbsoluteTime; |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 54 | extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 55 | typedef const struct __CFDate * CFDateRef; |
| 56 | extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 57 | extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 58 | typedef __darwin_natural_t natural_t; |
| 59 | typedef natural_t mach_port_name_t; |
| 60 | typedef mach_port_name_t mach_port_t; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 61 | typedef int kern_return_t; |
| 62 | typedef kern_return_t mach_error_t; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 63 | typedef signed char BOOL; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 64 | typedef unsigned long NSUInteger; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 65 | @class NSString, Protocol; |
| 66 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 67 | typedef struct _NSZone NSZone; |
| 68 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 69 | @protocol NSObject - (BOOL)isEqual:(id)object; |
| 70 | - (id)retain; |
| 71 | - (oneway void)release; |
Ted Kremenek | a7ecc37 | 2009-01-28 21:20:48 +0000 | [diff] [blame] | 72 | - (id)autorelease; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 73 | @end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 74 | @end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 75 | @end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 76 | @end @interface NSObject <NSObject> { |
| 77 | } |
Ted Kremenek | 8be2a67 | 2009-03-13 20:27:06 +0000 | [diff] [blame] | 78 | + (id)allocWithZone:(NSZone *)zone; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 79 | + (id)alloc; |
| 80 | - (void)dealloc; |
| 81 | @end extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 82 | typedef struct { |
| 83 | } |
| 84 | NSFastEnumerationState; |
| 85 | @protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 86 | @end @class NSString; |
| 87 | @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 88 | @end @interface NSArray (NSArrayCreation) + (id)array; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 89 | @end @interface NSAutoreleasePool : NSObject { |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 90 | } |
| 91 | - (void)drain; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 92 | @end extern NSString * const NSBundleDidLoadNotification; |
| 93 | typedef double NSTimeInterval; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 94 | @interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 95 | @end typedef unsigned short unichar; |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 96 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 97 | - ( const char *)UTF8String; |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 98 | - (id)initWithUTF8String:(const char *)nullTerminatedCString; |
| 99 | + (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 100 | @end @class NSString, NSData; |
| 101 | @interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 102 | + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length; |
| 103 | + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 104 | @end @class NSString; |
| 105 | @interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 106 | @end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey; |
| 107 | - (void)setObject:(id)anObject forKey:(id)aKey; |
| 108 | @end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 109 | @end typedef double CGFloat; |
| 110 | struct CGSize { |
| 111 | }; |
| 112 | typedef struct CGSize CGSize; |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 113 | struct CGRect { |
| 114 | }; |
| 115 | typedef struct CGRect CGRect; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 116 | @protocol NSLocking - (void)lock; |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 117 | - (id)init; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 118 | @end @class NSURLAuthenticationChallenge; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 119 | typedef mach_port_t io_object_t; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 120 | typedef char io_name_t[128]; |
| 121 | typedef io_object_t io_iterator_t; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 122 | typedef io_object_t io_service_t; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 123 | typedef struct IONotificationPort * IONotificationPortRef; |
| 124 | typedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator ); |
| 125 | io_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching ); |
| 126 | kern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing ); |
| 127 | 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)); |
| 128 | kern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification ); |
| 129 | CFMutableDictionaryRef IOServiceMatching( const char * name ); |
| 130 | CFMutableDictionaryRef IOServiceNameMatching( const char * name ); |
| 131 | CFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName ); |
| 132 | CFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path ); |
| 133 | CFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID ); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 134 | typedef struct __DASession * DASessionRef; |
| 135 | extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); |
| 136 | typedef struct __DADisk * DADiskRef; |
| 137 | extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); |
| 138 | extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); |
| 139 | extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); |
| 140 | extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 141 | typedef struct CGColorSpace *CGColorSpaceRef; |
| 142 | typedef struct CGImage *CGImageRef; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 143 | typedef struct CGLayer *CGLayerRef; |
| 144 | @class NSArray, NSError, NSEvent, NSMenu, NSUndoManager, NSWindow; |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 145 | @interface NSResponder : NSObject <NSCoding> { |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 146 | } |
| 147 | @end @protocol NSAnimatablePropertyContainer - (id)animator; |
| 148 | @end extern NSString *NSAnimationTriggerOrderIn ; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 149 | @interface NSView : NSResponder <NSAnimatablePropertyContainer> { |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 150 | struct __VFlags2 { |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 151 | } |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 152 | _vFlags2; |
| 153 | } |
| 154 | @end extern NSString * const NSFullScreenModeAllScreens; |
| 155 | @protocol NSChangeSpelling - (void)changeSpelling:(id)sender; |
| 156 | @end @protocol NSIgnoreMisspelledWords - (void)ignoreSpelling:(id)sender; |
| 157 | @end @class NSColor, NSFont, NSNotification; |
| 158 | @interface NSText : NSView <NSChangeSpelling, NSIgnoreMisspelledWords> { |
| 159 | } |
| 160 | @end @protocol NSValidatedUserInterfaceItem - (SEL)action; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 161 | @end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 162 | @end @class NSArray, NSError, NSImage, NSView, NSNotificationCenter, NSURL, NSScreen, NSRunningApplication; |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 163 | @interface NSApplication : NSResponder <NSUserInterfaceValidations> { |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 164 | } |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 165 | @end enum { |
| 166 | NSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 }; |
| 167 | typedef NSUInteger NSApplicationTerminateReply; |
| 168 | @protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; |
| 169 | @end enum { |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 170 | } |
| 171 | _CFlags; |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 172 | @interface CIImage : NSObject <NSCoding, NSCopying> { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 173 | } |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 174 | typedef int CIFormat; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 175 | @end enum { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 176 | kDAReturnSuccess = 0, kDAReturnError = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x01, kDAReturnBusy = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x02, kDAReturnBadArgument = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x03, kDAReturnExclusiveAccess = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x04, kDAReturnNoResources = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x05, kDAReturnNotFound = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x06, kDAReturnNotMounted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x07, kDAReturnNotPermitted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x08, kDAReturnNotPrivileged = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x09, kDAReturnNotReady = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0A, kDAReturnNotWritable = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0B, kDAReturnUnsupported = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0C }; |
| 177 | typedef mach_error_t DAReturn; |
| 178 | typedef const struct __DADissenter * DADissenterRef; |
| 179 | extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 180 | @interface CIContext: NSObject { |
| 181 | } |
| 182 | - (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r; |
| 183 | - (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 184 | - (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d; |
| 185 | @end @class NSURL; |
| 186 | @protocol QCCompositionRenderer - (NSDictionary*) attributes; |
| 187 | @end @interface QCRenderer : NSObject <QCCompositionRenderer> { |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 188 | } |
| 189 | - (id) createSnapshotImageOfType:(NSString*)type; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 190 | @end extern NSString* const QCViewDidStartRenderingNotification; |
| 191 | @interface QCView : NSView <QCCompositionRenderer> { |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 192 | } |
| 193 | - (id) createSnapshotImageOfType:(NSString*)type; |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 194 | @end enum { |
| 195 | ICEXIFOrientation1 = 1, ICEXIFOrientation2 = 2, ICEXIFOrientation3 = 3, ICEXIFOrientation4 = 4, ICEXIFOrientation5 = 5, ICEXIFOrientation6 = 6, ICEXIFOrientation7 = 7, ICEXIFOrientation8 = 8, }; |
| 196 | @class ICDevice; |
| 197 | @protocol ICDeviceDelegate <NSObject> @required - (void)didRemoveDevice:(ICDevice*)device; |
| 198 | @end @class ICCameraDevice; |
| 199 | @class ICScannerDevice; |
| 200 | @protocol ICScannerDeviceDelegate <ICDeviceDelegate> @optional - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner; |
Ted Kremenek | a0cc00d | 2009-05-11 17:45:06 +0000 | [diff] [blame] | 201 | @end |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 203 | //===----------------------------------------------------------------------===// |
| 204 | // Test cases. |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | |
| 207 | CFAbsoluteTime f1() { |
| 208 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 209 | CFDateRef date = CFDateCreate(0, t); |
| 210 | CFRetain(date); |
| 211 | CFRelease(date); |
| 212 | CFDateGetAbsoluteTime(date); // no-warning |
| 213 | CFRelease(date); |
| 214 | t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} |
| 215 | return t; |
| 216 | } |
| 217 | |
| 218 | CFAbsoluteTime f2() { |
| 219 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 220 | CFDateRef date = CFDateCreate(0, t); |
| 221 | [((NSDate*) date) retain]; |
| 222 | CFRelease(date); |
| 223 | CFDateGetAbsoluteTime(date); // no-warning |
| 224 | [((NSDate*) date) release]; |
| 225 | t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} |
| 226 | return t; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | NSDate* global_x; |
| 231 | |
| 232 | // Test to see if we supresss an error when we store the pointer |
| 233 | // to a global. |
| 234 | |
| 235 | CFAbsoluteTime f3() { |
| 236 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 237 | CFDateRef date = CFDateCreate(0, t); |
| 238 | [((NSDate*) date) retain]; |
| 239 | CFRelease(date); |
| 240 | CFDateGetAbsoluteTime(date); // no-warning |
| 241 | global_x = (NSDate*) date; |
| 242 | [((NSDate*) date) release]; |
| 243 | t = CFDateGetAbsoluteTime(date); // no-warning |
| 244 | return t; |
| 245 | } |
| 246 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 247 | //--------------------------------------------------------------------------- |
| 248 | // Test case 'f4' differs for region store and basic store. See |
| 249 | // retain-release-region-store.m and retain-release-basic-store.m. |
| 250 | //--------------------------------------------------------------------------- |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 251 | |
| 252 | // Test a leak. |
| 253 | |
| 254 | CFAbsoluteTime f5(int x) { |
| 255 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 256 | CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}} |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 257 | |
| 258 | if (x) |
| 259 | CFRelease(date); |
| 260 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 261 | return t; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // Test a leak involving the return. |
| 265 | |
| 266 | CFDateRef f6(int x) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 267 | CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning{{leak}} |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 268 | CFRetain(date); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 269 | return date; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Test a leak involving an overwrite. |
| 273 | |
| 274 | CFDateRef f7() { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 275 | CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); //expected-warning{{leak}} |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 276 | CFRetain(date); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 277 | date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 278 | return date; |
| 279 | } |
| 280 | |
| 281 | // Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and |
| 282 | // has the word create. |
| 283 | CFDateRef MyDateCreate(); |
| 284 | |
| 285 | CFDateRef f8() { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 286 | CFDateRef date = MyDateCreate(); // expected-warning{{leak}} |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 287 | CFRetain(date); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 288 | return date; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | CFDateRef f9() { |
| 292 | CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); |
| 293 | int *p = 0; |
Ted Kremenek | 25d01ba | 2009-03-09 22:46:49 +0000 | [diff] [blame] | 294 | // When allocations fail, CFDateCreate can return null. |
| 295 | if (!date) *p = 1; // expected-warning{{null}} |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 296 | return date; |
| 297 | } |
| 298 | |
| 299 | // Handle DiskArbitration API: |
| 300 | // |
| 301 | // http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/ |
| 302 | // |
| 303 | void f10(io_service_t media, DADiskRef d, CFStringRef s) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 304 | DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}} |
| 305 | if (disk) NSLog(@"ok"); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 306 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 307 | disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}} |
| 308 | if (disk) NSLog(@"ok"); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 309 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 310 | CFDictionaryRef dict = DADiskCopyDescription(d); // expected-warning{{leak}} |
| 311 | if (dict) NSLog(@"ok"); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 313 | disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}} |
| 314 | if (disk) NSLog(@"ok"); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 316 | DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, // expected-warning{{leak}} |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 317 | kDAReturnSuccess, s); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 318 | if (dissenter) NSLog(@"ok"); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 320 | DASessionRef session = DASessionCreate(kCFAllocatorDefault); // expected-warning{{leak}} |
| 321 | if (session) NSLog(@"ok"); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | // Test retain/release checker with CFString and CFMutableArray. |
| 325 | void f11() { |
| 326 | // Create the array. |
| 327 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); |
| 328 | |
| 329 | // Create a string. |
| 330 | CFStringRef s1 = CFStringCreateWithCString(0, "hello world", |
| 331 | kCFStringEncodingUTF8); |
| 332 | |
| 333 | // Add the string to the array. |
| 334 | CFArrayAppendValue(A, s1); |
| 335 | |
| 336 | // Decrement the reference count. |
| 337 | CFRelease(s1); // no-warning |
| 338 | |
| 339 | // Get the string. We don't own it. |
| 340 | s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0); |
| 341 | |
| 342 | // Release the array. |
| 343 | CFRelease(A); // no-warning |
| 344 | |
| 345 | // Release the string. This is a bug. |
| 346 | CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}} |
| 347 | } |
| 348 | |
Ted Kremenek | 9989065 | 2009-01-16 18:40:33 +0000 | [diff] [blame] | 349 | // PR 3337: Handle functions declared using typedefs. |
| 350 | typedef CFTypeRef CREATEFUN(); |
| 351 | CREATEFUN MyCreateFun; |
| 352 | |
| 353 | void f12() { |
| 354 | CFTypeRef o = MyCreateFun(); // expected-warning {{leak}} |
| 355 | } |
Ted Kremenek | a7ecc37 | 2009-01-28 21:20:48 +0000 | [diff] [blame] | 356 | |
| 357 | void f13_autorelease() { |
Ted Kremenek | 6b62ec9 | 2009-05-09 01:50:57 +0000 | [diff] [blame] | 358 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning |
Ted Kremenek | a7ecc37 | 2009-01-28 21:20:48 +0000 | [diff] [blame] | 359 | [(id) A autorelease]; // no-warning |
| 360 | } |
Ted Kremenek | 79f7f8a | 2009-02-19 18:20:28 +0000 | [diff] [blame] | 361 | |
Ted Kremenek | 6b62ec9 | 2009-05-09 01:50:57 +0000 | [diff] [blame] | 362 | void f13_autorelease_b() { |
| 363 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); |
| 364 | [(id) A autorelease]; |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 365 | [(id) A autorelease]; // expected-warning{{Object sent -autorelease too many times}} |
Ted Kremenek | 6b62ec9 | 2009-05-09 01:50:57 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | CFMutableArrayRef f13_autorelease_c() { |
| 369 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); |
| 370 | [(id) A autorelease]; |
| 371 | [(id) A autorelease]; |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 372 | return A; // expected-warning{{Object sent -autorelease too many times}} |
Ted Kremenek | 6b62ec9 | 2009-05-09 01:50:57 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | CFMutableArrayRef f13_autorelease_d() { |
| 376 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); |
| 377 | [(id) A autorelease]; |
| 378 | [(id) A autorelease]; |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 379 | CFMutableArrayRef B = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{Object sent -autorelease too many times}} |
Ted Kremenek | 6b62ec9 | 2009-05-09 01:50:57 +0000 | [diff] [blame] | 380 | CFRelease(B); // no-warning |
Mike Stump | 08631d1 | 2009-07-21 18:58:15 +0000 | [diff] [blame] | 381 | while (1) {} |
Ted Kremenek | 6b62ec9 | 2009-05-09 01:50:57 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | |
Ted Kremenek | 79f7f8a | 2009-02-19 18:20:28 +0000 | [diff] [blame] | 385 | // This case exercises the logic where the leak site is the same as the allocation site. |
| 386 | void f14_leakimmediately() { |
| 387 | CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} |
| 388 | } |
Ted Kremenek | 9f45d28 | 2009-03-05 18:15:02 +0000 | [diff] [blame] | 389 | |
Ted Kremenek | e82e13a | 2009-04-07 05:33:18 +0000 | [diff] [blame] | 390 | // Test that we track an allocated object beyond the point where the *name* |
| 391 | // of the variable storing the reference is no longer live. |
| 392 | void f15() { |
| 393 | // Create the array. |
| 394 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); |
| 395 | CFMutableArrayRef *B = &A; |
| 396 | // At this point, the name 'A' is no longer live. |
| 397 | CFRelease(*B); // no-warning |
| 398 | } |
| 399 | |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 400 | // Test when we pass NULL to CFRetain/CFRelease. |
| 401 | void f16(int x, CFTypeRef p) { |
| 402 | if (p) |
| 403 | return; |
| 404 | |
| 405 | if (x) { |
| 406 | CFRelease(p); // expected-warning{{Null pointer argument in call to CFRelease}} |
| 407 | } |
| 408 | else { |
| 409 | CFRetain(p); // expected-warning{{Null pointer argument in call to CFRetain}} |
| 410 | } |
| 411 | } |
Ted Kremenek | e82e13a | 2009-04-07 05:33:18 +0000 | [diff] [blame] | 412 | |
Ted Kremenek | 9f45d28 | 2009-03-05 18:15:02 +0000 | [diff] [blame] | 413 | // Test basic tracking of ivars associated with 'self'. For the retain/release |
| 414 | // checker we currently do not want to flag leaks associated with stores |
| 415 | // of tracked objects to ivars. |
| 416 | @interface SelfIvarTest : NSObject { |
| 417 | id myObj; |
| 418 | } |
| 419 | - (void)test_self_tracking; |
| 420 | @end |
| 421 | |
| 422 | @implementation SelfIvarTest |
| 423 | - (void)test_self_tracking { |
| 424 | myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning |
| 425 | } |
| 426 | @end |
| 427 | |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 428 | // Test return of non-owned objects in contexts where an owned object |
| 429 | // is expected. |
| 430 | @interface TestReturnNotOwnedWhenExpectedOwned |
| 431 | - (NSString*)newString; |
| 432 | @end |
| 433 | |
| 434 | @implementation TestReturnNotOwnedWhenExpectedOwned |
| 435 | - (NSString*)newString { |
| 436 | NSString *s = [NSString stringWithUTF8String:"hello"]; |
| 437 | return s; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}} |
| 438 | } |
| 439 | @end |
| 440 | |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 441 | // <rdar://problem/6659160> |
| 442 | int isFoo(char c); |
| 443 | |
| 444 | static void rdar_6659160(char *inkind, char *inname) |
| 445 | { |
| 446 | // We currently expect that [NSObject alloc] cannot fail. This |
| 447 | // will be a toggled flag in the future. It can indeed return null, but |
| 448 | // Cocoa programmers generally aren't expected to reason about out-of-memory |
| 449 | // conditions. |
| 450 | NSString *kind = [[NSString alloc] initWithUTF8String:inkind]; // expected-warning{{leak}} |
| 451 | |
| 452 | // We do allow stringWithUTF8String to fail. This isn't really correct, as |
Ted Kremenek | 68ac94a | 2009-03-23 17:10:25 +0000 | [diff] [blame] | 453 | // far as returning 0. In most error conditions it will throw an exception. |
| 454 | // If allocation fails it could return 0, but again this |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 455 | // isn't expected. |
| 456 | NSString *name = [NSString stringWithUTF8String:inname]; |
| 457 | if(!name) |
| 458 | return; |
| 459 | |
| 460 | const char *kindC = 0; |
| 461 | const char *nameC = 0; |
| 462 | |
| 463 | // In both cases, we cannot reach a point down below where we |
| 464 | // dereference kindC or nameC with either being null. This is because |
| 465 | // we assume that [NSObject alloc] doesn't fail and that we have the guard |
| 466 | // up above. |
| 467 | |
| 468 | if(kind) |
| 469 | kindC = [kind UTF8String]; |
| 470 | if(name) |
| 471 | nameC = [name UTF8String]; |
Ted Kremenek | 25d01ba | 2009-03-09 22:46:49 +0000 | [diff] [blame] | 472 | if(!isFoo(kindC[0])) // expected-warning{{null}} |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 473 | return; |
| 474 | if(!isFoo(nameC[0])) // no-warning |
| 475 | return; |
| 476 | |
| 477 | [kind release]; |
Ted Kremenek | f9df136 | 2009-04-23 21:25:57 +0000 | [diff] [blame] | 478 | [name release]; // expected-warning{{Incorrect decrement of the reference count}} |
Ted Kremenek | cd57fce | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 479 | } |
Ted Kremenek | 9f45d28 | 2009-03-05 18:15:02 +0000 | [diff] [blame] | 480 | |
Ted Kremenek | 8be2a67 | 2009-03-13 20:27:06 +0000 | [diff] [blame] | 481 | // PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming |
| 482 | // conventions with respect to 'return'ing ownership. |
| 483 | @interface PR3677: NSObject @end |
| 484 | @implementation PR3677 |
| 485 | + (id)allocWithZone:(NSZone *)inZone { |
| 486 | return [super allocWithZone:inZone]; // no-warning |
| 487 | } |
| 488 | @end |
| 489 | |
Ted Kremenek | c505d4f | 2009-03-19 19:50:58 +0000 | [diff] [blame] | 490 | // PR 3820 - Reason about calls to -dealloc |
| 491 | void pr3820_DeallocInsteadOfRelease(void) |
| 492 | { |
| 493 | id foo = [[NSString alloc] init]; // no-warning |
| 494 | [foo dealloc]; |
| 495 | // foo is not leaked, since it has been deallocated. |
| 496 | } |
| 497 | |
| 498 | void pr3820_ReleaseAfterDealloc(void) |
| 499 | { |
| 500 | id foo = [[NSString alloc] init]; |
| 501 | [foo dealloc]; |
| 502 | [foo release]; // expected-warning{{used after it is release}} |
| 503 | // NSInternalInconsistencyException: message sent to deallocated object |
| 504 | } |
| 505 | |
| 506 | void pr3820_DeallocAfterRelease(void) |
| 507 | { |
| 508 | NSLog(@"\n\n[%s]", __FUNCTION__); |
| 509 | id foo = [[NSString alloc] init]; |
| 510 | [foo release]; |
| 511 | [foo dealloc]; // expected-warning{{used after it is released}} |
| 512 | // message sent to released object |
| 513 | } |
Ted Kremenek | 68ac94a | 2009-03-23 17:10:25 +0000 | [diff] [blame] | 514 | |
| 515 | // From <rdar://problem/6704930>. The problem here is that 'length' binds to |
| 516 | // '($0 - 1)' after '--length', but SimpleConstraintManager doesn't know how to |
| 517 | // reason about '($0 - 1) > constant'. As a temporary hack, we drop the value |
| 518 | // of '($0 - 1)' and conjure a new symbol. |
| 519 | void rdar6704930(unsigned char *s, unsigned int length) { |
| 520 | NSString* name = 0; |
| 521 | if (s != 0) { |
| 522 | if (length > 0) { |
| 523 | while (length > 0) { |
| 524 | if (*s == ':') { |
| 525 | ++s; |
| 526 | --length; |
| 527 | name = [[NSString alloc] init]; // no-warning |
| 528 | break; |
| 529 | } |
| 530 | ++s; |
| 531 | --length; |
| 532 | } |
| 533 | if ((length == 0) && (name != 0)) { |
| 534 | [name release]; |
| 535 | name = 0; |
| 536 | } |
| 537 | if (length == 0) { // no ':' found -> use it all as name |
| 538 | name = [[NSString alloc] init]; // no-warning |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (name != 0) { |
| 544 | [name release]; |
| 545 | } |
| 546 | } |
| 547 | |
Ted Kremenek | 0fc169e | 2009-04-24 23:09:54 +0000 | [diff] [blame] | 548 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 549 | // <rdar://problem/6833332> |
| 550 | // One build of the analyzer accidentally stopped tracking the allocated |
| 551 | // object after the 'retain'. |
Ted Kremenek | ebd5a2d | 2009-05-11 18:30:24 +0000 | [diff] [blame] | 552 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fde52c9 | 2009-04-29 00:41:31 +0000 | [diff] [blame] | 553 | |
| 554 | @interface rdar_6833332 : NSObject <NSApplicationDelegate> { |
| 555 | NSWindow *window; |
| 556 | } |
| 557 | @property (nonatomic, retain) NSWindow *window; |
| 558 | @end |
| 559 | |
| 560 | @implementation rdar_6833332 |
| 561 | @synthesize window; |
| 562 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { |
| 563 | NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}} |
| 564 | |
| 565 | [dict setObject:@"foo" forKey:@"bar"]; |
| 566 | |
| 567 | NSLog(@"%@", dict); |
| 568 | } |
| 569 | - (void)dealloc { |
| 570 | [window release]; |
| 571 | [super dealloc]; |
| 572 | } |
| 573 | @end |
| 574 | |
Ted Kremenek | 8c6096e | 2009-05-09 03:10:32 +0000 | [diff] [blame] | 575 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a0cc00d | 2009-05-11 17:45:06 +0000 | [diff] [blame] | 576 | // <rdar://problem/6257780> clang checker fails to catch use-after-release |
| 577 | //===----------------------------------------------------------------------===// |
| 578 | |
Ted Kremenek | ebd5a2d | 2009-05-11 18:30:24 +0000 | [diff] [blame] | 579 | int rdar_6257780_Case1() { |
Ted Kremenek | a0cc00d | 2009-05-11 17:45:06 +0000 | [diff] [blame] | 580 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 581 | NSArray *array = [NSArray array]; |
| 582 | [array release]; // expected-warning{{Incorrect decrement of the reference count of an object is not owned at this point by the caller}} |
| 583 | [pool drain]; |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ebd5a2d | 2009-05-11 18:30:24 +0000 | [diff] [blame] | 588 | // <rdar://problem/6866843> Checker should understand new/setObject:/release constructs |
| 589 | //===----------------------------------------------------------------------===// |
| 590 | |
| 591 | void rdar_6866843() { |
| 592 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 593 | NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init]; |
| 594 | NSArray* array = [[NSArray alloc] init]; |
| 595 | [dictionary setObject:array forKey:@"key"]; |
| 596 | [array release]; |
| 597 | // Using 'array' here should be fine |
| 598 | NSLog(@"array = %@\n", array); // no-warning |
| 599 | // Now the array is released |
| 600 | [dictionary release]; |
| 601 | [pool drain]; |
| 602 | } |
| 603 | |
Ted Kremenek | 6738b73 | 2009-05-12 04:53:03 +0000 | [diff] [blame] | 604 | |
| 605 | //===----------------------------------------------------------------------===// |
| 606 | // <rdar://problem/6877235> Classes typedef-ed to CF objects should get the same treatment as CF objects |
| 607 | //===----------------------------------------------------------------------===// |
| 608 | |
| 609 | typedef CFTypeRef OtherRef; |
| 610 | |
| 611 | @interface RDar6877235 : NSObject {} |
| 612 | - (CFTypeRef)_copyCFTypeRef; |
| 613 | - (OtherRef)_copyOtherRef; |
| 614 | @end |
| 615 | |
| 616 | @implementation RDar6877235 |
| 617 | - (CFTypeRef)_copyCFTypeRef { |
| 618 | return [[NSString alloc] init]; // no-warning |
| 619 | } |
| 620 | - (OtherRef)_copyOtherRef { |
| 621 | return [[NSString alloc] init]; // no-warning |
| 622 | } |
| 623 | @end |
| 624 | |
Ted Kremenek | ebd5a2d | 2009-05-11 18:30:24 +0000 | [diff] [blame] | 625 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 626 | //<rdar://problem/6320065> false positive - init method returns an object owned by caller |
| 627 | //===----------------------------------------------------------------------===// |
| 628 | |
| 629 | @interface RDar6320065 : NSObject { |
| 630 | NSString *_foo; |
| 631 | } |
| 632 | - (id)initReturningNewClass; |
| 633 | - (id)initReturningNewClassBad; |
| 634 | - (id)initReturningNewClassBad2; |
| 635 | @end |
| 636 | |
| 637 | @interface RDar6320065Subclass : RDar6320065 |
| 638 | @end |
| 639 | |
| 640 | @implementation RDar6320065 |
| 641 | - (id)initReturningNewClass { |
| 642 | [self release]; |
| 643 | self = [[RDar6320065Subclass alloc] init]; // no-warning |
| 644 | return self; |
| 645 | } |
| 646 | - (id)initReturningNewClassBad { |
| 647 | [self release]; |
| 648 | [[RDar6320065Subclass alloc] init]; // expected-warning {{leak}} |
| 649 | return self; |
| 650 | } |
| 651 | - (id)initReturningNewClassBad2 { |
| 652 | [self release]; |
| 653 | self = [[RDar6320065Subclass alloc] init]; |
| 654 | return [self autorelease]; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}} |
| 655 | } |
| 656 | |
| 657 | @end |
| 658 | |
| 659 | @implementation RDar6320065Subclass |
| 660 | @end |
| 661 | |
| 662 | int RDar6320065_test() { |
| 663 | RDar6320065 *test = [[RDar6320065 alloc] init]; // no-warning |
| 664 | [test release]; |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3043766 | 2009-05-14 21:29:16 +0000 | [diff] [blame] | 669 | // <rdar://problem/6859457> [NSData dataWithBytesNoCopy] does not return a retained object |
| 670 | //===----------------------------------------------------------------------===// |
| 671 | |
| 672 | @interface RDar6859457 : NSObject {} |
| 673 | - (NSString*) NoCopyString; |
| 674 | - (NSString*) noCopyString; |
| 675 | @end |
| 676 | |
| 677 | @implementation RDar6859457 |
| 678 | - (NSString*) NoCopyString { return [[NSString alloc] init]; } // no-warning |
| 679 | - (NSString*) noCopyString { return [[NSString alloc] init]; } // no-warning |
| 680 | @end |
| 681 | |
| 682 | void test_RDar6859457(RDar6859457 *x, void *bytes, NSUInteger dataLength) { |
| 683 | [x NoCopyString]; // expected-warning{{leak}} |
| 684 | [x noCopyString]; // expected-warning{{leak}} |
| 685 | [NSData dataWithBytesNoCopy:bytes length:dataLength]; // no-warning |
| 686 | [NSData dataWithBytesNoCopy:bytes length:dataLength freeWhenDone:1]; // no-warning |
| 687 | } |
| 688 | |
| 689 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ba67f6a | 2009-05-18 23:14:34 +0000 | [diff] [blame] | 690 | // PR 4230 - an autorelease pool is not necessarily leaked during a premature |
| 691 | // return |
| 692 | //===----------------------------------------------------------------------===// |
| 693 | |
| 694 | static void PR4230(void) |
| 695 | { |
| 696 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // no-warning |
| 697 | NSString *object = [[[NSString alloc] init] autorelease]; // no-warning |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7db1604 | 2009-05-15 15:49:00 +0000 | [diff] [blame] | 702 | // Method name that has a null IdentifierInfo* for its first selector slot. |
| 703 | // This test just makes sure that we handle it. |
| 704 | //===----------------------------------------------------------------------===// |
| 705 | |
| 706 | @interface TestNullIdentifier |
| 707 | @end |
| 708 | |
| 709 | @implementation TestNullIdentifier |
| 710 | + (id):(int)x, ... { |
| 711 | return [[NSString alloc] init]; // expected-warning{{leak}} |
| 712 | } |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 713 | @end |
Ted Kremenek | 7db1604 | 2009-05-15 15:49:00 +0000 | [diff] [blame] | 714 | |
| 715 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fae664a | 2009-05-16 01:38:01 +0000 | [diff] [blame] | 716 | // <rdar://problem/6893565> don't flag leaks for return types that cannot be |
| 717 | // determined to be CF types |
| 718 | //===----------------------------------------------------------------------===// |
| 719 | |
| 720 | // We don't know if 'struct s6893565' represents a Core Foundation type, so |
| 721 | // we shouldn't emit an error here. |
| 722 | typedef struct s6893565* TD6893565; |
| 723 | |
| 724 | @interface RDar6893565 {} |
| 725 | -(TD6893565)newThing; |
| 726 | @end |
| 727 | |
| 728 | @implementation RDar6893565 |
| 729 | -(TD6893565)newThing { |
| 730 | return (TD6893565) [[NSString alloc] init]; // no-warning |
| 731 | } |
| 732 | @end |
| 733 | |
| 734 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 735 | // <rdar://problem/6902710> clang: false positives w/QC and CoreImage methods |
| 736 | //===----------------------------------------------------------------------===// |
| 737 | |
| 738 | void rdar6902710(QCView *view, QCRenderer *renderer, CIContext *context, |
| 739 | NSString *str, CIImage *img, CGRect rect, |
| 740 | CIFormat form, CGColorSpaceRef cs) { |
| 741 | [view createSnapshotImageOfType:str]; // expected-warning{{leak}} |
| 742 | [renderer createSnapshotImageOfType:str]; // expected-warning{{leak}} |
| 743 | [context createCGImage:img fromRect:rect]; // expected-warning{{leak}} |
| 744 | [context createCGImage:img fromRect:rect format:form colorSpace:cs]; // expected-warning{{leak}} |
| 745 | } |
| 746 | |
| 747 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 748 | // <rdar://problem/6945561> -[CIContext createCGLayerWithSize:info:] |
| 749 | // misinterpreted by clang scan-build |
| 750 | //===----------------------------------------------------------------------===// |
| 751 | |
| 752 | void rdar6945561(CIContext *context, CGSize size, CFDictionaryRef d) { |
| 753 | [context createCGLayerWithSize:size info:d]; // expected-warning{{leak}} |
| 754 | } |
| 755 | |
| 756 | //===----------------------------------------------------------------------===// |
| 757 | // <rdar://problem/6961230> add knowledge of IOKit functions to retain/release |
| 758 | // checker |
| 759 | //===----------------------------------------------------------------------===// |
| 760 | |
| 761 | void IOBSDNameMatching_wrapper(mach_port_t masterPort, uint32_t options, const char * bsdName) { |
| 762 | IOBSDNameMatching(masterPort, options, bsdName); // expected-warning{{leak}} |
| 763 | } |
| 764 | |
| 765 | void IOServiceMatching_wrapper(const char * name) { |
| 766 | IOServiceMatching(name); // expected-warning{{leak}} |
| 767 | } |
| 768 | |
| 769 | void IOServiceNameMatching_wrapper(const char * name) { |
| 770 | IOServiceNameMatching(name); // expected-warning{{leak}} |
| 771 | } |
| 772 | |
Ted Kremenek | 71ef5d6 | 2009-07-17 00:19:33 +0000 | [diff] [blame] | 773 | CF_RETURNS_RETAINED CFDictionaryRef CreateDict(); |
Ted Kremenek | fdd8b8e | 2009-06-16 20:44:39 +0000 | [diff] [blame] | 774 | |
| 775 | void IOServiceAddNotification_wrapper(mach_port_t masterPort, const io_name_t notificationType, |
| 776 | mach_port_t wakePort, uintptr_t reference, io_iterator_t * notification ) { |
| 777 | |
| 778 | CFDictionaryRef matching = CreateDict(); |
| 779 | CFRelease(matching); |
| 780 | IOServiceAddNotification(masterPort, notificationType, matching, // expected-warning{{used after it is released}} expected-warning{{deprecated}} |
| 781 | wakePort, reference, notification); |
| 782 | } |
| 783 | |
| 784 | void IORegistryEntryIDMatching_wrapper(uint64_t entryID ) { |
| 785 | IORegistryEntryIDMatching(entryID); // expected-warning{{leak}} |
| 786 | } |
| 787 | |
| 788 | void IOOpenFirmwarePathMatching_wrapper(mach_port_t masterPort, uint32_t options, |
| 789 | const char * path) { |
| 790 | IOOpenFirmwarePathMatching(masterPort, options, path); // expected-warning{{leak}} |
| 791 | } |
| 792 | |
| 793 | void IOServiceGetMatchingService_wrapper(mach_port_t masterPort) { |
| 794 | CFDictionaryRef matching = CreateDict(); |
| 795 | IOServiceGetMatchingService(masterPort, matching); |
| 796 | CFRelease(matching); // expected-warning{{used after it is released}} |
| 797 | } |
| 798 | |
| 799 | void IOServiceGetMatchingServices_wrapper(mach_port_t masterPort, io_iterator_t *existing) { |
| 800 | CFDictionaryRef matching = CreateDict(); |
| 801 | IOServiceGetMatchingServices(masterPort, matching, existing); |
| 802 | CFRelease(matching); // expected-warning{{used after it is released}} |
| 803 | } |
| 804 | |
| 805 | void IOServiceAddMatchingNotification_wrapper(IONotificationPortRef notifyPort, const io_name_t notificationType, |
| 806 | IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification) { |
| 807 | |
| 808 | CFDictionaryRef matching = CreateDict(); |
| 809 | IOServiceAddMatchingNotification(notifyPort, notificationType, matching, callback, refCon, notification); |
| 810 | CFRelease(matching); // expected-warning{{used after it is released}} |
| 811 | } |
| 812 | |
| 813 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 8c6096e | 2009-05-09 03:10:32 +0000 | [diff] [blame] | 814 | // Tests of ownership attributes. |
| 815 | //===----------------------------------------------------------------------===// |
| 816 | |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 817 | typedef NSString* MyStringTy; |
| 818 | |
Ted Kremenek | 8b31826 | 2009-07-21 21:21:04 +0000 | [diff] [blame] | 819 | @protocol FooP; |
| 820 | |
Ted Kremenek | 8c6096e | 2009-05-09 03:10:32 +0000 | [diff] [blame] | 821 | @interface TestOwnershipAttr : NSObject |
Ted Kremenek | 71ef5d6 | 2009-07-17 00:19:33 +0000 | [diff] [blame] | 822 | - (NSString*) returnsAnOwnedString NS_RETURNS_RETAINED; // no-warning |
| 823 | - (NSString*) returnsAnOwnedCFString CF_RETURNS_RETAINED; // no-warning |
| 824 | - (MyStringTy) returnsAnOwnedTypedString NS_RETURNS_RETAINED; // no-warning |
| 825 | - (int) returnsAnOwnedInt NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to functions or methods that return a pointer or Objective-C object}} |
Ted Kremenek | 8b31826 | 2009-07-21 21:21:04 +0000 | [diff] [blame] | 826 | - (id<FooP>) returnsOwnedProt NS_RETURNS_RETAINED; // no-warning |
Ted Kremenek | 8c6096e | 2009-05-09 03:10:32 +0000 | [diff] [blame] | 827 | @end |
| 828 | |
Ted Kremenek | 71ef5d6 | 2009-07-17 00:19:33 +0000 | [diff] [blame] | 829 | static int ownership_attribute_doesnt_go_here NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to function or method types}} |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 830 | |
Ted Kremenek | 8c6096e | 2009-05-09 03:10:32 +0000 | [diff] [blame] | 831 | void test_attr_1(TestOwnershipAttr *X) { |
| 832 | NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}} |
| 833 | } |
| 834 | |
| 835 | void test_attr_1b(TestOwnershipAttr *X) { |
| 836 | NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}} |
| 837 | } |
Ted Kremenek | b9d8db8 | 2009-06-05 23:00:33 +0000 | [diff] [blame] | 838 | |
| 839 | @interface MyClassTestCFAttr : NSObject {} |
Ted Kremenek | 71ef5d6 | 2009-07-17 00:19:33 +0000 | [diff] [blame] | 840 | - (NSDate*) returnsCFRetained CF_RETURNS_RETAINED; |
| 841 | - (CFDateRef) returnsCFRetainedAsCF CF_RETURNS_RETAINED; |
Ted Kremenek | b9d8db8 | 2009-06-05 23:00:33 +0000 | [diff] [blame] | 842 | - (NSDate*) alsoReturnsRetained; |
Ted Kremenek | 6d4b76d | 2009-07-06 18:30:43 +0000 | [diff] [blame] | 843 | - (CFDateRef) alsoReturnsRetainedAsCF; |
Ted Kremenek | 71ef5d6 | 2009-07-17 00:19:33 +0000 | [diff] [blame] | 844 | - (NSDate*) returnsNSRetained NS_RETURNS_RETAINED; |
Ted Kremenek | b9d8db8 | 2009-06-05 23:00:33 +0000 | [diff] [blame] | 845 | @end |
| 846 | |
Ted Kremenek | 71ef5d6 | 2009-07-17 00:19:33 +0000 | [diff] [blame] | 847 | CF_RETURNS_RETAINED |
Ted Kremenek | b9d8db8 | 2009-06-05 23:00:33 +0000 | [diff] [blame] | 848 | CFDateRef returnsRetainedCFDate() { |
| 849 | return CFDateCreate(0, CFAbsoluteTimeGetCurrent()); |
| 850 | } |
| 851 | |
| 852 | @implementation MyClassTestCFAttr |
| 853 | - (NSDate*) returnsCFRetained { |
| 854 | return (NSDate*) returnsRetainedCFDate(); // No leak. |
| 855 | } |
| 856 | |
Ted Kremenek | 6d4b76d | 2009-07-06 18:30:43 +0000 | [diff] [blame] | 857 | - (CFDateRef) returnsCFRetainedAsCF { |
| 858 | return returnsRetainedCFDate(); // No leak. |
| 859 | } |
| 860 | |
| 861 | |
Ted Kremenek | b9d8db8 | 2009-06-05 23:00:33 +0000 | [diff] [blame] | 862 | - (NSDate*) alsoReturnsRetained { |
| 863 | return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}} |
| 864 | } |
| 865 | |
Ted Kremenek | 6d4b76d | 2009-07-06 18:30:43 +0000 | [diff] [blame] | 866 | - (CFDateRef) alsoReturnsRetainedAsCF { |
| 867 | return returnsRetainedCFDate(); // expected-warning{{leak}} |
| 868 | } |
| 869 | |
| 870 | |
Ted Kremenek | b9d8db8 | 2009-06-05 23:00:33 +0000 | [diff] [blame] | 871 | - (NSDate*) returnsNSRetained { |
| 872 | return (NSDate*) returnsRetainedCFDate(); // no-warning |
| 873 | } |
| 874 | @end |
| 875 | |
| 876 | |
Ted Kremenek | 8c6096e | 2009-05-09 03:10:32 +0000 | [diff] [blame] | 877 | |