1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -fblocks -verify %s
2 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks -std=c++11 -DLEAKS -fblocks -verify %s
3 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -fblocks -DTEST_INLINABLE_ALLOCATORS -verify %s
4 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks -std=c++11 -DLEAKS -fblocks -DTEST_INLINABLE_ALLOCATORS -verify %s
5 #include "Inputs/system-header-simulator-cxx.h"
6 #include "Inputs/system-header-simulator-objc.h"
8 typedef __typeof__(sizeof(int)) size_t;
9 extern "C" void *malloc(size_t);
10 extern "C" void *alloca(size_t);
11 extern "C" void free(void *);
13 //----------------------------------------------------------------------------
14 // Check for intersections with unix.Malloc and unix.MallocWithAnnotations
15 // checkers bounded with cplusplus.NewDelete.
16 //----------------------------------------------------------------------------
18 //----- malloc()/free() are subjects of unix.Malloc and unix.MallocWithAnnotations
19 void testMallocFreeNoWarn() {
23 int *p1 = (int *)malloc(sizeof(int));
24 free(++p1); // no warn
26 int *p2 = (int *)malloc(sizeof(int));
30 int *p3 = (int *)malloc(sizeof(int)); // no warn
32 int *p4 = (int *)malloc(sizeof(int));
34 int j = *p4; // no warn
36 int *p5 = (int *)alloca(sizeof(int));
40 void testDeleteMalloced() {
41 int *p1 = (int *)malloc(sizeof(int));
44 int *p2 = (int *)__builtin_alloca(sizeof(int));
48 void testUseZeroAllocatedMalloced() {
49 int *p1 = (int *)malloc(0);
53 //----- Test free standard new
54 void testFreeOpNew() {
55 void *p = operator new(0);
59 // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
62 void testFreeNewExpr() {
67 // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
70 void testObjcFreeNewed() {
72 NSData *nsdata = [NSData dataWithBytesNoCopy:p length:sizeof(int) freeWhenDone:1];
74 // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
78 void testFreeAfterDelete() {
81 free(p); // expected-warning{{Use of memory after it is freed}}
84 void testStandardPlacementNewAfterDelete() {
87 p = new(p) int; // expected-warning{{Use of memory after it is freed}}