Debian Codegraph @

clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp

raw
1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedLocalVarsChecker -verify %s
2
3 #include "mock-types.h"
4 #include "mock-system-header.h"
5
6 void someFunction();
7
8 namespace raw_ptr {
9 void foo() {
10 RefCountable *bar;
11 // FIXME: later on we might warn on uninitialized vars too
12 }
13
14 void bar(RefCountable *) {}
15 } // namespace raw_ptr
16
17 namespace reference {
18 void foo_ref() {
19 RefCountable automatic;
20 RefCountable &bar = automatic;
21 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
22 someFunction();
23 bar.method();
24 }
25
26 void foo_ref_trivial() {
27 RefCountable automatic;
28 RefCountable &bar = automatic;
29 }
30
31 void bar_ref(RefCountable &) {}
32 } // namespace reference
33
34 namespace guardian_scopes {
35 void foo1() {
36 RefPtr<RefCountable> foo;
37 { RefCountable *bar = foo.get(); }
38 }
39
40 void foo2() {
41 RefPtr<RefCountable> foo;
42 // missing embedded scope here
43 RefCountable *bar = foo.get();
44 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
45 someFunction();
46 bar->method();
47 }
48
49 void foo3() {
50 RefPtr<RefCountable> foo;
51 {
52 { RefCountable *bar = foo.get(); }
53 }
54 }
55
56 void foo4() {
57 {
58 RefPtr<RefCountable> foo;
59 { RefCountable *bar = foo.get(); }
60 }
61 }
62
63 void foo5() {
64 RefPtr<RefCountable> foo;
65 auto* bar = foo.get();
66 bar->trivial();
67 }
68
69 void foo6() {
70 RefPtr<RefCountable> foo;
71 auto* bar = foo.get();
72 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
73 bar->method();
74 }
75
76 struct SelfReferencingStruct {
77 SelfReferencingStruct* ptr;
78 RefCountable* obj { nullptr };
79 };
80
81 void foo7(RefCountable* obj) {
82 SelfReferencingStruct bar = { &bar, obj };
83 bar.obj->method();
84 }
85
86 } // namespace guardian_scopes
87
88 namespace auto_keyword {
89 class Foo {
90 RefCountable *provide_ref_ctnbl();
91
92 void evil_func() {
93 RefCountable *bar = provide_ref_ctnbl();
94 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
95 auto *baz = provide_ref_ctnbl();
96 // expected-warning@-1{{Local variable 'baz' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
97 auto *baz2 = this->provide_ref_ctnbl();
98 // expected-warning@-1{{Local variable 'baz2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
99 [[clang::suppress]] auto *baz_suppressed = provide_ref_ctnbl(); // no-warning
100 }
101
102 void func() {
103 RefCountable *bar = provide_ref_ctnbl();
104 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
105 if (bar)
106 bar->method();
107 }
108 };
109 } // namespace auto_keyword
110
111 namespace guardian_casts {
112 void foo1() {
113 RefPtr<RefCountable> foo;
114 {
115 RefCountable *bar = downcast<RefCountable>(foo.get());
116 bar->method();
117 }
118 foo->method();
119 }
120
121 void foo2() {
122 RefPtr<RefCountable> foo;
123 {
124 RefCountable *bar =
125 static_cast<RefCountable *>(downcast<RefCountable>(foo.get()));
126 someFunction();
127 }
128 }
129 } // namespace guardian_casts
130
131 namespace guardian_ref_conversion_operator {
132 void foo() {
133 Ref<RefCountable> rc;
134 {
135 RefCountable &rr = rc;
136 rr.method();
137 someFunction();
138 }
139 }
140 } // namespace guardian_ref_conversion_operator
141
142 namespace ignore_for_if {
143 RefCountable *provide_ref_ctnbl() { return nullptr; }
144
145 void foo() {
146 // no warnings
147 if (RefCountable *a = provide_ref_ctnbl())
148 a->trivial();
149 for (RefCountable *b = provide_ref_ctnbl(); b != nullptr;)
150 b->trivial();
151 RefCountable *array[1];
152 for (RefCountable *c : array)
153 c->trivial();
154 while (RefCountable *d = provide_ref_ctnbl())
155 d->trivial();
156 do {
157 RefCountable *e = provide_ref_ctnbl();
158 e->trivial();
159 } while (1);
160 someFunction();
161 }
162
163 void bar() {
164 if (RefCountable *a = provide_ref_ctnbl()) {
165 // expected-warning@-1{{Local variable 'a' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
166 a->method();
167 }
168 for (RefCountable *b = provide_ref_ctnbl(); b != nullptr;) {
169 // expected-warning@-1{{Local variable 'b' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
170 b->method();
171 }
172 RefCountable *array[1];
173 for (RefCountable *c : array) {
174 // expected-warning@-1{{Local variable 'c' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
175 c->method();
176 }
177
178 while (RefCountable *d = provide_ref_ctnbl()) {
179 // expected-warning@-1{{Local variable 'd' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
180 d->method();
181 }
182 do {
183 RefCountable *e = provide_ref_ctnbl();
184 // expected-warning@-1{{Local variable 'e' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
185 e->method();
186 } while (1);
187 someFunction();
188 }
189
190 } // namespace ignore_for_if
191
192 namespace ignore_system_headers {
193
194 RefCountable *provide_ref_ctnbl();
195
196 void system_header() {
197 localVar<RefCountable>(provide_ref_ctnbl);
198 }
199
200 } // ignore_system_headers
201
202 namespace conditional_op {
203 RefCountable *provide_ref_ctnbl();
204 bool bar();
205
206 void foo() {
207 RefCountable *a = bar() ? nullptr : provide_ref_ctnbl();
208 // expected-warning@-1{{Local variable 'a' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
209 RefPtr<RefCountable> b = provide_ref_ctnbl();
210 {
211 RefCountable* c = bar() ? nullptr : b.get();
212 c->method();
213 RefCountable* d = bar() ? b.get() : nullptr;
214 d->method();
215 }
216 }
217
218 } // namespace conditional_op
219
220 namespace local_assignment_basic {
221
222 RefCountable *provide_ref_cntbl();
223
224 void foo(RefCountable* a) {
225 RefCountable* b = a;
226 // expected-warning@-1{{Local variable 'b' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
227 if (b->trivial())
228 b = provide_ref_cntbl();
229 }
230
231 void bar(RefCountable* a) {
232 RefCountable* b;
233 // expected-warning@-1{{Local variable 'b' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
234 b = provide_ref_cntbl();
235 }
236
237 void baz() {
238 RefPtr a = provide_ref_cntbl();
239 {
240 RefCountable* b = a.get();
241 // expected-warning@-1{{Local variable 'b' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
242 b = provide_ref_cntbl();
243 }
244 }
245
246 } // namespace local_assignment_basic
247
248 namespace local_assignment_to_parameter {
249
250 RefCountable *provide_ref_cntbl();
251 void someFunction();
252
253 void foo(RefCountable* a) {
254 a = provide_ref_cntbl();
255 // expected-warning@-1{{Assignment to an uncounted parameter 'a' is unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
256 someFunction();
257 a->method();
258 }
259
260 } // namespace local_assignment_to_parameter
261
262 namespace local_assignment_to_static_local {
263
264 RefCountable *provide_ref_cntbl();
265 void someFunction();
266
267 void foo() {
268 static RefCountable* a = nullptr;
269 // expected-warning@-1{{Static local variable 'a' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
270 a = provide_ref_cntbl();
271 someFunction();
272 a->method();
273 }
274
275 } // namespace local_assignment_to_static_local
276
277 namespace local_assignment_to_global {
278
279 RefCountable *provide_ref_cntbl();
280 void someFunction();
281
282 RefCountable* g_a = nullptr;
283 // expected-warning@-1{{Global variable 'local_assignment_to_global::g_a' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
284
285 void foo() {
286 g_a = provide_ref_cntbl();
287 someFunction();
288 g_a->method();
289 }
290
291 } // namespace local_assignment_to_global