2017-03-04 02:02:02 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete -std=c++11 -verify %s
|
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete,cplusplus.NewDeleteLeaks -DLEAKS -std=c++11 -verify %s
|
2013-04-05 07:46:29 +08:00
|
|
|
|
2013-04-16 04:39:37 +08:00
|
|
|
#include "Inputs/system-header-simulator-for-malloc.h"
|
2013-04-05 07:46:29 +08:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Check that unix.Malloc catches all types of bugs.
|
|
|
|
//--------------------------------------------------
|
|
|
|
void testMallocDoubleFree() {
|
|
|
|
int *p = (int *)malloc(sizeof(int));
|
|
|
|
free(p);
|
|
|
|
free(p); // expected-warning{{Attempt to free released memory}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testMallocLeak() {
|
|
|
|
int *p = (int *)malloc(sizeof(int));
|
2013-04-06 08:41:36 +08:00
|
|
|
} // expected-warning{{Potential leak of memory pointed to by 'p'}}
|
2013-04-05 07:46:29 +08:00
|
|
|
|
|
|
|
void testMallocUseAfterFree() {
|
|
|
|
int *p = (int *)malloc(sizeof(int));
|
|
|
|
free(p);
|
|
|
|
int j = *p; // expected-warning{{Use of memory after it is freed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testMallocBadFree() {
|
|
|
|
int i;
|
|
|
|
free(&i); // expected-warning{{Argument to free() is the address of the local variable 'i', which is not memory allocated by malloc()}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testMallocOffsetFree() {
|
|
|
|
int *p = (int *)malloc(sizeof(int));
|
|
|
|
free(++p); // expected-warning{{Argument to free() is offset by 4 bytes from the start of memory allocated by malloc()}}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------
|
|
|
|
// Check that unix.MismatchedDeallocator catches all types of bugs.
|
|
|
|
//-----------------------------------------------------------------
|
|
|
|
void testMismatchedDeallocator() {
|
|
|
|
int *x = (int *)malloc(sizeof(int));
|
|
|
|
delete x; // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// Check that alpha.cplusplus.NewDelete catches all types of bugs.
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
void testNewDoubleFree() {
|
|
|
|
int *p = new int;
|
|
|
|
delete p;
|
2018-12-17 20:07:57 +08:00
|
|
|
delete p; // expected-warning{{Attempt to free released memory}}
|
2013-04-05 07:46:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testNewLeak() {
|
|
|
|
int *p = new int;
|
2013-04-06 01:55:00 +08:00
|
|
|
}
|
|
|
|
#ifdef LEAKS
|
2013-04-06 08:41:36 +08:00
|
|
|
// expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
|
2013-04-06 01:55:00 +08:00
|
|
|
#endif
|
2013-04-05 07:46:29 +08:00
|
|
|
|
|
|
|
void testNewUseAfterFree() {
|
|
|
|
int *p = (int *)operator new(0);
|
|
|
|
delete p;
|
|
|
|
int j = *p; // expected-warning{{Use of memory after it is freed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testNewBadFree() {
|
|
|
|
int i;
|
|
|
|
delete &i; // expected-warning{{Argument to 'delete' is the address of the local variable 'i', which is not memory allocated by 'new'}}
|
|
|
|
}
|
|
|
|
|
|
|
|
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'}}
|
|
|
|
}
|
2013-04-09 08:30:28 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// 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}}
|
|
|
|
}
|
2015-05-19 03:59:11 +08:00
|
|
|
int *allocIntArray(unsigned c) {
|
|
|
|
return new int[c];
|
|
|
|
}
|
2013-04-09 08:30:28 +08:00
|
|
|
void testMismatchedChangePointeeThroughAssignment() {
|
2015-05-19 03:59:11 +08:00
|
|
|
int *arr = allocIntArray(4);
|
2013-04-09 08:30:28 +08:00
|
|
|
globalPtr = arr;
|
|
|
|
delete arr; // expected-warning{{Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'delete'}}
|
2015-05-19 03:59:11 +08:00
|
|
|
}
|