Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-store=region -verify %s |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +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; |
Ted Kremenek | 47dcd06 | 2009-10-15 23:44:02 +0000 | [diff] [blame] | 45 | @protocol NSObject |
| 46 | - (BOOL)isEqual:(id)object; |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 47 | - (id)retain; |
| 48 | - (oneway void)release; |
| 49 | @end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; |
| 50 | @end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; |
Ted Kremenek | 47dcd06 | 2009-10-15 23:44:02 +0000 | [diff] [blame] | 51 | @end |
| 52 | @interface NSObject <NSObject> {} |
| 53 | + (id)allocWithZone:(NSZone *)zone; |
| 54 | + (id)alloc; |
| 55 | - (void)dealloc; |
| 56 | @end |
| 57 | typedef float CGFloat; |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 58 | typedef double NSTimeInterval; |
| 59 | @interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; |
| 60 | @end enum { |
| 61 | 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' } |
| 62 | __attribute__((deprecated)); |
| 63 | typedef int kern_return_t; |
| 64 | typedef kern_return_t mach_error_t; |
| 65 | typedef mach_port_t io_object_t; |
| 66 | typedef io_object_t io_service_t; |
| 67 | typedef struct __DASession * DASessionRef; |
| 68 | extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); |
| 69 | typedef struct __DADisk * DADiskRef; |
| 70 | extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); |
| 71 | extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); |
| 72 | extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); |
| 73 | extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); |
| 74 | @interface NSAppleEventManager : NSObject { |
| 75 | } |
| 76 | @end enum { |
| 77 | 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 }; |
| 78 | typedef mach_error_t DAReturn; |
| 79 | typedef const struct __DADissenter * DADissenterRef; |
| 80 | extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); |
Ted Kremenek | 47dcd06 | 2009-10-15 23:44:02 +0000 | [diff] [blame] | 81 | @interface NSNumber : NSObject |
Ted Kremenek | 473e167 | 2009-10-16 00:30:49 +0000 | [diff] [blame] | 82 | - (id)initWithInt:(int)value; |
| 83 | @end |
| 84 | typedef unsigned long NSUInteger; |
| 85 | @interface NSArray : NSObject |
| 86 | -(id) initWithObjects:(const id *)objects count:(NSUInteger) cnt; |
| 87 | @end |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 88 | |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | // Test cases. |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | |
| 93 | // Test to see if we *issue* an error when we store the pointer |
| 94 | // to a struct. This differs from basic store. |
| 95 | |
Chris Lattner | e030358 | 2010-01-09 20:43:19 +0000 | [diff] [blame] | 96 | CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); |
| 97 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 98 | struct foo { |
| 99 | NSDate* f; |
| 100 | }; |
| 101 | |
| 102 | CFAbsoluteTime f4() { |
| 103 | struct foo x; |
| 104 | |
| 105 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 106 | CFDateRef date = CFDateCreate(0, t); |
| 107 | [((NSDate*) date) retain]; |
| 108 | CFRelease(date); |
| 109 | CFDateGetAbsoluteTime(date); // no-warning |
| 110 | x.f = (NSDate*) date; |
| 111 | [((NSDate*) date) release]; |
| 112 | t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} |
| 113 | return t; |
| 114 | } |
| 115 | |
Ted Kremenek | f27893a | 2009-03-05 05:14:55 +0000 | [diff] [blame] | 116 | // Test that assigning to an self.ivar loses track of an object. |
| 117 | // This is a temporary hack to reduce false positives. |
| 118 | @interface Test3 : NSObject { |
| 119 | id myObj; |
| 120 | } |
| 121 | - (void)test_self_assign_ivar; |
| 122 | @end |
| 123 | |
| 124 | @implementation Test3 |
| 125 | - (void)test_self_assign_ivar { |
| 126 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 127 | CFDateRef date = CFDateCreate(0, t); // no-warning |
| 128 | myObj = (id) date; |
| 129 | } |
| 130 | @end |
| 131 | |
Ted Kremenek | 473e167 | 2009-10-16 00:30:49 +0000 | [diff] [blame] | 132 | //===------------------------------------------------------------------------------------------===// |
| 133 | // <rdar://problem/7257223> (also <rdar://problem/7283470>) - False positive due to not invalidating |
| 134 | // the reference count of a tracked region that was itself invalidated. |
| 135 | //===------------------------------------------------------------------------------------------===// |
Ted Kremenek | 159c53d | 2009-10-14 01:54:16 +0000 | [diff] [blame] | 136 | |
| 137 | typedef struct __rdar_7257223 { CFDateRef x; } RDar7257223; |
| 138 | void rdar_7257223_aux(RDar7257223 *p); |
| 139 | |
Ted Kremenek | 159c53d | 2009-10-14 01:54:16 +0000 | [diff] [blame] | 140 | CFDateRef rdar7257223_Create(void) { |
| 141 | RDar7257223 s; |
| 142 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 143 | s.x = CFDateCreate(0, t); // no-warning |
| 144 | rdar_7257223_aux(&s); |
| 145 | return s.x; |
| 146 | } |
| 147 | |
| 148 | CFDateRef rdar7257223_Create_2(void) { |
| 149 | RDar7257223 s; |
| 150 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 151 | s.x = CFDateCreate(0, t); // no-warning |
| 152 | return s.x; |
| 153 | } |
Ted Kremenek | f27893a | 2009-03-05 05:14:55 +0000 | [diff] [blame] | 154 | |
Ted Kremenek | 47dcd06 | 2009-10-15 23:44:02 +0000 | [diff] [blame] | 155 | void rdar7283470(void) { |
| 156 | NSNumber *numbers[] = { |
| 157 | [[NSNumber alloc] initWithInt:1], // no-warning |
| 158 | [[NSNumber alloc] initWithInt:2], // no-warning |
| 159 | [[NSNumber alloc] initWithInt:3], // no-warning |
| 160 | [[NSNumber alloc] initWithInt:4], // no-warning |
| 161 | [[NSNumber alloc] initWithInt:5] // no-warning |
| 162 | }; |
| 163 | |
| 164 | for (unsigned i = 0 ; i < sizeof(numbers) / sizeof(numbers[0]) ; ++i) |
| 165 | [numbers[i] release]; |
| 166 | } |
| 167 | |
| 168 | void rdar7283470_positive(void) { |
| 169 | NSNumber *numbers[] = { |
| 170 | [[NSNumber alloc] initWithInt:1], // expected-warning{{leak}} |
| 171 | [[NSNumber alloc] initWithInt:2], // expected-warning{{leak}} |
| 172 | [[NSNumber alloc] initWithInt:3], // expected-warning{{leak}} |
| 173 | [[NSNumber alloc] initWithInt:4], // expected-warning{{leak}} |
| 174 | [[NSNumber alloc] initWithInt:5] // expected-warning{{leak}} |
| 175 | }; |
| 176 | } |
| 177 | |
Ted Kremenek | 473e167 | 2009-10-16 00:30:49 +0000 | [diff] [blame] | 178 | void rdar7283470_2(void) { |
| 179 | NSNumber *numbers[] = { |
| 180 | [[NSNumber alloc] initWithInt:1], // no-warning |
| 181 | [[NSNumber alloc] initWithInt:2], // no-warning |
| 182 | [[NSNumber alloc] initWithInt:3], // no-warning |
| 183 | [[NSNumber alloc] initWithInt:4], // no-warning |
| 184 | [[NSNumber alloc] initWithInt:5] // no-warning |
| 185 | }; |
| 186 | |
| 187 | NSArray *s_numbers =[[NSArray alloc] initWithObjects:&numbers[0] count:sizeof(numbers) / sizeof(numbers[0])]; |
| 188 | |
| 189 | for (unsigned i = 0 ; i < sizeof(numbers) / sizeof(numbers[0]) ; ++i) |
| 190 | [numbers[i] release]; |
| 191 | |
| 192 | [s_numbers release]; |
| 193 | } |
| 194 | |
| 195 | void rdar7283470_2_positive(void) { |
| 196 | NSNumber *numbers[] = { |
| 197 | [[NSNumber alloc] initWithInt:1], // no-warning |
| 198 | [[NSNumber alloc] initWithInt:2], // no-warning |
| 199 | [[NSNumber alloc] initWithInt:3], // no-warning |
| 200 | [[NSNumber alloc] initWithInt:4], // no-warning |
| 201 | [[NSNumber alloc] initWithInt:5] // no-warning |
| 202 | }; |
| 203 | |
| 204 | NSArray *s_numbers =[[NSArray alloc] initWithObjects: &numbers[0] count:sizeof(numbers) / sizeof(numbers[0])]; // expected-warning{{leak}} |
| 205 | |
| 206 | for (unsigned i = 0 ; i < sizeof(numbers) / sizeof(numbers[0]) ; ++i) |
| 207 | [numbers[i] release]; |
| 208 | } |
| 209 | |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 210 | void pr6699(int x) { |
| 211 | CFDateRef values[2]; |
| 212 | values[0] = values[1] = 0; |
| 213 | |
| 214 | if (x) { |
| 215 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 216 | values[1] = CFDateCreate(0, t); |
| 217 | } |
| 218 | |
| 219 | if (values[1]) { |
| 220 | // A bug in RegionStore::RemoveDeadBindings caused 'values[1]' to get prematurely |
| 221 | // pruned from the store. |
| 222 | CFRelease(values[1]); // no-warning |
| 223 | } |
| 224 | } |
| 225 | |