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 void foo8(RefCountable* obj) {
87 RefPtr<RefCountable> foo;
88 {
89 RefCountable *bar = foo.get();
90 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
91 foo = nullptr;
92 bar->method();
93 }
94 RefPtr<RefCountable> baz;
95 {
96 RefCountable *bar = baz.get();
97 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
98 baz = obj;
99 bar->method();
100 }
101 foo = nullptr;
102 {
103 RefCountable *bar = foo.get();
104 // No warning. It's okay to mutate RefPtr in an outer scope.
105 bar->method();
106 }
107 foo = obj;
108 {
109 RefCountable *bar = foo.get();
110 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
111 foo.releaseNonNull();
112 bar->method();
113 }
114 {
115 RefCountable *bar = foo.get();
116 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
117 foo = obj ? obj : nullptr;
118 bar->method();
119 }
120 {
121 RefCountable *bar = foo->trivial() ? foo.get() : nullptr;
122 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
123 foo = nullptr;
124 foo = (RefCountable *)0;
125 bar->method();
126 }
127 }
128
129 void foo9(RefCountable& o) {
130 Ref<RefCountable> guardian(o);
131 {
132 RefCountable &bar = guardian.get();
133 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
134 guardian = o; // We don't detect that we're setting it to the same value.
135 bar.method();
136 }
137 {
138 RefCountable *bar = guardian.ptr();
139 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
140 Ref<RefCountable> other(*bar); // We don't detect other has the same value as guardian.
141 guardian.swap(other);
142 bar->method();
143 }
144 {
145 RefCountable *bar = guardian.ptr();
146 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
147 Ref<RefCountable> other(static_cast<Ref<RefCountable>&&>(guardian));
148 bar->method();
149 }
150 {
151 RefCountable *bar = guardian.ptr();
152 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
153 guardian.leakRef();
154 bar->method();
155 }
156 {
157 RefCountable *bar = guardian.ptr();
158 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
159 guardian = o.trivial() ? o : *bar;
160 bar->method();
161 }
162 }
163
164 } // namespace guardian_scopes
165
166 namespace auto_keyword {
167 class Foo {
168 RefCountable *provide_ref_ctnbl();
169
170 void evil_func() {
171 RefCountable *bar = provide_ref_ctnbl();
172 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
173 auto *baz = provide_ref_ctnbl();
174 // expected-warning@-1{{Local variable 'baz' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
175 auto *baz2 = this->provide_ref_ctnbl();
176 // expected-warning@-1{{Local variable 'baz2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
177 [[clang::suppress]] auto *baz_suppressed = provide_ref_ctnbl(); // no-warning
178 }
179
180 void func() {
181 RefCountable *bar = provide_ref_ctnbl();
182 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
183 if (bar)
184 bar->method();
185 }
186 };
187 } // namespace auto_keyword
188
189 namespace guardian_casts {
190 void foo1() {
191 RefPtr<RefCountable> foo;
192 {
193 RefCountable *bar = downcast<RefCountable>(foo.get());
194 bar->method();
195 }
196 foo->method();
197 }
198
199 void foo2() {
200 RefPtr<RefCountable> foo;
201 {
202 RefCountable *bar =
203 static_cast<RefCountable *>(downcast<RefCountable>(foo.get()));
204 someFunction();
205 }
206 }
207 } // namespace guardian_casts
208
209 namespace casts {
210
211 RefCountable* provide() { return nullptr; }
212 RefCountable* downcast(RefCountable*);
213 template<class T> T* bitwise_cast(T*);
214 template<class T> T* bit_cast(T*);
215
216 void foo() {
217 auto* cast1 = downcast(provide());
218 auto* cast2 = bitwise_cast(provide());
219 auto* cast3 = bit_cast(provide());
220 }
221 } // namespace casts
222
223 namespace guardian_ref_conversion_operator {
224 void foo() {
225 Ref<RefCountable> rc;
226 {
227 RefCountable &rr = rc;
228 rr.method();
229 someFunction();
230 }
231 }
232 } // namespace guardian_ref_conversion_operator
233
234 namespace ignore_for_if {
235 RefCountable *provide_ref_ctnbl() { return nullptr; }
236
237 void foo() {
238 // no warnings
239 if (RefCountable *a = provide_ref_ctnbl())
240 a->trivial();
241 for (RefCountable *b = provide_ref_ctnbl(); b != nullptr;)
242 b->trivial();
243 RefCountable *array[1];
244 for (RefCountable *c : array)
245 c->trivial();
246 while (RefCountable *d = provide_ref_ctnbl())
247 d->trivial();
248 do {
249 RefCountable *e = provide_ref_ctnbl();
250 e->trivial();
251 } while (1);
252 someFunction();
253 }
254
255 void bar() {
256 if (RefCountable *a = provide_ref_ctnbl()) {
257 // expected-warning@-1{{Local variable 'a' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
258 a->method();
259 }
260 for (RefCountable *b = provide_ref_ctnbl(); b != nullptr;) {
261 // expected-warning@-1{{Local variable 'b' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
262 b->method();
263 }
264 RefCountable *array[1];
265 for (RefCountable *c : array) {
266 // expected-warning@-1{{Local variable 'c' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
267 c->method();
268 }
269
270 while (RefCountable *d = provide_ref_ctnbl()) {
271 // expected-warning@-1{{Local variable 'd' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
272 d->method();
273 }
274 do {
275 RefCountable *e = provide_ref_ctnbl();
276 // expected-warning@-1{{Local variable 'e' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
277 e->method();
278 } while (1);
279 someFunction();
280 }
281
282 } // namespace ignore_for_if
283
284 namespace ignore_system_headers {
285
286 RefCountable *provide_ref_ctnbl();
287
288 void system_header() {
289 localVar<RefCountable>(provide_ref_ctnbl);
290 }
291
292 } // ignore_system_headers
293
294 namespace conditional_op {
295 RefCountable *provide_ref_ctnbl();
296 bool bar();
297
298 void foo() {
299 RefCountable *a = bar() ? nullptr : provide_ref_ctnbl();
300 // expected-warning@-1{{Local variable 'a' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
301 RefPtr<RefCountable> b = provide_ref_ctnbl();
302 {
303 RefCountable* c = bar() ? nullptr : b.get();
304 c->method();
305 RefCountable* d = bar() ? b.get() : nullptr;
306 d->method();
307 }
308 }
309
310 } // namespace conditional_op
311
312 namespace local_assignment_basic {
313
314 RefCountable *provide_ref_cntbl();
315
316 void foo(RefCountable* a) {
317 RefCountable* b = a;
318 // expected-warning@-1{{Local variable 'b' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
319 if (b->trivial())
320 b = provide_ref_cntbl();
321 }
322
323 void bar(RefCountable* a) {
324 RefCountable* b;
325 // expected-warning@-1{{Local variable 'b' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
326 b = provide_ref_cntbl();
327 }
328
329 void baz() {
330 RefPtr a = provide_ref_cntbl();
331 {
332 RefCountable* b = a.get();
333 // expected-warning@-1{{Local variable 'b' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
334 b = provide_ref_cntbl();
335 }
336 }
337
338 } // namespace local_assignment_basic
339
340 namespace local_assignment_to_parameter {
341
342 RefCountable *provide_ref_cntbl();
343 void someFunction();
344
345 void foo(RefCountable* a) {
346 a = provide_ref_cntbl();
347 // expected-warning@-1{{Assignment to an uncounted parameter 'a' is unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
348 someFunction();
349 a->method();
350 }
351
352 } // namespace local_assignment_to_parameter
353
354 namespace local_assignment_to_static_local {
355
356 RefCountable *provide_ref_cntbl();
357 void someFunction();
358
359 void foo() {
360 static RefCountable* a = nullptr;
361 // expected-warning@-1{{Static local variable 'a' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
362 a = provide_ref_cntbl();
363 someFunction();
364 a->method();
365 }
366
367 } // namespace local_assignment_to_static_local
368
369 namespace local_assignment_to_global {
370
371 RefCountable *provide_ref_cntbl();
372 void someFunction();
373
374 RefCountable* g_a = nullptr;
375 // expected-warning@-1{{Global variable 'local_assignment_to_global::g_a' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
376
377 void foo() {
378 g_a = provide_ref_cntbl();
379 someFunction();
380 g_a->method();
381 }
382
383 } // namespace local_assignment_to_global
384
385 namespace local_refcountable_checkable_object {
386
387 RefCountableAndCheckable* provide_obj();
388
389 void local_raw_ptr() {
390 RefCountableAndCheckable* a = nullptr;
391 // expected-warning@-1{{Local variable 'a' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
392 a = provide_obj();
393 a->method();
394 }
395
396 void local_checked_ptr() {
397 CheckedPtr<RefCountableAndCheckable> a = nullptr;
398 a = provide_obj();
399 a->method();
400 }
401
402 void local_var_with_guardian_checked_ptr() {
403 CheckedPtr<RefCountableAndCheckable> a = provide_obj();
404 {
405 auto* b = a.get();
406 b->method();
407 }
408 }
409
410 void local_var_with_guardian_checked_ptr_with_assignment() {
411 CheckedPtr<RefCountableAndCheckable> a = provide_obj();
412 {
413 RefCountableAndCheckable* b = a.get();
414 // expected-warning@-1{{Local variable 'b' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
415 b = provide_obj();
416 b->method();
417 }
418 }
419
420 void local_var_with_guardian_checked_ref() {
421 CheckedRef<RefCountableAndCheckable> a = *provide_obj();
422 {
423 RefCountableAndCheckable& b = a;
424 b.method();
425 }
426 }
427
428 void static_var() {
429 static RefCountableAndCheckable* a = nullptr;
430 // expected-warning@-1{{Static local variable 'a' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
431 a = provide_obj();
432 }
433
434 } // namespace local_refcountable_checkable_object
435
436 namespace local_var_in_recursive_function {
437
438 struct TreeNode {
439 Ref<TreeNode> create() { return Ref(*new TreeNode); }
440
441 void ref() const { ++refCount; }
442 void deref() const {
443 if (!--refCount)
444 delete this;
445 }
446
447 int recursiveCost();
448 int recursiveWeight();
449 int weight();
450
451 int cost { 0 };
452 mutable unsigned refCount { 0 };
453 TreeNode* nextSibling { nullptr };
454 TreeNode* firstChild { nullptr };
455 };
456
457 int TreeNode::recursiveCost() {
458 // no warnings
459 unsigned totalCost = cost;
460 for (TreeNode* node = firstChild; node; node = node->nextSibling)
461 totalCost += recursiveCost();
462 return totalCost;
463 }
464
465 int TreeNode::recursiveWeight() {
466 unsigned totalCost = weight();
467 for (TreeNode* node = firstChild; node; node = node->nextSibling)
468 // expected-warning@-1{{Local variable 'node' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
469 totalCost += recursiveWeight();
470 return totalCost;
471 }
472
473 } // namespace local_var_in_recursive_function
474
475 namespace local_var_for_singleton {
476 RefCountable *singleton();
477 RefCountable *otherSingleton();
478 void foo() {
479 RefCountable* bar = singleton();
480 RefCountable* baz = otherSingleton();
481 }
482 }
483
484 namespace virtual_function {
485 struct SomeObject {
486 virtual RefCountable* provide() { return nullptr; }
487 virtual RefCountable* operator&() { return nullptr; }
488 };
489 void foo(SomeObject* obj) {
490 auto* bar = obj->provide();
491 // expected-warning@-1{{Local variable 'bar' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
492 auto* baz = &*obj;
493 // expected-warning@-1{{Local variable 'baz' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
494 }
495 }
496
497 namespace vardecl_in_if_condition {
498 RefCountable* provide();
499
500 RefCountable* get() {
501 if (auto* obj = provide())
502 return obj; // no warning
503 return nullptr;
504 }
505
506 RefCountable* get_non_trivial_then() {
507 if (auto* obj = provide()) // expected-warning{{Local variable 'obj' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
508 return obj->next();
509 return nullptr;
510 }
511
512 RefCountable* get_non_trivial_else() {
513 if (auto* obj = provide())
514 return obj;
515 else
516 return provide()->next();
517 return nullptr;
518 }
519
520 RefCountable& get_ref() {
521 if (auto* obj = provide())
522 return *obj; // no warning
523 static Ref<RefCountable> empty = RefCountable::create();
524 return empty.get();
525 }
526
527 RefCountable* get_non_trivial_condition() {
528 if (auto* obj = provide(); obj && obj->next())
529 return obj; // expected-warning@-1{{Local variable 'obj' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
530 return nullptr;
531 }
532
533 RefCountable* get_non_trivial_else2() {
534 if (auto* obj = provide(); !obj) // expected-warning{{Local variable 'obj' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
535 return nullptr;
536 else
537 return obj->next();
538 }
539
540 }