2013-04-06 01:55:07 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -fblocks -verify %s
|
2014-10-21 20:41:36 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks -std=c++11 -DLEAKS -fblocks -verify %s
|
2013-03-30 08:50:37 +08:00
|
|
|
#include "Inputs/system-header-simulator-cxx.h"
|
|
|
|
#include "Inputs/system-header-simulator-objc.h"
|
|
|
|
|
|
|
|
typedef __typeof__(sizeof(int)) size_t;
|
|
|
|
extern "C" void *malloc(size_t);
|
2015-02-19 21:36:20 +08:00
|
|
|
extern "C" void *alloca(size_t);
|
2013-03-30 08:50:37 +08:00
|
|
|
extern "C" void free(void *);
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Check for intersections with unix.Malloc and unix.MallocWithAnnotations
|
|
|
|
// checkers bounded with cplusplus.NewDelete.
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2013-04-05 08:37:32 +08:00
|
|
|
//----- malloc()/free() are subjects of unix.Malloc and unix.MallocWithAnnotations
|
2013-03-30 08:50:37 +08:00
|
|
|
void testMallocFreeNoWarn() {
|
|
|
|
int i;
|
|
|
|
free(&i); // no warn
|
|
|
|
|
|
|
|
int *p1 = (int *)malloc(sizeof(int));
|
|
|
|
free(++p1); // no warn
|
|
|
|
|
|
|
|
int *p2 = (int *)malloc(sizeof(int));
|
|
|
|
free(p2);
|
|
|
|
free(p2); // no warn
|
|
|
|
|
|
|
|
int *p3 = (int *)malloc(sizeof(int)); // no warn
|
2013-04-05 08:37:32 +08:00
|
|
|
|
|
|
|
int *p4 = (int *)malloc(sizeof(int));
|
|
|
|
free(p4);
|
|
|
|
int j = *p4; // no warn
|
2015-02-19 21:36:20 +08:00
|
|
|
|
|
|
|
int *p5 = (int *)alloca(sizeof(int));
|
|
|
|
free(p5); // no warn
|
2013-03-30 08:50:37 +08:00
|
|
|
}
|
|
|
|
|
2013-03-30 09:22:45 +08:00
|
|
|
void testDeleteMalloced() {
|
2015-02-19 21:36:20 +08:00
|
|
|
int *p1 = (int *)malloc(sizeof(int));
|
|
|
|
delete p1; // no warn
|
|
|
|
|
|
|
|
int *p2 = (int *)__builtin_alloca(sizeof(int));
|
|
|
|
delete p2; // no warn
|
2013-03-30 09:22:45 +08:00
|
|
|
}
|
|
|
|
|
2015-04-14 22:18:04 +08:00
|
|
|
void testUseZeroAllocatedMalloced() {
|
|
|
|
int *p1 = (int *)malloc(0);
|
|
|
|
*p1 = 1; // no warn
|
|
|
|
}
|
|
|
|
|
2013-03-30 08:50:37 +08:00
|
|
|
//----- Test free standard new
|
|
|
|
void testFreeOpNew() {
|
|
|
|
void *p = operator new(0);
|
|
|
|
free(p);
|
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-03-30 08:50:37 +08:00
|
|
|
|
|
|
|
void testFreeNewExpr() {
|
|
|
|
int *p = new int;
|
|
|
|
free(p);
|
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-03-30 08:50:37 +08:00
|
|
|
|
|
|
|
void testObjcFreeNewed() {
|
|
|
|
int *p = new int;
|
2013-04-06 01:55:00 +08:00
|
|
|
NSData *nsdata = [NSData dataWithBytesNoCopy:p length:sizeof(int) freeWhenDone:1];
|
|
|
|
#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-03-30 08:50:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testFreeAfterDelete() {
|
|
|
|
int *p = new int;
|
|
|
|
delete p;
|
|
|
|
free(p); // expected-warning{{Use of memory after it is freed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testStandardPlacementNewAfterDelete() {
|
|
|
|
int *p = new int;
|
|
|
|
delete p;
|
|
|
|
p = new(p) int; // expected-warning{{Use of memory after it is freed}}
|
|
|
|
}
|