diff options
Diffstat (limited to 'test/Analysis/Malloc+MismatchedDeallocator+NewDelete.cpp')
-rw-r--r-- | test/Analysis/Malloc+MismatchedDeallocator+NewDelete.cpp | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/test/Analysis/Malloc+MismatchedDeallocator+NewDelete.cpp b/test/Analysis/Malloc+MismatchedDeallocator+NewDelete.cpp index b0bb173..5a596d4 100644 --- a/test/Analysis/Malloc+MismatchedDeallocator+NewDelete.cpp +++ b/test/Analysis/Malloc+MismatchedDeallocator+NewDelete.cpp @@ -1,8 +1,7 @@ -// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,alpha.cplusplus.NewDelete -analyzer-store region -std=c++11 -verify %s +// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete -std=c++11 -verify %s +// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete,alpha.cplusplus.NewDeleteLeaks -DLEAKS -std=c++11 -verify %s -typedef __typeof(sizeof(int)) size_t; -void *malloc(size_t); -void free(void *); +#include "Inputs/system-header-simulator-for-malloc.h" //-------------------------------------------------- // Check that unix.Malloc catches all types of bugs. @@ -15,7 +14,7 @@ void testMallocDoubleFree() { void testMallocLeak() { int *p = (int *)malloc(sizeof(int)); -} // expected-warning{{Memory is never released; potential leak of memory pointed to by 'p'}} +} // expected-warning{{Potential leak of memory pointed to by 'p'}} void testMallocUseAfterFree() { int *p = (int *)malloc(sizeof(int)); @@ -52,7 +51,10 @@ void testNewDoubleFree() { void testNewLeak() { int *p = new int; -} // expected-warning{{Memory is never released; potential leak of memory pointed to by 'p'}} +} +#ifdef LEAKS +// expected-warning@-2 {{Potential leak of memory pointed to by 'p'}} +#endif void testNewUseAfterFree() { int *p = (int *)operator new(0); @@ -69,3 +71,35 @@ void testNewOffsetFree() { int *p = new int; operator delete(++p); // expected-warning{{Argument to operator delete is offset by 4 bytes from the start of memory allocated by 'new'}} } + +//---------------------------------------------------------------- +// Test that we check for free errors on escaped pointers. +//---------------------------------------------------------------- +void changePtr(int **p); +static int *globalPtr; +void changePointee(int *p); + +void testMismatchedChangePtrThroughCall() { + int *p = (int*)malloc(sizeof(int)*4); + changePtr(&p); + delete p; // no-warning the value of the pointer might have changed +} + +void testMismatchedChangePointeeThroughCall() { + int *p = (int*)malloc(sizeof(int)*4); + changePointee(p); + delete p; // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}} +} + +void testShouldReportDoubleFreeNotMismatched() { + int *p = (int*)malloc(sizeof(int)*4); + globalPtr = p; + free(p); + delete globalPtr; // expected-warning {{Attempt to free released memory}} +} + +void testMismatchedChangePointeeThroughAssignment() { + int *arr = new int[4]; + globalPtr = arr; + delete arr; // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'delete'}} +}
\ No newline at end of file |