malloc.mm revision bb2a6864f111e13f7905725963649c60c60bf18b
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-store=region -verify %s 2 3typedef unsigned int UInt32; 4typedef signed long CFIndex; 5typedef signed char BOOL; 6typedef unsigned long NSUInteger; 7@class NSString, Protocol; 8extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); 9typedef 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 29extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); 30typedef struct { 31} 32NSFastEnumerationState; 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; 45typedef 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 60typedef __typeof(sizeof(int)) size_t; 61void *malloc(size_t); 62void free(void *); 63 64// Done with headers. Start testing. 65void 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 72void 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} 77