1 // RUN: %clang_cc1 -Wfree-nonheap-object -std=c++11 -x c++ -fsyntax-only -verify %s
2
free(void *)3 extern "C" void free(void *) {}
4
5 namespace std {
6 using size_t = decltype(sizeof(0));
7 void *malloc(size_t);
8 void free(void *p);
9 } // namespace std
10
11 int GI;
12
13 struct S {
operator char*S14 operator char *() { return ptr; }
15
CFreeS16 void CFree() {
17 ::free(&ptr); // expected-warning {{attempt to call free on non-heap object 'ptr'}}
18 ::free(&I); // expected-warning {{attempt to call free on non-heap object 'I'}}
19 ::free(ptr);
20 }
21
CXXFreeS22 void CXXFree() {
23 std::free(&ptr); // expected-warning {{attempt to call std::free on non-heap object 'ptr'}}
24 std::free(&I); // expected-warning {{attempt to call std::free on non-heap object 'I'}}
25 std::free(ptr);
26 }
27
28 private:
29 char *ptr = (char *)std::malloc(10);
30 static int I;
31 };
32
33 int S::I = 0;
34
test1()35 void test1() {
36 {
37 free(&GI); // expected-warning {{attempt to call free on non-heap object 'GI'}}
38 }
39 {
40 static int SI = 0;
41 free(&SI); // expected-warning {{attempt to call free on non-heap object 'SI'}}
42 }
43 {
44 int I = 0;
45 free(&I); // expected-warning {{attempt to call free on non-heap object 'I'}}
46 }
47 {
48 int I = 0;
49 int *P = &I;
50 free(P);
51 }
52 {
53 void *P = std::malloc(8);
54 free(P); // FIXME diagnosing this would require control flow analysis.
55 }
56 {
57 int A[] = {0, 1, 2, 3};
58 free(A); // expected-warning {{attempt to call free on non-heap object 'A'}}
59 }
60 {
61 int A[] = {0, 1, 2, 3};
62 free(&A); // expected-warning {{attempt to call free on non-heap object 'A'}}
63 }
64 {
65 S s;
66 free(s);
67 free(&s); // expected-warning {{attempt to call free on non-heap object 's'}}
68 }
69 {
70 S s;
71 s.CFree();
72 }
73 }
74
test2()75 void test2() {
76 {
77 std::free(&GI); // expected-warning {{attempt to call std::free on non-heap object 'GI'}}
78 }
79 {
80 static int SI = 0;
81 std::free(&SI); // expected-warning {{attempt to call std::free on non-heap object 'SI'}}
82 }
83 {
84 int I = 0;
85 std::free(&I); // expected-warning {{attempt to call std::free on non-heap object 'I'}}
86 }
87 {
88 int I = 0;
89 int *P = &I;
90 std::free(P); // FIXME diagnosing this would require control flow analysis.
91 }
92 {
93 void *P = std::malloc(8);
94 std::free(P);
95 }
96 {
97 int A[] = {0, 1, 2, 3};
98 std::free(A); // expected-warning {{attempt to call std::free on non-heap object 'A'}}
99 }
100 {
101 int A[] = {0, 1, 2, 3};
102 std::free(&A); // expected-warning {{attempt to call std::free on non-heap object 'A'}}
103 }
104 {
105 S s;
106 std::free(s);
107 std::free(&s); // expected-warning {{attempt to call std::free on non-heap object 's'}}
108 }
109 {
110 S s;
111 s.CXXFree();
112 }
113 }
114