NewDelete-intersections.mm revision 33337ca4d89605025818daf83390ab4271d598d9
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,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"
5
6typedef __typeof__(sizeof(int)) size_t;
7extern "C" void *malloc(size_t);
8extern "C" void *alloca(size_t);
9extern "C" void free(void *);
10
11//----------------------------------------------------------------------------
12// Check for intersections with unix.Malloc and unix.MallocWithAnnotations 
13// checkers bounded with cplusplus.NewDelete.
14//----------------------------------------------------------------------------
15
16//----- malloc()/free() are subjects of unix.Malloc and unix.MallocWithAnnotations
17void testMallocFreeNoWarn() {
18  int i;
19  free(&i); // no warn
20
21  int *p1 = (int *)malloc(sizeof(int));
22  free(++p1); // no warn
23
24  int *p2 = (int *)malloc(sizeof(int));
25  free(p2);
26  free(p2); // no warn
27
28  int *p3 = (int *)malloc(sizeof(int)); // no warn
29
30  int *p4 = (int *)malloc(sizeof(int));
31  free(p4);
32  int j = *p4; // no warn
33
34  int *p5 = (int *)alloca(sizeof(int));
35  free(p5); // no warn
36}
37
38void testDeleteMalloced() {
39  int *p1 = (int *)malloc(sizeof(int));
40  delete p1; // no warn
41
42  int *p2 = (int *)__builtin_alloca(sizeof(int));
43  delete p2; // no warn
44} 
45
46void testUseZeroAllocatedMalloced() {
47  int *p1 = (int *)malloc(0);
48  *p1 = 1; // no warn
49}
50
51//----- Test free standard new
52void testFreeOpNew() {
53  void *p = operator new(0);
54  free(p);
55}
56#ifdef LEAKS
57// expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
58#endif
59
60void testFreeNewExpr() {
61  int *p = new int;
62  free(p);
63}
64#ifdef LEAKS
65// expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
66#endif
67
68void testObjcFreeNewed() {
69  int *p = new int;
70  NSData *nsdata = [NSData dataWithBytesNoCopy:p length:sizeof(int) freeWhenDone:1];
71#ifdef LEAKS
72  // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
73#endif
74}
75
76void testFreeAfterDelete() {
77  int *p = new int;  
78  delete p;
79  free(p); // expected-warning{{Use of memory after it is freed}}
80}
81
82void testStandardPlacementNewAfterDelete() {
83  int *p = new int;  
84  delete p;
85  p = new(p) int; // expected-warning{{Use of memory after it is freed}}
86}
87