1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -fblocks -verify %s
2 // RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.NewDelete,alpha.cplusplus.NewDeleteLeaks -std=c++11 -DLEAKS -fblocks -verify %s
3 #include "Inputs/system-header-simulator-cxx.h"
4 #include "Inputs/system-header-simulator-objc.h"
6 typedef __typeof__(sizeof(int)) size_t;
7 extern "C" void *malloc(size_t);
8 extern "C" void free(void *);
10 //----------------------------------------------------------------------------
11 // Check for intersections with unix.Malloc and unix.MallocWithAnnotations
12 // checkers bounded with cplusplus.NewDelete.
13 //----------------------------------------------------------------------------
15 //----- malloc()/free() are subjects of unix.Malloc and unix.MallocWithAnnotations
16 void testMallocFreeNoWarn() {
20 int *p1 = (int *)malloc(sizeof(int));
21 free(++p1); // no warn
23 int *p2 = (int *)malloc(sizeof(int));
27 int *p3 = (int *)malloc(sizeof(int)); // no warn
29 int *p4 = (int *)malloc(sizeof(int));
31 int j = *p4; // no warn
34 void testDeleteMalloced() {
35 int *p = (int *)malloc(sizeof(int));
39 //----- Test free standard new
40 void testFreeOpNew() {
41 void *p = operator new(0);
45 // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
48 void testFreeNewExpr() {
53 // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
56 void testObjcFreeNewed() {
58 NSData *nsdata = [NSData dataWithBytesNoCopy:p length:sizeof(int) freeWhenDone:1];
60 // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
64 void testFreeAfterDelete() {
67 free(p); // expected-warning{{Use of memory after it is freed}}
70 void testStandardPlacementNewAfterDelete() {
73 p = new(p) int; // expected-warning{{Use of memory after it is freed}}