[cfe-commits] r150734 - in /cfe/trunk/test/Analysis: malloc.c malloc.mm

Anna Zaks ganna at apple.com
Thu Feb 16 14:26:16 PST 2012


Author: zaks
Date: Thu Feb 16 16:26:15 2012
New Revision: 150734

URL: http://llvm.org/viewvc/llvm-project?rev=150734&view=rev
Log:
[analyzer] MallocChecker: more tests.

Added:
    cfe/trunk/test/Analysis/malloc.mm
Modified:
    cfe/trunk/test/Analysis/malloc.c

Modified: cfe/trunk/test/Analysis/malloc.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/malloc.c?rev=150734&r1=150733&r2=150734&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/malloc.c (original)
+++ cfe/trunk/test/Analysis/malloc.c Thu Feb 16 16:26:15 2012
@@ -485,6 +485,13 @@
   free(GlS.x);
 }
 
+char *ArrayG[12];
+
+void globalArrayTest() {
+  char *p = (char*)malloc(12);
+  ArrayG[0] = p;
+}
+
 // Make sure that we properly handle a pointer stored into a local struct/array.
 typedef struct _StructWithPtr {
   int *memP;
@@ -635,3 +642,11 @@
   StructWithPtr *pSt = &St;
   pSt->memP = malloc(12);
 }
+
+// TODO: This should produce a warning, similar to the previous issue.
+void localArrayTest() {
+  char *p = (char*)malloc(12);
+  char *ArrayL[12];
+  ArrayL[0] = p;
+}
+

Added: cfe/trunk/test/Analysis/malloc.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/malloc.mm?rev=150734&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/malloc.mm (added)
+++ cfe/trunk/test/Analysis/malloc.mm Thu Feb 16 16:26:15 2012
@@ -0,0 +1,76 @@
+// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.unix.Malloc -analyzer-store=region -verify %s
+
+typedef unsigned int UInt32;
+typedef signed long CFIndex;
+typedef signed char BOOL;
+typedef unsigned long NSUInteger;
+ at class NSString, Protocol;
+extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
+typedef struct _NSZone NSZone;
+ at class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
+ at protocol NSObject
+- (BOOL)isEqual:(id)object;
+- (id)retain;
+- (oneway void)release;
+- (id)autorelease;
+- (id)init;
+ at end  @protocol NSCopying  - (id)copyWithZone:(NSZone *)zone;
+ at end  @protocol NSMutableCopying  - (id)mutableCopyWithZone:(NSZone *)zone;
+ at end  @protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder;
+ at end
+ at interface NSObject <NSObject> {}
++ (id)allocWithZone:(NSZone *)zone;
++ (id)alloc;
+- (void)dealloc;
+ at end
+ at interface NSObject (NSCoderMethods)
+- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;
+ at end
+extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
+typedef struct {
+}
+NSFastEnumerationState;
+ at protocol NSFastEnumeration  - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
+ at end           @class NSString, NSDictionary;
+ at interface NSValue : NSObject <NSCopying, NSCoding>  - (void)getValue:(void *)value;
+ at end  @interface NSNumber : NSValue  - (char)charValue;
+- (id)initWithInt:(int)value;
+ at end   @class NSString;
+ at interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>  - (NSUInteger)count;
+ at end  @interface NSArray (NSArrayCreation)  + (id)array;
+ at end       @interface NSAutoreleasePool : NSObject {
+}
+- (void)drain;
+ at end extern NSString * const NSBundleDidLoadNotification;
+typedef double NSTimeInterval;
+ at interface NSDate : NSObject <NSCopying, NSCoding>  - (NSTimeInterval)timeIntervalSinceReferenceDate;
+ at end            typedef unsigned short unichar;
+ at interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
+- (NSUInteger)length;
+- (NSString *)stringByAppendingString:(NSString *)aString;
+- ( const char *)UTF8String;
+- (id)initWithUTF8String:(const char *)nullTerminatedCString;
++ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
+ at end        @class NSString, NSURL, NSError;
+ at interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding>  - (NSUInteger)length;
++ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
++ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
+ at end 
+
+typedef __typeof(sizeof(int)) size_t;
+void *malloc(size_t);
+void free(void *);
+
+// Done with headers. Start testing.
+void testNSDatafFreeWhenDoneNoError(NSUInteger dataLength) {
+  unsigned char *data = (unsigned char *)malloc(42);
+  NSData *nsdata = [NSData dataWithBytesNoCopy:data length:dataLength];
+  free(data); // no warning
+}
+
+// False Negative
+void testNSDatafFreeWhenDone(NSUInteger dataLength) {
+  unsigned char *data = (unsigned char *)malloc(42);
+  NSData *nsdata = [NSData dataWithBytesNoCopy:data length:dataLength freeWhenDone:1];
+  free(data); // false negative
+}





More information about the cfe-commits mailing list