diff options
Diffstat (limited to 'test/Analysis/malloc.mm')
-rw-r--r-- | test/Analysis/malloc.mm | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/test/Analysis/malloc.mm b/test/Analysis/malloc.mm index c92c966..bd9d2d2 100644 --- a/test/Analysis/malloc.mm +++ b/test/Analysis/malloc.mm @@ -1,9 +1,6 @@ // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-store=region -verify -fblocks %s -#include "Inputs/system-header-simulator-objc.h" - -typedef __typeof(sizeof(int)) size_t; -void *malloc(size_t); -void free(void *); +#import "Inputs/system-header-simulator-objc.h" +#import "Inputs/system-header-simulator-for-malloc.h" // Done with headers. Start testing. void testNSDatafFreeWhenDoneNoError(NSUInteger dataLength) { @@ -21,6 +18,11 @@ void testNSDataFreeWhenDoneYES2(NSUInteger dataLength) { NSData *nsdata = [[NSData alloc] initWithBytesNoCopy:data length:dataLength freeWhenDone:1]; // no-warning } +void testNSDataFreeWhenDoneYES2_with_wrapper(NSUInteger dataLength) { + unsigned char *data = (unsigned char *)malloc(42); + Wrapper *nsdata = [[Wrapper alloc] initWithBytesNoCopy:data length:dataLength]; // no-warning +} + void testNSStringFreeWhenDoneYES3(NSUInteger dataLength) { unsigned char *data = (unsigned char *)malloc(42); NSString *nsstr = [[NSString alloc] initWithBytesNoCopy:data length:dataLength encoding:NSUTF8StringEncoding freeWhenDone:1]; @@ -64,6 +66,11 @@ void testNSStringFreeWhenDoneNO2(NSUInteger dataLength) { NSString *nsstr = [[NSString alloc] initWithCharactersNoCopy:data length:dataLength freeWhenDone:0]; // expected-warning{{leak}} } +void testOffsetFree() { + int *p = (int *)malloc(sizeof(int)); + NSData *nsdata = [NSData dataWithBytesNoCopy:++p length:sizeof(int) freeWhenDone:1]; // expected-warning{{Argument to +dataWithBytesNoCopy:length:freeWhenDone: is offset by 4 bytes from the start of memory allocated by malloc()}} +} + void testRelinquished1() { void *data = malloc(42); NSData *nsdata = [NSData dataWithBytesNoCopy:data length:42 freeWhenDone:1]; @@ -77,6 +84,31 @@ void testRelinquished2() { [NSData dataWithBytesNoCopy:data length:42]; // expected-warning {{Attempt to free released memory}} } +void testNoCopy() { + char *p = (char *)calloc(sizeof(int), 1); + CustomData *w = [CustomData somethingNoCopy:p]; // no-warning +} + +void testFreeWhenDone() { + char *p = (char *)calloc(sizeof(int), 1); + CustomData *w = [CustomData something:p freeWhenDone:1]; // no-warning +} + +void testFreeWhenDonePositive() { + char *p = (char *)calloc(sizeof(int), 1); + CustomData *w = [CustomData something:p freeWhenDone:0]; // expected-warning{{leak}} +} + +void testFreeWhenDoneNoCopy() { + int *p = (int *)malloc(sizeof(int)); + CustomData *w = [CustomData somethingNoCopy:p length:sizeof(int) freeWhenDone:1]; // no-warning +} + +void testFreeWhenDoneNoCopyPositive() { + int *p = (int *)malloc(sizeof(int)); + CustomData *w = [CustomData somethingNoCopy:p length:sizeof(int) freeWhenDone:0]; // expected-warning{{leak}} +} + // Test CF/NS...NoCopy. PR12100: Pointers can escape when custom deallocators are provided. void testNSDatafFreeWhenDone(NSUInteger dataLength) { CFStringRef str; |