Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 1 | // RUN: clang -analyze -checker-cfref -verify %s |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 2 | |
| 3 | //===----------------------------------------------------------------------===// |
| 4 | // The following code is reduced using delta-debugging from |
| 5 | // Foundation.h (Mac OS X). |
| 6 | // |
| 7 | // It includes the basic definitions for the test cases below. |
| 8 | // Not including Foundation.h directly makes this test case both svelte and |
| 9 | // portable to non-Mac platforms. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | typedef unsigned int __darwin_natural_t; |
| 13 | typedef unsigned long UInt32; |
| 14 | typedef signed long CFIndex; |
| 15 | typedef const void * CFTypeRef; |
| 16 | typedef const struct __CFString * CFStringRef; |
| 17 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 18 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 19 | extern CFTypeRef CFRetain(CFTypeRef cf); |
| 20 | extern void CFRelease(CFTypeRef cf); |
| 21 | typedef struct { |
| 22 | } |
| 23 | CFArrayCallBacks; |
| 24 | extern const CFArrayCallBacks kCFTypeArrayCallBacks; |
| 25 | typedef const struct __CFArray * CFArrayRef; |
| 26 | typedef struct __CFArray * CFMutableArrayRef; |
| 27 | extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); |
| 28 | extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); |
| 29 | typedef const struct __CFDictionary * CFDictionaryRef; |
| 30 | typedef UInt32 CFStringEncoding; |
| 31 | enum { |
| 32 | 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 }; |
| 33 | extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); |
| 34 | typedef double CFTimeInterval; |
| 35 | typedef CFTimeInterval CFAbsoluteTime; |
| 36 | typedef const struct __CFDate * CFDateRef; |
| 37 | extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); |
| 38 | extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); |
| 39 | typedef __darwin_natural_t natural_t; |
| 40 | typedef natural_t mach_port_name_t; |
| 41 | typedef mach_port_name_t mach_port_t; |
| 42 | typedef signed char BOOL; |
| 43 | typedef struct _NSZone NSZone; |
| 44 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 45 | @protocol NSObject - (BOOL)isEqual:(id)object; |
| 46 | - (id)retain; |
| 47 | - (oneway void)release; |
| 48 | @end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; |
| 49 | @end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 50 | @end @interface NSObject <NSObject> { |
| 51 | } |
| 52 | @end typedef float CGFloat; |
| 53 | typedef double NSTimeInterval; |
| 54 | @interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; |
| 55 | @end enum { |
| 56 | NSObjCNoType = 0, NSObjCVoidType = 'v', NSObjCCharType = 'c', NSObjCShortType = 's', NSObjCLongType = 'l', NSObjCLonglongType = 'q', NSObjCFloatType = 'f', NSObjCDoubleType = 'd', NSObjCBoolType = 'B', NSObjCSelectorType = ':', NSObjCObjectType = '@', NSObjCStructType = '{', NSObjCPointerType = '^', NSObjCStringType = '*', NSObjCArrayType = '[', NSObjCUnionType = '(', NSObjCBitfield = 'b' } |
| 57 | __attribute__((deprecated)); |
| 58 | typedef int kern_return_t; |
| 59 | typedef kern_return_t mach_error_t; |
| 60 | typedef mach_port_t io_object_t; |
| 61 | typedef io_object_t io_service_t; |
| 62 | typedef struct __DASession * DASessionRef; |
| 63 | extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); |
| 64 | typedef struct __DADisk * DADiskRef; |
| 65 | extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); |
| 66 | extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); |
| 67 | extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); |
| 68 | extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); |
| 69 | @interface NSAppleEventManager : NSObject { |
| 70 | } |
| 71 | @end enum { |
| 72 | 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 }; |
| 73 | typedef mach_error_t DAReturn; |
| 74 | typedef const struct __DADissenter * DADissenterRef; |
| 75 | extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); |
| 76 | |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | // Test cases. |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
| 81 | CFAbsoluteTime f1() { |
| 82 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 83 | CFDateRef date = CFDateCreate(0, t); |
| 84 | CFRetain(date); |
| 85 | CFRelease(date); |
| 86 | CFDateGetAbsoluteTime(date); // no-warning |
| 87 | CFRelease(date); |
| 88 | t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} |
| 89 | return t; |
| 90 | } |
| 91 | |
| 92 | CFAbsoluteTime f2() { |
| 93 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 94 | CFDateRef date = CFDateCreate(0, t); |
| 95 | [((NSDate*) date) retain]; |
| 96 | CFRelease(date); |
| 97 | CFDateGetAbsoluteTime(date); // no-warning |
| 98 | [((NSDate*) date) release]; |
| 99 | t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} |
| 100 | return t; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | NSDate* global_x; |
| 105 | |
| 106 | // Test to see if we supresss an error when we store the pointer |
| 107 | // to a global. |
| 108 | |
| 109 | CFAbsoluteTime f3() { |
| 110 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 111 | CFDateRef date = CFDateCreate(0, t); |
| 112 | [((NSDate*) date) retain]; |
| 113 | CFRelease(date); |
| 114 | CFDateGetAbsoluteTime(date); // no-warning |
| 115 | global_x = (NSDate*) date; |
| 116 | [((NSDate*) date) release]; |
| 117 | t = CFDateGetAbsoluteTime(date); // no-warning |
| 118 | return t; |
| 119 | } |
| 120 | |
| 121 | // Test to see if we supresss an error when we store the pointer |
| 122 | // to a struct. |
| 123 | |
| 124 | struct foo { |
| 125 | NSDate* f; |
| 126 | }; |
| 127 | |
| 128 | CFAbsoluteTime f4() { |
| 129 | struct foo x; |
| 130 | |
| 131 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 132 | CFDateRef date = CFDateCreate(0, t); |
| 133 | [((NSDate*) date) retain]; |
| 134 | CFRelease(date); |
| 135 | CFDateGetAbsoluteTime(date); // no-warning |
| 136 | x.f = (NSDate*) date; |
| 137 | [((NSDate*) date) release]; |
| 138 | t = CFDateGetAbsoluteTime(date); // no-warning |
| 139 | return t; |
| 140 | } |
| 141 | |
| 142 | // Test a leak. |
| 143 | |
| 144 | CFAbsoluteTime f5(int x) { |
| 145 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 146 | CFDateRef date = CFDateCreate(0, t); |
| 147 | |
| 148 | if (x) |
| 149 | CFRelease(date); |
| 150 | |
| 151 | return t; // expected-warning{{leak}} |
| 152 | } |
| 153 | |
| 154 | // Test a leak involving the return. |
| 155 | |
| 156 | CFDateRef f6(int x) { |
| 157 | CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); |
| 158 | CFRetain(date); |
| 159 | return date; // expected-warning{{leak}} |
| 160 | } |
| 161 | |
| 162 | // Test a leak involving an overwrite. |
| 163 | |
| 164 | CFDateRef f7() { |
| 165 | CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); |
| 166 | CFRetain(date); //expected-warning{{leak}} |
| 167 | date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); |
| 168 | return date; |
| 169 | } |
| 170 | |
| 171 | // Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and |
| 172 | // has the word create. |
| 173 | CFDateRef MyDateCreate(); |
| 174 | |
| 175 | CFDateRef f8() { |
| 176 | CFDateRef date = MyDateCreate(); |
| 177 | CFRetain(date); |
| 178 | return date; // expected-warning{{leak}} |
| 179 | } |
| 180 | |
| 181 | CFDateRef f9() { |
| 182 | CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); |
| 183 | int *p = 0; |
| 184 | // test that the checker assumes that CFDateCreate returns a non-null |
| 185 | // pointer |
| 186 | if (!date) *p = 1; // no-warning |
| 187 | return date; |
| 188 | } |
| 189 | |
| 190 | // Handle DiskArbitration API: |
| 191 | // |
| 192 | // http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/ |
| 193 | // |
| 194 | void f10(io_service_t media, DADiskRef d, CFStringRef s) { |
| 195 | DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); |
| 196 | if (disk) NSLog(@"ok"); // expected-warning{{leak}} |
| 197 | |
| 198 | disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); |
| 199 | if (disk) NSLog(@"ok"); // expected-warning{{leak}} |
| 200 | |
| 201 | CFDictionaryRef dict = DADiskCopyDescription(d); |
| 202 | if (dict) NSLog(@"ok"); // expected-warning{{leak}} |
| 203 | |
| 204 | disk = DADiskCopyWholeDisk(d); |
| 205 | if (disk) NSLog(@"ok"); // expected-warning{{leak}} |
| 206 | |
| 207 | DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, |
| 208 | kDAReturnSuccess, s); |
| 209 | if (dissenter) NSLog(@"ok"); // expected-warning{{leak}} |
| 210 | |
| 211 | DASessionRef session = DASessionCreate(kCFAllocatorDefault); |
| 212 | if (session) NSLog(@"ok"); // expected-warning{{leak}} |
| 213 | } |
| 214 | |
| 215 | // Test retain/release checker with CFString and CFMutableArray. |
| 216 | void f11() { |
| 217 | // Create the array. |
| 218 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); |
| 219 | |
| 220 | // Create a string. |
| 221 | CFStringRef s1 = CFStringCreateWithCString(0, "hello world", |
| 222 | kCFStringEncodingUTF8); |
| 223 | |
| 224 | // Add the string to the array. |
| 225 | CFArrayAppendValue(A, s1); |
| 226 | |
| 227 | // Decrement the reference count. |
| 228 | CFRelease(s1); // no-warning |
| 229 | |
| 230 | // Get the string. We don't own it. |
| 231 | s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0); |
| 232 | |
| 233 | // Release the array. |
| 234 | CFRelease(A); // no-warning |
| 235 | |
| 236 | // Release the string. This is a bug. |
| 237 | CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}} |
| 238 | } |
| 239 | |
Ted Kremenek | 9989065 | 2009-01-16 18:40:33 +0000 | [diff] [blame] | 240 | // PR 3337: Handle functions declared using typedefs. |
| 241 | typedef CFTypeRef CREATEFUN(); |
| 242 | CREATEFUN MyCreateFun; |
| 243 | |
| 244 | void f12() { |
| 245 | CFTypeRef o = MyCreateFun(); // expected-warning {{leak}} |
| 246 | } |