diff options
Diffstat (limited to 'test/Analysis/malloc.mm')
-rw-r--r-- | test/Analysis/malloc.mm | 80 |
1 files changed, 74 insertions, 6 deletions
diff --git a/test/Analysis/malloc.mm b/test/Analysis/malloc.mm index 3515a4f..7a9d881 100644 --- a/test/Analysis/malloc.mm +++ b/test/Analysis/malloc.mm @@ -9,7 +9,6 @@ void free(void *); void testNSDatafFreeWhenDoneNoError(NSUInteger dataLength) { unsigned char *data = (unsigned char *)malloc(42); NSData *nsdata = [NSData dataWithBytesNoCopy:data length:dataLength]; - free(data); // no warning } void testNSDataFreeWhenDoneYES(NSUInteger dataLength) { @@ -22,6 +21,16 @@ void testNSDataFreeWhenDoneYES2(NSUInteger dataLength) { NSData *nsdata = [[NSData alloc] initWithBytesNoCopy:data length:dataLength freeWhenDone:1]; // 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]; +} + +void testNSStringFreeWhenDoneYES4(NSUInteger dataLength) { + unichar *data = (unichar*)malloc(42); + NSString *nsstr = [[NSString alloc] initWithCharactersNoCopy:data length:dataLength freeWhenDone:1]; + free(data); //expected-warning {{Attempt to free non-owned memory}} +} void testNSStringFreeWhenDoneYES(NSUInteger dataLength) { unsigned char *data = (unsigned char *)malloc(42); @@ -55,11 +64,17 @@ void testNSStringFreeWhenDoneNO2(NSUInteger dataLength) { NSString *nsstr = [[NSString alloc] initWithCharactersNoCopy:data length:dataLength freeWhenDone:0]; // expected-warning{{leak}} } -// TODO: False Negative. -void testNSDatafFreeWhenDoneFN(NSUInteger dataLength) { - unsigned char *data = (unsigned char *)malloc(42); - NSData *nsdata = [NSData dataWithBytesNoCopy:data length:dataLength freeWhenDone:1]; - free(data); // false negative +void testRelinquished1() { + void *data = malloc(42); + NSData *nsdata = [NSData dataWithBytesNoCopy:data length:42 freeWhenDone:1]; + free(data); // expected-warning {{Attempt to free non-owned memory}} +} + +void testRelinquished2() { + void *data = malloc(42); + NSData *nsdata; + free(data); + [NSData dataWithBytesNoCopy:data length:42]; // expected-warning {{Attempt to free released memory}} } // Test CF/NS...NoCopy. PR12100: Pointers can escape when custom deallocators are provided. @@ -153,4 +168,57 @@ static void releaseDataCallback (void *info, const void *data, size_t size) { void testCGDataProviderCreateWithData() { void* b = calloc(8, 8); CGDataProviderRef p = CGDataProviderCreateWithData(0, b, 8*8, releaseDataCallback); +} + +// Assume that functions which take a function pointer can free memory even if +// they are defined in system headers and take the const pointer to the +// allocated memory. (radar://11160612) +extern CGDataProviderRef UnknownFunWithCallback(void *info, + const void *data, size_t size, + CGDataProviderReleaseDataCallback releaseData) + __attribute__((visibility("default"))); +void testUnknownFunWithCallBack() { + void* b = calloc(8, 8); + CGDataProviderRef p = UnknownFunWithCallback(0, b, 8*8, releaseDataCallback); +} + +// Test blocks. +void acceptBlockParam(void *, void (^block)(void *), unsigned); +void testCallWithBlockCallback() { + void *l = malloc(12); + acceptBlockParam(l, ^(void *i) { free(i); }, sizeof(char *)); +} + +// Test blocks in system headers. +void testCallWithBlockCallbackInSystem() { + void *l = malloc(12); + SystemHeaderFunctionWithBlockParam(l, ^(void *i) { free(i); }, sizeof(char *)); +} + +// Test escape into NSPointerArray. radar://11691035, PR13140 +void foo(NSPointerArray* pointerArray) { + + void* p1 = malloc (1024); + if (p1) { + [pointerArray addPointer:p1]; + } + + void* p2 = malloc (1024); + if (p2) { + [pointerArray insertPointer:p2 atIndex:1]; + } + + void* p3 = malloc (1024); + if (p3) { + [pointerArray replacePointerAtIndex:1 withPointer:p3]; + } + + // Freeing the buffer is allowed. + void* buffer = [pointerArray pointerAtIndex:0]; + free(buffer); +} + +void noCrashOnVariableArgumentSelector() { + NSMutableString *myString = [NSMutableString stringWithString:@"some text"]; + [myString appendFormat:@"some text = %d", 3]; }
\ No newline at end of file |