Anna Zaks | bb2a686 | 2012-02-20 21:10:37 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-store=region -verify %s |
Anna Zaks | ad901a6 | 2012-02-16 22:26:15 +0000 | [diff] [blame] | 2 | |
| 3 | typedef unsigned int UInt32; |
| 4 | typedef signed long CFIndex; |
| 5 | typedef signed char BOOL; |
| 6 | typedef unsigned long NSUInteger; |
| 7 | @class NSString, Protocol; |
| 8 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 9 | typedef struct _NSZone NSZone; |
| 10 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 11 | @protocol NSObject |
| 12 | - (BOOL)isEqual:(id)object; |
| 13 | - (id)retain; |
| 14 | - (oneway void)release; |
| 15 | - (id)autorelease; |
| 16 | - (id)init; |
| 17 | @end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; |
| 18 | @end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; |
| 19 | @end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 20 | @end |
| 21 | @interface NSObject <NSObject> {} |
| 22 | + (id)allocWithZone:(NSZone *)zone; |
| 23 | + (id)alloc; |
| 24 | - (void)dealloc; |
| 25 | @end |
| 26 | @interface NSObject (NSCoderMethods) |
| 27 | - (id)awakeAfterUsingCoder:(NSCoder *)aDecoder; |
| 28 | @end |
| 29 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 30 | typedef struct { |
| 31 | } |
| 32 | NSFastEnumerationState; |
| 33 | @protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; |
| 34 | @end @class NSString, NSDictionary; |
| 35 | @interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value; |
| 36 | @end @interface NSNumber : NSValue - (char)charValue; |
| 37 | - (id)initWithInt:(int)value; |
| 38 | @end @class NSString; |
| 39 | @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; |
| 40 | @end @interface NSArray (NSArrayCreation) + (id)array; |
| 41 | @end @interface NSAutoreleasePool : NSObject { |
| 42 | } |
| 43 | - (void)drain; |
| 44 | @end extern NSString * const NSBundleDidLoadNotification; |
| 45 | typedef double NSTimeInterval; |
| 46 | @interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; |
| 47 | @end typedef unsigned short unichar; |
| 48 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> |
| 49 | - (NSUInteger)length; |
| 50 | - (NSString *)stringByAppendingString:(NSString *)aString; |
| 51 | - ( const char *)UTF8String; |
| 52 | - (id)initWithUTF8String:(const char *)nullTerminatedCString; |
| 53 | + (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
| 54 | @end @class NSString, NSURL, NSError; |
| 55 | @interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
| 56 | + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length; |
| 57 | + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b; |
| 58 | @end |
| 59 | |
| 60 | typedef __typeof(sizeof(int)) size_t; |
| 61 | void *malloc(size_t); |
| 62 | void free(void *); |
| 63 | |
| 64 | // Done with headers. Start testing. |
| 65 | void testNSDatafFreeWhenDoneNoError(NSUInteger dataLength) { |
| 66 | unsigned char *data = (unsigned char *)malloc(42); |
| 67 | NSData *nsdata = [NSData dataWithBytesNoCopy:data length:dataLength]; |
| 68 | free(data); // no warning |
| 69 | } |
| 70 | |
| 71 | // False Negative |
| 72 | void testNSDatafFreeWhenDone(NSUInteger dataLength) { |
| 73 | unsigned char *data = (unsigned char *)malloc(42); |
| 74 | NSData *nsdata = [NSData dataWithBytesNoCopy:data length:dataLength freeWhenDone:1]; |
| 75 | free(data); // false negative |
| 76 | } |