| Ted Kremenek | fe32cc0 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1 | // RUN: clang -analyze -checker-cfref -verify %s && |
| Ted Kremenek | b535181 | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 2 | // RUN: clang -analyze -checker-cfref -analyzer-store=region -verify %s |
| Ted Kremenek | fe32cc0 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 3 | |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 4 | |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | // The following code is reduced using delta-debugging from |
| 7 | // Foundation.h (Mac OS X). |
| 8 | // |
| 9 | // It includes the basic definitions for the test cases below. |
| 10 | // Not including Foundation.h directly makes this test case both svelte and |
| 11 | // portable to non-Mac platforms. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | typedef unsigned int __darwin_natural_t; |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 15 | typedef struct {} div_t; |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 16 | typedef unsigned long UInt32; |
| 17 | typedef signed long CFIndex; |
| 18 | typedef const void * CFTypeRef; |
| 19 | typedef const struct __CFString * CFStringRef; |
| 20 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 21 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 22 | extern CFTypeRef CFRetain(CFTypeRef cf); |
| 23 | extern void CFRelease(CFTypeRef cf); |
| 24 | typedef struct { |
| 25 | } |
| 26 | CFArrayCallBacks; |
| 27 | extern const CFArrayCallBacks kCFTypeArrayCallBacks; |
| 28 | typedef const struct __CFArray * CFArrayRef; |
| 29 | typedef struct __CFArray * CFMutableArrayRef; |
| 30 | extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); |
| 31 | extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 32 | extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 33 | typedef const struct __CFDictionary * CFDictionaryRef; |
| 34 | typedef UInt32 CFStringEncoding; |
| 35 | enum { |
| 36 | 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 }; |
| 37 | extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); |
| 38 | typedef double CFTimeInterval; |
| 39 | typedef CFTimeInterval CFAbsoluteTime; |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 40 | extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 41 | typedef const struct __CFDate * CFDateRef; |
| 42 | extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); |
| 43 | extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); |
| 44 | typedef __darwin_natural_t natural_t; |
| 45 | typedef natural_t mach_port_name_t; |
| 46 | typedef mach_port_name_t mach_port_t; |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 47 | typedef struct { |
| 48 | } |
| 49 | CFRunLoopObserverContext; |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 50 | typedef signed char BOOL; |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 51 | typedef unsigned int NSUInteger; |
| 52 | @class NSString, Protocol; |
| 53 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 54 | typedef struct _NSZone NSZone; |
| 55 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 56 | @protocol NSObject - (BOOL)isEqual:(id)object; |
| 57 | - (id)retain; |
| 58 | - (oneway void)release; |
| Ted Kremenek | f675864 | 2009-01-28 21:20:48 +0000 | [diff] [blame] | 59 | - (id)autorelease; |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 60 | @end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 61 | @end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 62 | @end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; |
| Ted Kremenek | 340fd2d | 2009-03-13 20:27:06 +0000 | [diff] [blame^] | 63 | @end |
| 64 | @interface NSObject <NSObject> {} |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 65 | + (id)alloc; |
| Ted Kremenek | 340fd2d | 2009-03-13 20:27:06 +0000 | [diff] [blame^] | 66 | + (id)allocWithZone:(NSZone *)zone; |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 67 | @end typedef float CGFloat; |
| 68 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
| 69 | - (const char *)UTF8String; |
| 70 | - (id)initWithUTF8String:(const char *)nullTerminatedCString; |
| 71 | + (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
| 72 | @end extern NSString * const NSCurrentLocaleDidChangeNotification ; |
| 73 | @protocol NSLocking - (void)lock; |
| 74 | @end extern NSString * const NSUndoManagerCheckpointNotification; |
| 75 | typedef enum { |
| 76 | ACL_READ_DATA = (1<<1), ACL_LIST_DIRECTORY = (1<<1), ACL_WRITE_DATA = (1<<2), ACL_ADD_FILE = (1<<2), ACL_EXECUTE = (1<<3), ACL_SEARCH = (1<<3), ACL_DELETE = (1<<4), ACL_APPEND_DATA = (1<<5), ACL_ADD_SUBDIRECTORY = (1<<5), ACL_DELETE_CHILD = (1<<6), ACL_READ_ATTRIBUTES = (1<<7), ACL_WRITE_ATTRIBUTES = (1<<8), ACL_READ_EXTATTRIBUTES = (1<<9), ACL_WRITE_EXTATTRIBUTES = (1<<10), ACL_READ_SECURITY = (1<<11), ACL_WRITE_SECURITY = (1<<12), ACL_CHANGE_OWNER = (1<<13) } |
| 77 | acl_entry_id_t; |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 78 | typedef int kern_return_t; |
| 79 | typedef kern_return_t mach_error_t; |
| 80 | typedef mach_port_t io_object_t; |
| 81 | typedef io_object_t io_service_t; |
| 82 | typedef struct __DASession * DASessionRef; |
| 83 | extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); |
| 84 | typedef struct __DADisk * DADiskRef; |
| 85 | extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); |
| 86 | extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); |
| 87 | extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); |
| 88 | extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 89 | @interface NSResponder : NSObject <NSCoding> { |
| 90 | } |
| 91 | @end @class NSColor, NSFont, NSNotification; |
| 92 | typedef struct __CFlags { |
| 93 | } |
| 94 | _CFlags; |
| 95 | @interface NSCell : NSObject <NSCopying, NSCoding> { |
| 96 | } |
| 97 | @end @class NSDate, NSDictionary, NSError, NSException, NSNotification; |
| 98 | @interface NSManagedObjectContext : NSObject <NSCoding, NSLocking> { |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 99 | } |
| 100 | @end enum { |
| 101 | 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 }; |
| 102 | typedef mach_error_t DAReturn; |
| 103 | typedef const struct __DADissenter * DADissenterRef; |
| 104 | extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); |
| 105 | |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 109 | //===----------------------------------------------------------------------===// |
| 110 | // Test cases. |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | |
| 113 | CFAbsoluteTime f1() { |
| 114 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 115 | CFDateRef date = CFDateCreate(0, t); |
| 116 | CFRetain(date); |
| 117 | CFRelease(date); |
| 118 | CFDateGetAbsoluteTime(date); // no-warning |
| 119 | CFRelease(date); |
| 120 | t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} |
| 121 | return t; |
| 122 | } |
| 123 | |
| 124 | CFAbsoluteTime f2() { |
| 125 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 126 | CFDateRef date = CFDateCreate(0, t); |
| 127 | [((NSDate*) date) retain]; |
| 128 | CFRelease(date); |
| 129 | CFDateGetAbsoluteTime(date); // no-warning |
| 130 | [((NSDate*) date) release]; |
| 131 | t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} |
| 132 | return t; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | NSDate* global_x; |
| 137 | |
| 138 | // Test to see if we supresss an error when we store the pointer |
| 139 | // to a global. |
| 140 | |
| 141 | CFAbsoluteTime f3() { |
| 142 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 143 | CFDateRef date = CFDateCreate(0, t); |
| 144 | [((NSDate*) date) retain]; |
| 145 | CFRelease(date); |
| 146 | CFDateGetAbsoluteTime(date); // no-warning |
| 147 | global_x = (NSDate*) date; |
| 148 | [((NSDate*) date) release]; |
| 149 | t = CFDateGetAbsoluteTime(date); // no-warning |
| 150 | return t; |
| 151 | } |
| 152 | |
| Ted Kremenek | fe32cc0 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 153 | //--------------------------------------------------------------------------- |
| 154 | // Test case 'f4' differs for region store and basic store. See |
| 155 | // retain-release-region-store.m and retain-release-basic-store.m. |
| 156 | //--------------------------------------------------------------------------- |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 157 | |
| 158 | // Test a leak. |
| 159 | |
| 160 | CFAbsoluteTime f5(int x) { |
| 161 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 162 | CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}} |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 163 | |
| 164 | if (x) |
| 165 | CFRelease(date); |
| 166 | |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 167 | return t; |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Test a leak involving the return. |
| 171 | |
| 172 | CFDateRef f6(int x) { |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 173 | CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning{{leak}} |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 174 | CFRetain(date); |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 175 | return date; |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // Test a leak involving an overwrite. |
| 179 | |
| 180 | CFDateRef f7() { |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 181 | CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); //expected-warning{{leak}} |
| Ted Kremenek | f08ac27 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 182 | CFRetain(date); |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 183 | date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 184 | return date; |
| 185 | } |
| 186 | |
| 187 | // Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and |
| 188 | // has the word create. |
| 189 | CFDateRef MyDateCreate(); |
| 190 | |
| 191 | CFDateRef f8() { |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 192 | CFDateRef date = MyDateCreate(); // expected-warning{{leak}} |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 193 | CFRetain(date); |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 194 | return date; |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | CFDateRef f9() { |
| 198 | CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); |
| 199 | int *p = 0; |
| Ted Kremenek | 0b891a3 | 2009-03-09 22:46:49 +0000 | [diff] [blame] | 200 | // When allocations fail, CFDateCreate can return null. |
| 201 | if (!date) *p = 1; // expected-warning{{null}} |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 202 | return date; |
| 203 | } |
| 204 | |
| 205 | // Handle DiskArbitration API: |
| 206 | // |
| 207 | // http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/ |
| 208 | // |
| 209 | void f10(io_service_t media, DADiskRef d, CFStringRef s) { |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 210 | DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}} |
| 211 | if (disk) NSLog(@"ok"); |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 212 | |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 213 | disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}} |
| 214 | if (disk) NSLog(@"ok"); |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 215 | |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 216 | CFDictionaryRef dict = DADiskCopyDescription(d); // expected-warning{{leak}} |
| 217 | if (dict) NSLog(@"ok"); |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 218 | |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 219 | disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}} |
| 220 | if (disk) NSLog(@"ok"); |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 221 | |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 222 | DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, // expected-warning{{leak}} |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 223 | kDAReturnSuccess, s); |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 224 | if (dissenter) NSLog(@"ok"); |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 225 | |
| Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 226 | DASessionRef session = DASessionCreate(kCFAllocatorDefault); // expected-warning{{leak}} |
| 227 | if (session) NSLog(@"ok"); |
| Ted Kremenek | 7e90422 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Test retain/release checker with CFString and CFMutableArray. |
| 231 | void f11() { |
| 232 | // Create the array. |
| 233 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); |
| 234 | |
| 235 | // Create a string. |
| 236 | CFStringRef s1 = CFStringCreateWithCString(0, "hello world", |
| 237 | kCFStringEncodingUTF8); |
| 238 | |
| 239 | // Add the string to the array. |
| 240 | CFArrayAppendValue(A, s1); |
| 241 | |
| 242 | // Decrement the reference count. |
| 243 | CFRelease(s1); // no-warning |
| 244 | |
| 245 | // Get the string. We don't own it. |
| 246 | s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0); |
| 247 | |
| 248 | // Release the array. |
| 249 | CFRelease(A); // no-warning |
| 250 | |
| 251 | // Release the string. This is a bug. |
| 252 | CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}} |
| 253 | } |
| 254 | |
| Ted Kremenek | 86afde3 | 2009-01-16 18:40:33 +0000 | [diff] [blame] | 255 | // PR 3337: Handle functions declared using typedefs. |
| 256 | typedef CFTypeRef CREATEFUN(); |
| 257 | CREATEFUN MyCreateFun; |
| 258 | |
| 259 | void f12() { |
| 260 | CFTypeRef o = MyCreateFun(); // expected-warning {{leak}} |
| 261 | } |
| Ted Kremenek | f675864 | 2009-01-28 21:20:48 +0000 | [diff] [blame] | 262 | |
| 263 | void f13_autorelease() { |
| 264 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); |
| 265 | [(id) A autorelease]; // no-warning |
| 266 | } |
| Ted Kremenek | bea465ae | 2009-02-19 18:20:28 +0000 | [diff] [blame] | 267 | |
| 268 | // This case exercises the logic where the leak site is the same as the allocation site. |
| 269 | void f14_leakimmediately() { |
| 270 | CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} |
| 271 | } |
| Ted Kremenek | a7ec605 | 2009-03-05 18:15:02 +0000 | [diff] [blame] | 272 | |
| 273 | // Test basic tracking of ivars associated with 'self'. For the retain/release |
| 274 | // checker we currently do not want to flag leaks associated with stores |
| 275 | // of tracked objects to ivars. |
| 276 | @interface SelfIvarTest : NSObject { |
| 277 | id myObj; |
| 278 | } |
| 279 | - (void)test_self_tracking; |
| 280 | @end |
| 281 | |
| 282 | @implementation SelfIvarTest |
| 283 | - (void)test_self_tracking { |
| 284 | myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning |
| 285 | } |
| 286 | @end |
| 287 | |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 288 | // <rdar://problem/6659160> |
| 289 | int isFoo(char c); |
| 290 | |
| 291 | static void rdar_6659160(char *inkind, char *inname) |
| 292 | { |
| 293 | // We currently expect that [NSObject alloc] cannot fail. This |
| 294 | // will be a toggled flag in the future. It can indeed return null, but |
| 295 | // Cocoa programmers generally aren't expected to reason about out-of-memory |
| 296 | // conditions. |
| 297 | NSString *kind = [[NSString alloc] initWithUTF8String:inkind]; // expected-warning{{leak}} |
| 298 | |
| 299 | // We do allow stringWithUTF8String to fail. This isn't really correct, as |
| 300 | // far as returning nil. In most error conditions it will throw an exception. |
| 301 | // If allocation fails it could return nil, but again this |
| 302 | // isn't expected. |
| 303 | NSString *name = [NSString stringWithUTF8String:inname]; |
| 304 | if(!name) |
| 305 | return; |
| 306 | |
| 307 | const char *kindC = 0; |
| 308 | const char *nameC = 0; |
| 309 | |
| 310 | // In both cases, we cannot reach a point down below where we |
| 311 | // dereference kindC or nameC with either being null. This is because |
| 312 | // we assume that [NSObject alloc] doesn't fail and that we have the guard |
| 313 | // up above. |
| 314 | |
| 315 | if(kind) |
| 316 | kindC = [kind UTF8String]; |
| 317 | if(name) |
| 318 | nameC = [name UTF8String]; |
| Ted Kremenek | 0b891a3 | 2009-03-09 22:46:49 +0000 | [diff] [blame] | 319 | if(!isFoo(kindC[0])) // expected-warning{{null}} |
| Ted Kremenek | 3987bbe | 2009-03-09 22:28:18 +0000 | [diff] [blame] | 320 | return; |
| 321 | if(!isFoo(nameC[0])) // no-warning |
| 322 | return; |
| 323 | |
| 324 | [kind release]; |
| 325 | [name release]; |
| 326 | } |
| Ted Kremenek | a7ec605 | 2009-03-05 18:15:02 +0000 | [diff] [blame] | 327 | |
| Ted Kremenek | 340fd2d | 2009-03-13 20:27:06 +0000 | [diff] [blame^] | 328 | // PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming |
| 329 | // conventions with respect to 'return'ing ownership. |
| 330 | @interface PR3677: NSObject @end |
| 331 | @implementation PR3677 |
| 332 | + (id)allocWithZone:(NSZone *)inZone { |
| 333 | return [super allocWithZone:inZone]; // no-warning |
| 334 | } |
| 335 | @end |
| 336 | |