xref: /llvm-project/clang/docs/analyzer/checkers.rst (revision 56c5a6ba836065a6e3be9d04e2c64aa8a758a3f4)
1==================
2Available Checkers
3==================
4
5The analyzer performs checks that are categorized into families or "checkers".
6
7The default set of checkers covers a variety of checks targeted at finding security and API usage bugs,
8dead code, and other logic errors. See the :ref:`default-checkers` checkers list below.
9
10In addition to these, the analyzer contains a number of :ref:`alpha-checkers` (aka *alpha* checkers).
11These checkers are under development and are switched off by default. They may crash or emit a higher number of false positives.
12
13The :ref:`debug-checkers` package contains checkers for analyzer developers for debugging purposes.
14
15.. contents:: Table of Contents
16   :depth: 4
17
18
19.. _default-checkers:
20
21Default Checkers
22----------------
23
24.. _core-checkers:
25
26core
27^^^^
28Models core language features and contains general-purpose checkers such as division by zero,
29null pointer dereference, usage of uninitialized values, etc.
30*These checkers must be always switched on as other checker rely on them.*
31
32.. _core-BitwiseShift:
33
34core.BitwiseShift (C, C++)
35""""""""""""""""""""""""""
36
37Finds undefined behavior caused by the bitwise left- and right-shift operator
38operating on integer types.
39
40By default, this checker only reports situations when the right operand is
41either negative or larger than the bit width of the type of the left operand;
42these are logically unsound.
43
44Moreover, if the pedantic mode is activated by
45``-analyzer-config core.BitwiseShift:Pedantic=true``, then this checker also
46reports situations where the _left_ operand of a shift operator is negative or
47overflow occurs during the right shift of a signed value. (Most compilers
48handle these predictably, but the C standard and the C++ standards before C++20
49say that they're undefined behavior. In the C++20 standard these constructs are
50well-defined, so activating pedantic mode in C++20 has no effect.)
51
52**Examples**
53
54.. code-block:: cpp
55
56 static_assert(sizeof(int) == 4, "assuming 32-bit int")
57
58 void basic_examples(int a, int b) {
59   if (b < 0) {
60     b = a << b; // warn: right operand is negative in left shift
61   } else if (b >= 32) {
62     b = a >> b; // warn: right shift overflows the capacity of 'int'
63   }
64 }
65
66 int pedantic_examples(int a, int b) {
67   if (a < 0) {
68     return a >> b; // warn: left operand is negative in right shift
69   }
70   a = 1000u << 31; // OK, overflow of unsigned value is well-defined, a == 0
71   if (b > 10) {
72     a = b << 31; // this is undefined before C++20, but the checker doesn't
73                  // warn because it doesn't know the exact value of b
74   }
75   return 1000 << 31; // warn: this overflows the capacity of 'int'
76 }
77
78**Solution**
79
80Ensure the shift operands are in proper range before shifting.
81
82.. _core-CallAndMessage:
83
84core.CallAndMessage (C, C++, ObjC)
85""""""""""""""""""""""""""""""""""
86 Check for logical errors for function calls and Objective-C message expressions (e.g., uninitialized arguments, null function pointers).
87
88.. literalinclude:: checkers/callandmessage_example.c
89    :language: objc
90
91.. _core-DivideZero:
92
93core.DivideZero (C, C++, ObjC)
94""""""""""""""""""""""""""""""
95 Check for division by zero.
96
97.. literalinclude:: checkers/dividezero_example.c
98    :language: c
99
100.. _core-NonNullParamChecker:
101
102core.NonNullParamChecker (C, C++, ObjC)
103"""""""""""""""""""""""""""""""""""""""
104Check for null pointers passed as arguments to a function whose arguments are references or marked with the 'nonnull' attribute.
105
106.. code-block:: cpp
107
108 int f(int *p) __attribute__((nonnull));
109
110 void test(int *p) {
111   if (!p)
112     f(p); // warn
113 }
114
115.. _core-NullDereference:
116
117core.NullDereference (C, C++, ObjC)
118"""""""""""""""""""""""""""""""""""
119Check for dereferences of null pointers.
120
121This checker specifically does
122not report null pointer dereferences for x86 and x86-64 targets when the
123address space is 256 (x86 GS Segment), 257 (x86 FS Segment), or 258 (x86 SS
124segment). See `X86/X86-64 Language Extensions
125<https://clang.llvm.org/docs/LanguageExtensions.html#memory-references-to-specified-segments>`__
126for reference.
127
128The ``SuppressAddressSpaces`` option suppresses
129warnings for null dereferences of all pointers with address spaces. You can
130disable this behavior with the option
131``-analyzer-config core.NullDereference:SuppressAddressSpaces=false``.
132*Defaults to true*.
133
134.. code-block:: objc
135
136 // C
137 void test(int *p) {
138   if (p)
139     return;
140
141   int x = p[0]; // warn
142 }
143
144 // C
145 void test(int *p) {
146   if (!p)
147     *p = 0; // warn
148 }
149
150 // C++
151 class C {
152 public:
153   int x;
154 };
155
156 void test() {
157   C *pc = 0;
158   int k = pc->x; // warn
159 }
160
161 // Objective-C
162 @interface MyClass {
163 @public
164   int x;
165 }
166 @end
167
168 void test() {
169   MyClass *obj = 0;
170   obj->x = 1; // warn
171 }
172
173.. _core-StackAddressEscape:
174
175core.StackAddressEscape (C)
176"""""""""""""""""""""""""""
177Check that addresses to stack memory do not escape the function.
178
179.. code-block:: c
180
181 char const *p;
182
183 void test() {
184   char const str[] = "string";
185   p = str; // warn
186 }
187
188 void* test() {
189    return __builtin_alloca(12); // warn
190 }
191
192 void test() {
193   static int *x;
194   int y;
195   x = &y; // warn
196 }
197
198
199.. _core-UndefinedBinaryOperatorResult:
200
201core.UndefinedBinaryOperatorResult (C)
202""""""""""""""""""""""""""""""""""""""
203Check for undefined results of binary operators.
204
205.. code-block:: c
206
207 void test() {
208   int x;
209   int y = x + 1; // warn: left operand is garbage
210 }
211
212.. _core-VLASize:
213
214core.VLASize (C)
215""""""""""""""""
216Check for declarations of Variable Length Arrays (VLA) of undefined, zero or negative
217size.
218
219.. code-block:: c
220
221 void test() {
222   int x;
223   int vla1[x]; // warn: garbage as size
224 }
225
226 void test() {
227   int x = 0;
228   int vla2[x]; // warn: zero size
229 }
230
231
232The checker also gives warning if the `TaintPropagation` checker is switched on
233and an unbound, attacker controlled (tainted) value is used to define
234the size of the VLA.
235
236.. code-block:: c
237
238 void taintedVLA(void) {
239   int x;
240   scanf("%d", &x);
241   int vla[x]; // Declared variable-length array (VLA) has tainted (attacker controlled) size, that can be 0 or negative
242 }
243
244 void taintedVerfieidVLA(void) {
245   int x;
246   scanf("%d", &x);
247   if (x<1)
248     return;
249   int vla[x]; // no-warning. The analyzer can prove that x must be positive.
250 }
251
252
253.. _core-uninitialized-ArraySubscript:
254
255core.uninitialized.ArraySubscript (C)
256"""""""""""""""""""""""""""""""""""""
257Check for uninitialized values used as array subscripts.
258
259.. code-block:: c
260
261 void test() {
262   int i, a[10];
263   int x = a[i]; // warn: array subscript is undefined
264 }
265
266.. _core-uninitialized-Assign:
267
268core.uninitialized.Assign (C)
269"""""""""""""""""""""""""""""
270Check for assigning uninitialized values.
271
272.. code-block:: c
273
274 void test() {
275   int x;
276   x |= 1; // warn: left expression is uninitialized
277 }
278
279.. _core-uninitialized-Branch:
280
281core.uninitialized.Branch (C)
282"""""""""""""""""""""""""""""
283Check for uninitialized values used as branch conditions.
284
285.. code-block:: c
286
287 void test() {
288   int x;
289   if (x) // warn
290     return;
291 }
292
293.. _core-uninitialized-CapturedBlockVariable:
294
295core.uninitialized.CapturedBlockVariable (C)
296""""""""""""""""""""""""""""""""""""""""""""
297Check for blocks that capture uninitialized values.
298
299.. code-block:: c
300
301 void test() {
302   int x;
303   ^{ int y = x; }(); // warn
304 }
305
306.. _core-uninitialized-UndefReturn:
307
308core.uninitialized.UndefReturn (C)
309""""""""""""""""""""""""""""""""""
310Check for uninitialized values being returned to the caller.
311
312.. code-block:: c
313
314 int test() {
315   int x;
316   return x; // warn
317 }
318
319.. _core-uninitialized-NewArraySize:
320
321core.uninitialized.NewArraySize (C++)
322"""""""""""""""""""""""""""""""""""""
323
324Check if the element count in new[] is garbage or undefined.
325
326.. code-block:: cpp
327
328  void test() {
329    int n;
330    int *arr = new int[n]; // warn: Element count in new[] is a garbage value
331    delete[] arr;
332  }
333
334
335.. _cplusplus-checkers:
336
337
338cplusplus
339^^^^^^^^^
340
341C++ Checkers.
342
343.. _cplusplus-ArrayDelete:
344
345cplusplus.ArrayDelete (C++)
346"""""""""""""""""""""""""""
347
348Reports destructions of arrays of polymorphic objects that are destructed as
349their base class. If the dynamic type of the array is different from its static
350type, calling `delete[]` is undefined.
351
352This checker corresponds to the SEI CERT rule `EXP51-CPP: Do not delete an array through a pointer of the incorrect type <https://wiki.sei.cmu.edu/confluence/display/cplusplus/EXP51-CPP.+Do+not+delete+an+array+through+a+pointer+of+the+incorrect+type>`_.
353
354.. code-block:: cpp
355
356 class Base {
357 public:
358   virtual ~Base() {}
359 };
360 class Derived : public Base {};
361
362 Base *create() {
363   Base *x = new Derived[10]; // note: Casting from 'Derived' to 'Base' here
364   return x;
365 }
366
367 void foo() {
368   Base *x = create();
369   delete[] x; // warn: Deleting an array of 'Derived' objects as their base class 'Base' is undefined
370 }
371
372**Limitations**
373
374The checker does not emit note tags when casting to and from reference types,
375even though the pointer values are tracked across references.
376
377.. code-block:: cpp
378
379 void foo() {
380   Derived *d = new Derived[10];
381   Derived &dref = *d;
382
383   Base &bref = static_cast<Base&>(dref); // no note
384   Base *b = &bref;
385   delete[] b; // warn: Deleting an array of 'Derived' objects as their base class 'Base' is undefined
386 }
387
388.. _cplusplus-InnerPointer:
389
390cplusplus.InnerPointer (C++)
391""""""""""""""""""""""""""""
392Check for inner pointers of C++ containers used after re/deallocation.
393
394Many container methods in the C++ standard library are known to invalidate
395"references" (including actual references, iterators and raw pointers) to
396elements of the container. Using such references after they are invalidated
397causes undefined behavior, which is a common source of memory errors in C++ that
398this checker is capable of finding.
399
400The checker is currently limited to ``std::string`` objects and doesn't
401recognize some of the more sophisticated approaches to passing unowned pointers
402around, such as ``std::string_view``.
403
404.. code-block:: cpp
405
406 void deref_after_assignment() {
407   std::string s = "llvm";
408   const char *c = s.data(); // note: pointer to inner buffer of 'std::string' obtained here
409   s = "clang"; // note: inner buffer of 'std::string' reallocated by call to 'operator='
410   consume(c); // warn: inner pointer of container used after re/deallocation
411 }
412
413 const char *return_temp(int x) {
414   return std::to_string(x).c_str(); // warn: inner pointer of container used after re/deallocation
415   // note: pointer to inner buffer of 'std::string' obtained here
416   // note: inner buffer of 'std::string' deallocated by call to destructor
417 }
418
419.. _cplusplus-Move:
420
421cplusplus.Move (C++)
422""""""""""""""""""""
423Find use-after-move bugs in C++. This includes method calls on moved-from
424objects, assignment of a moved-from object, and repeated move of a moved-from
425object.
426
427.. code-block:: cpp
428
429 struct A {
430   void foo() {}
431 };
432
433 void f1() {
434   A a;
435   A b = std::move(a); // note: 'a' became 'moved-from' here
436   a.foo();            // warn: method call on a 'moved-from' object 'a'
437 }
438
439 void f2() {
440   A a;
441   A b = std::move(a);
442   A c(std::move(a)); // warn: move of an already moved-from object
443 }
444
445 void f3() {
446   A a;
447   A b = std::move(a);
448   b = a; // warn: copy of moved-from object
449 }
450
451The checker option ``WarnOn`` controls on what objects the use-after-move is
452checked:
453
454* The most strict value is ``KnownsOnly``, in this mode only objects are
455  checked whose type is known to be move-unsafe. These include most STL objects
456  (but excluding move-safe ones) and smart pointers.
457* With option value ``KnownsAndLocals`` local variables (of any type) are
458  additionally checked. The idea behind this is that local variables are
459  usually not tempting to be re-used so an use after move is more likely a bug
460  than with member variables.
461* With option value ``All`` any use-after move condition is checked on all
462  kinds of variables, excluding global variables and known move-safe cases.
463
464Default value is ``KnownsAndLocals``.
465
466Calls of methods named ``empty()`` or ``isEmpty()`` are allowed on moved-from
467objects because these methods are considered as move-safe. Functions called
468``reset()``, ``destroy()``, ``clear()``, ``assign``, ``resize``,  ``shrink`` are
469treated as state-reset functions and are allowed on moved-from objects, these
470make the object valid again. This applies to any type of object (not only STL
471ones).
472
473.. _cplusplus-NewDelete:
474
475cplusplus.NewDelete (C++)
476"""""""""""""""""""""""""
477Check for double-free and use-after-free problems. Traces memory managed by new/delete.
478
479Custom allocation/deallocation functions can be defined using
480:ref:`ownership attributes<analyzer-ownership-attrs>`.
481
482.. literalinclude:: checkers/newdelete_example.cpp
483    :language: cpp
484
485.. _cplusplus-NewDeleteLeaks:
486
487cplusplus.NewDeleteLeaks (C++)
488""""""""""""""""""""""""""""""
489Check for memory leaks. Traces memory managed by new/delete.
490
491Custom allocation/deallocation functions can be defined using
492:ref:`ownership attributes<analyzer-ownership-attrs>`.
493
494.. code-block:: cpp
495
496 void test() {
497   int *p = new int;
498 } // warn
499
500.. _cplusplus-PlacementNew:
501
502cplusplus.PlacementNew (C++)
503""""""""""""""""""""""""""""
504Check if default placement new is provided with pointers to sufficient storage capacity.
505
506.. code-block:: cpp
507
508 #include <new>
509
510 void f() {
511   short s;
512   long *lp = ::new (&s) long; // warn
513 }
514
515.. _cplusplus-SelfAssignment:
516
517cplusplus.SelfAssignment (C++)
518""""""""""""""""""""""""""""""
519Checks C++ copy and move assignment operators for self assignment.
520
521.. _cplusplus-StringChecker:
522
523cplusplus.StringChecker (C++)
524"""""""""""""""""""""""""""""
525Checks std::string operations.
526
527Checks if the cstring pointer from which the ``std::string`` object is
528constructed is ``NULL`` or not.
529If the checker cannot reason about the nullness of the pointer it will assume
530that it was non-null to satisfy the precondition of the constructor.
531
532This checker is capable of checking the `SEI CERT C++ coding rule STR51-CPP.
533Do not attempt to create a std::string from a null pointer
534<https://wiki.sei.cmu.edu/confluence/x/E3s-BQ>`__.
535
536.. code-block:: cpp
537
538 #include <string>
539
540 void f(const char *p) {
541   if (!p) {
542     std::string msg(p); // warn: The parameter must not be null
543   }
544 }
545
546.. _deadcode-checkers:
547
548deadcode
549^^^^^^^^
550
551Dead Code Checkers.
552
553.. _deadcode-DeadStores:
554
555deadcode.DeadStores (C)
556"""""""""""""""""""""""
557Check for values stored to variables that are never read afterwards.
558
559.. code-block:: c
560
561 void test() {
562   int x;
563   x = 1; // warn
564 }
565
566The ``WarnForDeadNestedAssignments`` option enables the checker to emit
567warnings for nested dead assignments. You can disable with the
568``-analyzer-config deadcode.DeadStores:WarnForDeadNestedAssignments=false``.
569*Defaults to true*.
570
571Would warn for this e.g.:
572if ((y = make_int())) {
573}
574
575.. _nullability-checkers:
576
577nullability
578^^^^^^^^^^^
579
580Checkers (mostly Objective C) that warn for null pointer passing and dereferencing errors.
581
582.. _nullability-NullPassedToNonnull:
583
584nullability.NullPassedToNonnull (ObjC)
585""""""""""""""""""""""""""""""""""""""
586Warns when a null pointer is passed to a pointer which has a _Nonnull type.
587
588.. code-block:: objc
589
590 if (name != nil)
591   return;
592 // Warning: nil passed to a callee that requires a non-null 1st parameter
593 NSString *greeting = [@"Hello " stringByAppendingString:name];
594
595.. _nullability-NullReturnedFromNonnull:
596
597nullability.NullReturnedFromNonnull (C, C++, ObjC)
598""""""""""""""""""""""""""""""""""""""""""""""""""
599Warns when a null pointer is returned from a function that has _Nonnull return type.
600
601.. code-block:: objc
602
603 - (nonnull id)firstChild {
604   id result = nil;
605   if ([_children count] > 0)
606     result = _children[0];
607
608   // Warning: nil returned from a method that is expected
609   // to return a non-null value
610   return result;
611 }
612
613Warns when a null pointer is returned from a function annotated with ``__attribute__((returns_nonnull))``
614
615.. code-block:: cpp
616
617 int global;
618 __attribute__((returns_nonnull)) void* getPtr(void* p);
619
620 void* getPtr(void* p) {
621   if (p) { // forgot to negate the condition
622     return &global;
623   }
624   // Warning: nullptr returned from a function that is expected
625   // to return a non-null value
626   return p;
627 }
628
629.. _nullability-NullableDereferenced:
630
631nullability.NullableDereferenced (ObjC)
632"""""""""""""""""""""""""""""""""""""""
633Warns when a nullable pointer is dereferenced.
634
635.. code-block:: objc
636
637 struct LinkedList {
638   int data;
639   struct LinkedList *next;
640 };
641
642 struct LinkedList * _Nullable getNext(struct LinkedList *l);
643
644 void updateNextData(struct LinkedList *list, int newData) {
645   struct LinkedList *next = getNext(list);
646   // Warning: Nullable pointer is dereferenced
647   next->data = 7;
648 }
649
650.. _nullability-NullablePassedToNonnull:
651
652nullability.NullablePassedToNonnull (ObjC)
653""""""""""""""""""""""""""""""""""""""""""
654Warns when a nullable pointer is passed to a pointer which has a _Nonnull type.
655
656.. code-block:: objc
657
658 typedef struct Dummy { int val; } Dummy;
659 Dummy *_Nullable returnsNullable();
660 void takesNonnull(Dummy *_Nonnull);
661
662 void test() {
663   Dummy *p = returnsNullable();
664   takesNonnull(p); // warn
665 }
666
667.. _nullability-NullableReturnedFromNonnull:
668
669nullability.NullableReturnedFromNonnull (ObjC)
670""""""""""""""""""""""""""""""""""""""""""""""
671Warns when a nullable pointer is returned from a function that has _Nonnull return type.
672
673.. _optin-checkers:
674
675optin
676^^^^^
677
678Checkers for portability, performance, optional security and coding style specific rules.
679
680.. _optin-core-EnumCastOutOfRange:
681
682optin.core.EnumCastOutOfRange (C, C++)
683""""""""""""""""""""""""""""""""""""""
684Check for integer to enumeration casts that would produce a value with no
685corresponding enumerator. This is not necessarily undefined behavior, but can
686lead to nasty surprises, so projects may decide to use a coding standard that
687disallows these "unusual" conversions.
688
689Note that no warnings are produced when the enum type (e.g. `std::byte`) has no
690enumerators at all.
691
692.. code-block:: cpp
693
694 enum WidgetKind { A=1, B, C, X=99 };
695
696 void foo() {
697   WidgetKind c = static_cast<WidgetKind>(3);  // OK
698   WidgetKind x = static_cast<WidgetKind>(99); // OK
699   WidgetKind d = static_cast<WidgetKind>(4);  // warn
700 }
701
702**Limitations**
703
704This checker does not accept the coding pattern where an enum type is used to
705store combinations of flag values:
706
707.. code-block:: cpp
708
709 enum AnimalFlags
710 {
711     HasClaws   = 1,
712     CanFly     = 2,
713     EatsFish   = 4,
714     Endangered = 8
715 };
716
717 AnimalFlags operator|(AnimalFlags a, AnimalFlags b)
718 {
719     return static_cast<AnimalFlags>(static_cast<int>(a) | static_cast<int>(b));
720 }
721
722 auto flags = HasClaws | CanFly;
723
724Projects that use this pattern should not enable this optin checker.
725
726.. _optin-cplusplus-UninitializedObject:
727
728optin.cplusplus.UninitializedObject (C++)
729"""""""""""""""""""""""""""""""""""""""""
730
731This checker reports uninitialized fields in objects created after a constructor
732call. It doesn't only find direct uninitialized fields, but rather makes a deep
733inspection of the object, analyzing all of its fields' subfields.
734The checker regards inherited fields as direct fields, so one will receive
735warnings for uninitialized inherited data members as well.
736
737.. code-block:: cpp
738
739 // With Pedantic and CheckPointeeInitialization set to true
740
741 struct A {
742   struct B {
743     int x; // note: uninitialized field 'this->b.x'
744     // note: uninitialized field 'this->bptr->x'
745     int y; // note: uninitialized field 'this->b.y'
746     // note: uninitialized field 'this->bptr->y'
747   };
748   int *iptr; // note: uninitialized pointer 'this->iptr'
749   B b;
750   B *bptr;
751   char *cptr; // note: uninitialized pointee 'this->cptr'
752
753   A (B *bptr, char *cptr) : bptr(bptr), cptr(cptr) {}
754 };
755
756 void f() {
757   A::B b;
758   char c;
759   A a(&b, &c); // warning: 6 uninitialized fields
760  //          after the constructor call
761 }
762
763 // With Pedantic set to false and
764 // CheckPointeeInitialization set to true
765 // (every field is uninitialized)
766
767 struct A {
768   struct B {
769     int x;
770     int y;
771   };
772   int *iptr;
773   B b;
774   B *bptr;
775   char *cptr;
776
777   A (B *bptr, char *cptr) : bptr(bptr), cptr(cptr) {}
778 };
779
780 void f() {
781   A::B b;
782   char c;
783   A a(&b, &c); // no warning
784 }
785
786 // With Pedantic set to true and
787 // CheckPointeeInitialization set to false
788 // (pointees are regarded as initialized)
789
790 struct A {
791   struct B {
792     int x; // note: uninitialized field 'this->b.x'
793     int y; // note: uninitialized field 'this->b.y'
794   };
795   int *iptr; // note: uninitialized pointer 'this->iptr'
796   B b;
797   B *bptr;
798   char *cptr;
799
800   A (B *bptr, char *cptr) : bptr(bptr), cptr(cptr) {}
801 };
802
803 void f() {
804   A::B b;
805   char c;
806   A a(&b, &c); // warning: 3 uninitialized fields
807  //          after the constructor call
808 }
809
810
811**Options**
812
813This checker has several options which can be set from command line (e.g.
814``-analyzer-config optin.cplusplus.UninitializedObject:Pedantic=true``):
815
816* ``Pedantic`` (boolean). If to false, the checker won't emit warnings for
817  objects that don't have at least one initialized field. Defaults to false.
818
819* ``NotesAsWarnings``  (boolean). If set to true, the checker will emit a
820  warning for each uninitialized field, as opposed to emitting one warning per
821  constructor call, and listing the uninitialized fields that belongs to it in
822  notes. *Defaults to false*.
823
824* ``CheckPointeeInitialization`` (boolean). If set to false, the checker will
825  not analyze the pointee of pointer/reference fields, and will only check
826  whether the object itself is initialized. *Defaults to false*.
827
828* ``IgnoreRecordsWithField`` (string). If supplied, the checker will not analyze
829  structures that have a field with a name or type name that matches  the given
830  pattern. *Defaults to ""*.
831
832.. _optin-cplusplus-VirtualCall:
833
834optin.cplusplus.VirtualCall (C++)
835"""""""""""""""""""""""""""""""""
836Check virtual function calls during construction or destruction.
837
838.. code-block:: cpp
839
840 class A {
841 public:
842   A() {
843     f(); // warn
844   }
845   virtual void f();
846 };
847
848 class A {
849 public:
850   ~A() {
851     this->f(); // warn
852   }
853   virtual void f();
854 };
855
856.. _optin-mpi-MPI-Checker:
857
858optin.mpi.MPI-Checker (C)
859"""""""""""""""""""""""""
860Checks MPI code.
861
862.. code-block:: c
863
864 void test() {
865   double buf = 0;
866   MPI_Request sendReq1;
867   MPI_Ireduce(MPI_IN_PLACE, &buf, 1, MPI_DOUBLE, MPI_SUM,
868       0, MPI_COMM_WORLD, &sendReq1);
869 } // warn: request 'sendReq1' has no matching wait.
870
871 void test() {
872   double buf = 0;
873   MPI_Request sendReq;
874   MPI_Isend(&buf, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &sendReq);
875   MPI_Irecv(&buf, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &sendReq); // warn
876   MPI_Isend(&buf, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &sendReq); // warn
877   MPI_Wait(&sendReq, MPI_STATUS_IGNORE);
878 }
879
880 void missingNonBlocking() {
881   int rank = 0;
882   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
883   MPI_Request sendReq1[10][10][10];
884   MPI_Wait(&sendReq1[1][7][9], MPI_STATUS_IGNORE); // warn
885 }
886
887.. _optin-osx-cocoa-localizability-EmptyLocalizationContextChecker:
888
889optin.osx.cocoa.localizability.EmptyLocalizationContextChecker (ObjC)
890"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
891Check that NSLocalizedString macros include a comment for context.
892
893.. code-block:: objc
894
895 - (void)test {
896   NSString *string = NSLocalizedString(@"LocalizedString", nil); // warn
897   NSString *string2 = NSLocalizedString(@"LocalizedString", @" "); // warn
898   NSString *string3 = NSLocalizedStringWithDefaultValue(
899     @"LocalizedString", nil, [[NSBundle alloc] init], nil,@""); // warn
900 }
901
902.. _optin-osx-cocoa-localizability-NonLocalizedStringChecker:
903
904optin.osx.cocoa.localizability.NonLocalizedStringChecker (ObjC)
905"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
906Warns about uses of non-localized NSStrings passed to UI methods expecting localized NSStrings.
907
908.. code-block:: objc
909
910 NSString *alarmText =
911   NSLocalizedString(@"Enabled", @"Indicates alarm is turned on");
912 if (!isEnabled) {
913   alarmText = @"Disabled";
914 }
915 UILabel *alarmStateLabel = [[UILabel alloc] init];
916
917 // Warning: User-facing text should use localized string macro
918 [alarmStateLabel setText:alarmText];
919
920.. _optin-performance-GCDAntipattern:
921
922optin.performance.GCDAntipattern
923""""""""""""""""""""""""""""""""
924Check for performance anti-patterns when using Grand Central Dispatch.
925
926.. _optin-performance-Padding:
927
928optin.performance.Padding (C, C++, ObjC)
929""""""""""""""""""""""""""""""""""""""""
930Check for excessively padded structs.
931
932This checker detects structs with excessive padding, which can lead to wasted
933memory thus decreased performance by reducing the effectiveness of the
934processor cache. Padding bytes are added by compilers to align data accesses
935as some processors require data to be aligned to certain boundaries. On others,
936unaligned data access are possible, but impose significantly larger latencies.
937
938To avoid padding bytes, the fields of a struct should be ordered by decreasing
939by alignment. Usually, its easier to think of the ``sizeof`` of the fields, and
940ordering the fields by ``sizeof`` would usually also lead to the same optimal
941layout.
942
943In rare cases, one can use the ``#pragma pack(1)`` directive to enforce a packed
944layout too, but it can significantly increase the access times, so reordering the
945fields is usually a better solution.
946
947
948.. code-block:: cpp
949
950 // warn: Excessive padding in 'struct NonOptimal' (35 padding bytes, where 3 is optimal)
951 struct NonOptimal {
952   char c1;
953   // 7 bytes of padding
954   std::int64_t big1; // 8 bytes
955   char c2;
956   // 7 bytes of padding
957   std::int64_t big2; // 8 bytes
958   char c3;
959   // 7 bytes of padding
960   std::int64_t big3; // 8 bytes
961   char c4;
962   // 7 bytes of padding
963   std::int64_t big4; // 8 bytes
964   char c5;
965   // 7 bytes of padding
966 };
967 static_assert(sizeof(NonOptimal) == 4*8+5+5*7);
968
969 // no-warning: The fields are nicely aligned to have the minimal amount of padding bytes.
970 struct Optimal {
971   std::int64_t big1; // 8 bytes
972   std::int64_t big2; // 8 bytes
973   std::int64_t big3; // 8 bytes
974   std::int64_t big4; // 8 bytes
975   char c1;
976   char c2;
977   char c3;
978   char c4;
979   char c5;
980   // 3 bytes of padding
981 };
982 static_assert(sizeof(Optimal) == 4*8+5+3);
983
984 // no-warning: Bit packing representation is also accepted by this checker, but
985 // it can significantly increase access times, so prefer reordering the fields.
986 #pragma pack(1)
987 struct BitPacked {
988   char c1;
989   std::int64_t big1; // 8 bytes
990   char c2;
991   std::int64_t big2; // 8 bytes
992   char c3;
993   std::int64_t big3; // 8 bytes
994   char c4;
995   std::int64_t big4; // 8 bytes
996   char c5;
997 };
998 static_assert(sizeof(BitPacked) == 4*8+5);
999
1000The ``AllowedPad`` option can be used to specify a threshold for the number
1001padding bytes raising the warning. If the number of padding bytes of the struct
1002and the optimal number of padding bytes differ by more than the threshold value,
1003a warning will be raised.
1004
1005By default, the ``AllowedPad`` threshold is 24 bytes.
1006
1007To override this threshold to e.g. 4 bytes, use the
1008``-analyzer-config optin.performance.Padding:AllowedPad=4`` option.
1009
1010
1011.. _optin-portability-UnixAPI:
1012
1013optin.portability.UnixAPI
1014"""""""""""""""""""""""""
1015Finds implementation-defined behavior in UNIX/Posix functions.
1016
1017
1018optin.taint
1019^^^^^^^^^^^
1020
1021Checkers implementing
1022`taint analysis <https://en.wikipedia.org/wiki/Taint_checking>`_.
1023
1024.. _optin-taint-GenericTaint:
1025
1026optin.taint.GenericTaint (C, C++)
1027"""""""""""""""""""""""""""""""""
1028
1029Taint analysis identifies potential security vulnerabilities where the
1030attacker can inject malicious data to the program to execute an attack
1031(privilege escalation, command injection, SQL injection etc.).
1032
1033The malicious data is injected at the taint source (e.g. ``getenv()`` call)
1034which is then propagated through function calls and being used as arguments of
1035sensitive operations, also called as taint sinks (e.g. ``system()`` call).
1036
1037One can defend against this type of vulnerability by always checking and
1038sanitizing the potentially malicious, untrusted user input.
1039
1040The goal of the checker is to discover and show to the user these potential
1041taint source-sink pairs and the propagation call chain.
1042
1043The most notable examples of taint sources are:
1044
1045  - data from network
1046  - files or standard input
1047  - environment variables
1048  - data from databases
1049
1050Let us examine a practical example of a Command Injection attack.
1051
1052.. code-block:: c
1053
1054  // Command Injection Vulnerability Example
1055  int main(int argc, char** argv) {
1056    char cmd[2048] = "/bin/cat ";
1057    char filename[1024];
1058    printf("Filename:");
1059    scanf (" %1023[^\n]", filename); // The attacker can inject a shell escape here
1060    strcat(cmd, filename);
1061    system(cmd); // Warning: Untrusted data is passed to a system call
1062  }
1063
1064The program prints the content of any user specified file.
1065Unfortunately the attacker can execute arbitrary commands
1066with shell escapes. For example with the following input the `ls` command is also
1067executed after the contents of `/etc/shadow` is printed.
1068`Input: /etc/shadow ; ls /`
1069
1070The analysis implemented in this checker points out this problem.
1071
1072One can protect against such attack by for example checking if the provided
1073input refers to a valid file and removing any invalid user input.
1074
1075.. code-block:: c
1076
1077  // No vulnerability anymore, but we still get the warning
1078  void sanitizeFileName(char* filename){
1079    if (access(filename,F_OK)){// Verifying user input
1080      printf("File does not exist\n");
1081      filename[0]='\0';
1082      }
1083  }
1084  int main(int argc, char** argv) {
1085    char cmd[2048] = "/bin/cat ";
1086    char filename[1024];
1087    printf("Filename:");
1088    scanf (" %1023[^\n]", filename); // The attacker can inject a shell escape here
1089    sanitizeFileName(filename);// filename is safe after this point
1090    if (!filename[0])
1091      return -1;
1092    strcat(cmd, filename);
1093    system(cmd); // Superfluous Warning: Untrusted data is passed to a system call
1094  }
1095
1096Unfortunately, the checker cannot discover automatically that the programmer
1097have performed data sanitation, so it still emits the warning.
1098
1099One can get rid of this superfluous warning by telling by specifying the
1100sanitation functions in the taint configuration file (see
1101:doc:`user-docs/TaintAnalysisConfiguration`).
1102
1103.. code-block:: YAML
1104
1105  Filters:
1106  - Name: sanitizeFileName
1107    Args: [0]
1108
1109The clang invocation to pass the configuration file location:
1110
1111.. code-block:: bash
1112
1113  clang  --analyze -Xclang -analyzer-config  -Xclang optin.taint.TaintPropagation:Config=`pwd`/taint_config.yml ...
1114
1115If you are validating your inputs instead of sanitizing them, or don't want to
1116mention each sanitizing function in our configuration,
1117you can use a more generic approach.
1118
1119Introduce a generic no-op `csa_mark_sanitized(..)` function to
1120tell the Clang Static Analyzer
1121that the variable is safe to be used on that analysis path.
1122
1123.. code-block:: c
1124
1125  // Marking sanitized variables safe.
1126  // No vulnerability anymore, no warning.
1127
1128  // User csa_mark_sanitize function is for the analyzer only
1129  #ifdef __clang_analyzer__
1130    void csa_mark_sanitized(const void *);
1131  #endif
1132
1133  int main(int argc, char** argv) {
1134    char cmd[2048] = "/bin/cat ";
1135    char filename[1024];
1136    printf("Filename:");
1137    scanf (" %1023[^\n]", filename);
1138    if (access(filename,F_OK)){// Verifying user input
1139      printf("File does not exist\n");
1140      return -1;
1141    }
1142    #ifdef __clang_analyzer__
1143      csa_mark_sanitized(filename); // Indicating to CSA that filename variable is safe to be used after this point
1144    #endif
1145    strcat(cmd, filename);
1146    system(cmd); // No warning
1147  }
1148
1149Similarly to the previous example, you need to
1150define a `Filter` function in a `YAML` configuration file
1151and add the `csa_mark_sanitized` function.
1152
1153.. code-block:: YAML
1154
1155  Filters:
1156  - Name: csa_mark_sanitized
1157    Args: [0]
1158
1159Then calling `csa_mark_sanitized(X)` will tell the analyzer that `X` is safe to
1160be used after this point, because its contents are verified. It is the
1161responsibility of the programmer to ensure that this verification was indeed
1162correct. Please note that `csa_mark_sanitized` function is only declared and
1163used during Clang Static Analysis and skipped in (production) builds.
1164
1165Further examples of injection vulnerabilities this checker can find.
1166
1167.. code-block:: c
1168
1169  void test() {
1170    char x = getchar(); // 'x' marked as tainted
1171    system(&x); // warn: untrusted data is passed to a system call
1172  }
1173
1174  // note: compiler internally checks if the second param to
1175  // sprintf is a string literal or not.
1176  // Use -Wno-format-security to suppress compiler warning.
1177  void test() {
1178    char s[10], buf[10];
1179    fscanf(stdin, "%s", s); // 's' marked as tainted
1180
1181    sprintf(buf, s); // warn: untrusted data used as a format string
1182  }
1183
1184There are built-in sources, propagations and sinks even if no external taint
1185configuration is provided.
1186
1187Default sources:
1188 ``_IO_getc``, ``fdopen``, ``fopen``, ``freopen``, ``get_current_dir_name``,
1189 ``getch``, ``getchar``, ``getchar_unlocked``, ``getwd``, ``getcwd``,
1190 ``getgroups``, ``gethostname``, ``getlogin``, ``getlogin_r``, ``getnameinfo``,
1191 ``gets``, ``gets_s``, ``getseuserbyname``, ``readlink``, ``readlinkat``,
1192 ``scanf``, ``scanf_s``, ``socket``, ``wgetch``
1193
1194Default propagations rules:
1195 ``atoi``, ``atol``, ``atoll``, ``basename``, ``dirname``, ``fgetc``,
1196 ``fgetln``, ``fgets``, ``fnmatch``, ``fread``, ``fscanf``, ``fscanf_s``,
1197 ``index``, ``inflate``, ``isalnum``, ``isalpha``, ``isascii``, ``isblank``,
1198 ``iscntrl``, ``isdigit``, ``isgraph``, ``islower``, ``isprint``, ``ispunct``,
1199 ``isspace``, ``isupper``, ``isxdigit``, ``memchr``, ``memrchr``, ``sscanf``,
1200 ``getc``, ``getc_unlocked``, ``getdelim``, ``getline``, ``getw``, ``memcmp``,
1201 ``memcpy``, ``memmem``, ``memmove``, ``mbtowc``, ``pread``, ``qsort``,
1202 ``qsort_r``, ``rawmemchr``, ``read``, ``recv``, ``recvfrom``, ``rindex``,
1203 ``strcasestr``, ``strchr``, ``strchrnul``, ``strcasecmp``, ``strcmp``,
1204 ``strcspn``, ``strncasecmp``, ``strncmp``, ``strndup``,
1205 ``strndupa``, ``strpbrk``, ``strrchr``, ``strsep``, ``strspn``,
1206 ``strstr``, ``strtol``, ``strtoll``, ``strtoul``, ``strtoull``, ``tolower``,
1207 ``toupper``, ``ttyname``, ``ttyname_r``, ``wctomb``, ``wcwidth``
1208
1209Default sinks:
1210 ``printf``, ``setproctitle``, ``system``, ``popen``, ``execl``, ``execle``,
1211 ``execlp``, ``execv``, ``execvp``, ``execvP``, ``execve``, ``dlopen``
1212
1213Please note that there are no built-in filter functions.
1214
1215One can configure their own taint sources, sinks, and propagation rules by
1216providing a configuration file via checker option
1217``optin.taint.TaintPropagation:Config``. The configuration file is in
1218`YAML <http://llvm.org/docs/YamlIO.html#introduction-to-yaml>`_ format. The
1219taint-related options defined in the config file extend but do not override the
1220built-in sources, rules, sinks. The format of the external taint configuration
1221file is not stable, and could change without any notice even in a non-backward
1222compatible way.
1223
1224For a more detailed description of configuration options, please see the
1225:doc:`user-docs/TaintAnalysisConfiguration`. For an example see
1226:ref:`clangsa-taint-configuration-example`.
1227
1228**Configuration**
1229
1230* `Config`  Specifies the name of the YAML configuration file. The user can
1231  define their own taint sources and sinks.
1232
1233**Related Guidelines**
1234
1235* `CWE Data Neutralization Issues
1236  <https://cwe.mitre.org/data/definitions/137.html>`_
1237* `SEI Cert STR02-C. Sanitize data passed to complex subsystems
1238  <https://wiki.sei.cmu.edu/confluence/display/c/STR02-C.+Sanitize+data+passed+to+complex+subsystems>`_
1239* `SEI Cert ENV33-C. Do not call system()
1240  <https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152177>`_
1241* `ENV03-C. Sanitize the environment when invoking external programs
1242  <https://wiki.sei.cmu.edu/confluence/display/c/ENV03-C.+Sanitize+the+environment+when+invoking+external+programs>`_
1243
1244**Limitations**
1245
1246* The taintedness property is not propagated through function calls which are
1247  unknown (or too complex) to the analyzer, unless there is a specific
1248  propagation rule built-in to the checker or given in the YAML configuration
1249  file. This causes potential true positive findings to be lost.
1250
1251
1252.. _optin-taint-TaintedAlloc:
1253
1254optin.taint.TaintedAlloc (C, C++)
1255"""""""""""""""""""""""""""""""""
1256
1257This checker warns for cases when the ``size`` parameter of the ``malloc`` ,
1258``calloc``, ``realloc``, ``alloca`` or the size parameter of the
1259array new C++ operator is tainted (potentially attacker controlled).
1260If an attacker can inject a large value as the size parameter, memory exhaustion
1261denial of service attack can be carried out.
1262
1263The analyzer emits warning only if it cannot prove that the size parameter is
1264within reasonable bounds (``<= SIZE_MAX/4``). This functionality partially
1265covers the SEI Cert coding standard rule `INT04-C
1266<https://wiki.sei.cmu.edu/confluence/display/c/INT04-C.+Enforce+limits+on+integer+values+originating+from+tainted+sources>`_.
1267
1268You can silence this warning either by bound checking the ``size`` parameter, or
1269by explicitly marking the ``size`` parameter as sanitized. See the
1270:ref:`optin-taint-GenericTaint` checker for an example.
1271
1272Custom allocation/deallocation functions can be defined using
1273:ref:`ownership attributes<analyzer-ownership-attrs>`.
1274
1275.. code-block:: c
1276
1277  void vulnerable(void) {
1278    size_t size = 0;
1279    scanf("%zu", &size);
1280    int *p = malloc(size); // warn: malloc is called with a tainted (potentially attacker controlled) value
1281    free(p);
1282  }
1283
1284  void not_vulnerable(void) {
1285    size_t size = 0;
1286    scanf("%zu", &size);
1287    if (1024 < size)
1288      return;
1289    int *p = malloc(size); // No warning expected as the the user input is bound
1290    free(p);
1291  }
1292
1293  void vulnerable_cpp(void) {
1294    size_t size = 0;
1295    scanf("%zu", &size);
1296    int *ptr = new int[size];// warn: Memory allocation function is called with a tainted (potentially attacker controlled) value
1297    delete[] ptr;
1298  }
1299
1300.. _optin-taint-TaintedDiv:
1301
1302optin.taint.TaintedDiv (C, C++, ObjC)
1303"""""""""""""""""""""""""""""""""""""
1304This checker warns when the denominator in a division
1305operation is a tainted (potentially attacker controlled) value.
1306If the attacker can set the denominator to 0, a runtime error can
1307be triggered. The checker warns when the denominator is a tainted
1308value and the analyzer cannot prove that it is not 0. This warning
1309is more pessimistic than the :ref:`core-DivideZero` checker
1310which warns only when it can prove that the denominator is 0.
1311
1312.. code-block:: c
1313
1314  int vulnerable(int n) {
1315    size_t size = 0;
1316    scanf("%zu", &size);
1317    return n / size; // warn: Division by a tainted value, possibly zero
1318  }
1319
1320  int not_vulnerable(int n) {
1321    size_t size = 0;
1322    scanf("%zu", &size);
1323    if (!size)
1324      return 0;
1325    return n / size; // no warning
1326  }
1327
1328.. _security-checkers:
1329
1330security
1331^^^^^^^^
1332
1333Security related checkers.
1334
1335.. _security-cert-env-InvalidPtr:
1336
1337security.cert.env.InvalidPtr
1338""""""""""""""""""""""""""""""""""
1339
1340Corresponds to SEI CERT Rules `ENV31-C <https://wiki.sei.cmu.edu/confluence/display/c/ENV31-C.+Do+not+rely+on+an+environment+pointer+following+an+operation+that+may+invalidate+it>`_ and `ENV34-C <https://wiki.sei.cmu.edu/confluence/display/c/ENV34-C.+Do+not+store+pointers+returned+by+certain+functions>`_.
1341
1342* **ENV31-C**:
1343  Rule is about the possible problem with ``main`` function's third argument, environment pointer,
1344  "envp". When environment array is modified using some modification function
1345  such as ``putenv``, ``setenv`` or others, It may happen that memory is reallocated,
1346  however "envp" is not updated to reflect the changes and points to old memory
1347  region.
1348
1349* **ENV34-C**:
1350  Some functions return a pointer to a statically allocated buffer.
1351  Consequently, subsequent call of these functions will invalidate previous
1352  pointer. These functions include: ``getenv``, ``localeconv``, ``asctime``, ``setlocale``, ``strerror``
1353
1354.. code-block:: c
1355
1356  int main(int argc, const char *argv[], const char *envp[]) {
1357    if (setenv("MY_NEW_VAR", "new_value", 1) != 0) {
1358      // setenv call may invalidate 'envp'
1359      /* Handle error */
1360    }
1361    if (envp != NULL) {
1362      for (size_t i = 0; envp[i] != NULL; ++i) {
1363        puts(envp[i]);
1364        // envp may no longer point to the current environment
1365        // this program has unanticipated behavior, since envp
1366        // does not reflect changes made by setenv function.
1367      }
1368    }
1369    return 0;
1370  }
1371
1372  void previous_call_invalidation() {
1373    char *p, *pp;
1374
1375    p = getenv("VAR");
1376    setenv("SOMEVAR", "VALUE", /*overwrite = */1);
1377    // call to 'setenv' may invalidate p
1378
1379    *p;
1380    // dereferencing invalid pointer
1381  }
1382
1383
1384The ``InvalidatingGetEnv`` option is available for treating ``getenv`` calls as
1385invalidating. When enabled, the checker issues a warning if ``getenv`` is called
1386multiple times and their results are used without first creating a copy.
1387This level of strictness might be considered overly pedantic for the commonly
1388used ``getenv`` implementations.
1389
1390To enable this option, use:
1391``-analyzer-config security.cert.env.InvalidPtr:InvalidatingGetEnv=true``.
1392
1393By default, this option is set to *false*.
1394
1395When this option is enabled, warnings will be generated for scenarios like the
1396following:
1397
1398.. code-block:: c
1399
1400  char* p = getenv("VAR");
1401  char* pp = getenv("VAR2"); // assumes this call can invalidate `env`
1402  strlen(p); // warns about accessing invalid ptr
1403
1404.. _security-FloatLoopCounter:
1405
1406security.FloatLoopCounter (C)
1407"""""""""""""""""""""""""""""
1408Warn on using a floating point value as a loop counter (CERT: FLP30-C, FLP30-CPP).
1409
1410.. code-block:: c
1411
1412 void test() {
1413   for (float x = 0.1f; x <= 1.0f; x += 0.1f) {} // warn
1414 }
1415
1416.. _security-insecureAPI-UncheckedReturn:
1417
1418security.insecureAPI.UncheckedReturn (C)
1419""""""""""""""""""""""""""""""""""""""""
1420Warn on uses of functions whose return values must be always checked.
1421
1422.. code-block:: c
1423
1424 void test() {
1425   setuid(1); // warn
1426 }
1427
1428.. _security-insecureAPI-bcmp:
1429
1430security.insecureAPI.bcmp (C)
1431"""""""""""""""""""""""""""""
1432Warn on uses of the 'bcmp' function.
1433
1434.. code-block:: c
1435
1436 void test() {
1437   bcmp(ptr0, ptr1, n); // warn
1438 }
1439
1440.. _security-insecureAPI-bcopy:
1441
1442security.insecureAPI.bcopy (C)
1443""""""""""""""""""""""""""""""
1444Warn on uses of the 'bcopy' function.
1445
1446.. code-block:: c
1447
1448 void test() {
1449   bcopy(src, dst, n); // warn
1450 }
1451
1452.. _security-insecureAPI-bzero:
1453
1454security.insecureAPI.bzero (C)
1455""""""""""""""""""""""""""""""
1456Warn on uses of the 'bzero' function.
1457
1458.. code-block:: c
1459
1460 void test() {
1461   bzero(ptr, n); // warn
1462 }
1463
1464.. _security-insecureAPI-getpw:
1465
1466security.insecureAPI.getpw (C)
1467""""""""""""""""""""""""""""""
1468Warn on uses of the 'getpw' function.
1469
1470.. code-block:: c
1471
1472 void test() {
1473   char buff[1024];
1474   getpw(2, buff); // warn
1475 }
1476
1477.. _security-insecureAPI-gets:
1478
1479security.insecureAPI.gets (C)
1480"""""""""""""""""""""""""""""
1481Warn on uses of the 'gets' function.
1482
1483.. code-block:: c
1484
1485 void test() {
1486   char buff[1024];
1487   gets(buff); // warn
1488 }
1489
1490.. _security-insecureAPI-mkstemp:
1491
1492security.insecureAPI.mkstemp (C)
1493""""""""""""""""""""""""""""""""
1494Warn when 'mkstemp' is passed fewer than 6 X's in the format string.
1495
1496.. code-block:: c
1497
1498 void test() {
1499   mkstemp("XX"); // warn
1500 }
1501
1502.. _security-insecureAPI-mktemp:
1503
1504security.insecureAPI.mktemp (C)
1505"""""""""""""""""""""""""""""""
1506Warn on uses of the ``mktemp`` function.
1507
1508.. code-block:: c
1509
1510 void test() {
1511   char *x = mktemp("/tmp/zxcv"); // warn: insecure, use mkstemp
1512 }
1513
1514.. _security-insecureAPI-rand:
1515
1516security.insecureAPI.rand (C)
1517"""""""""""""""""""""""""""""
1518Warn on uses of inferior random number generating functions (only if arc4random function is available):
1519``drand48, erand48, jrand48, lcong48, lrand48, mrand48, nrand48, random, rand_r``.
1520
1521.. code-block:: c
1522
1523 void test() {
1524   random(); // warn
1525 }
1526
1527.. _security-insecureAPI-strcpy:
1528
1529security.insecureAPI.strcpy (C)
1530"""""""""""""""""""""""""""""""
1531Warn on uses of the ``strcpy`` and ``strcat`` functions.
1532
1533.. code-block:: c
1534
1535 void test() {
1536   char x[4];
1537   char *y = "abcd";
1538
1539   strcpy(x, y); // warn
1540 }
1541
1542
1543.. _security-insecureAPI-vfork:
1544
1545security.insecureAPI.vfork (C)
1546""""""""""""""""""""""""""""""
1547 Warn on uses of the 'vfork' function.
1548
1549.. code-block:: c
1550
1551 void test() {
1552   vfork(); // warn
1553 }
1554
1555.. _security-insecureAPI-DeprecatedOrUnsafeBufferHandling:
1556
1557security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C)
1558"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1559 Warn on occurrences of unsafe or deprecated buffer handling functions, which now have a secure variant: ``sprintf, fprintf, vsprintf, scanf, wscanf, fscanf, fwscanf, vscanf, vwscanf, vfscanf, vfwscanf, sscanf, swscanf, vsscanf, vswscanf, swprintf, snprintf, vswprintf, vsnprintf, memcpy, memmove, strncpy, strncat, memset``
1560
1561.. code-block:: c
1562
1563 void test() {
1564   char buf [5];
1565   strncpy(buf, "a", 1); // warn
1566 }
1567
1568.. _security-MmapWriteExec:
1569
1570security.MmapWriteExec (C)
1571""""""""""""""""""""""""""
1572Warn on ``mmap()`` calls with both writable and executable access.
1573
1574.. code-block:: c
1575
1576 void test(int n) {
1577   void *c = mmap(NULL, 32, PROT_READ | PROT_WRITE | PROT_EXEC,
1578                  MAP_PRIVATE | MAP_ANON, -1, 0);
1579   // warn: Both PROT_WRITE and PROT_EXEC flags are set. This can lead to
1580   //       exploitable memory regions, which could be overwritten with malicious
1581   //       code
1582 }
1583
1584.. _security-PointerSub:
1585
1586security.PointerSub (C)
1587"""""""""""""""""""""""
1588Check for pointer subtractions on two pointers pointing to different memory
1589chunks. According to the C standard §6.5.6 only subtraction of pointers that
1590point into (or one past the end) the same array object is valid (for this
1591purpose non-array variables are like arrays of size 1). This checker only
1592searches for different memory objects at subtraction, but does not check if the
1593array index is correct. Furthermore, only cases are reported where
1594stack-allocated objects are involved (no warnings on pointers to memory
1595allocated by `malloc`).
1596
1597.. code-block:: c
1598
1599 void test() {
1600   int a, b, c[10], d[10];
1601   int x = &c[3] - &c[1];
1602   x = &d[4] - &c[1]; // warn: 'c' and 'd' are different arrays
1603   x = (&a + 1) - &a;
1604   x = &b - &a; // warn: 'a' and 'b' are different variables
1605 }
1606
1607 struct S {
1608   int x[10];
1609   int y[10];
1610 };
1611
1612 void test1() {
1613   struct S a[10];
1614   struct S b;
1615   int d = &a[4] - &a[6];
1616   d = &a[0].x[3] - &a[0].x[1];
1617   d = a[0].y - a[0].x; // warn: 'S.b' and 'S.a' are different objects
1618   d = (char *)&b.y - (char *)&b.x; // warn: different members of the same object
1619   d = (char *)&b.y - (char *)&b; // warn: object of type S is not the same array as a member of it
1620 }
1621
1622There may be existing applications that use code like above for calculating
1623offsets of members in a structure, using pointer subtractions. This is still
1624undefined behavior according to the standard and code like this can be replaced
1625with the `offsetof` macro.
1626
1627.. _security-putenv-stack-array:
1628
1629security.PutenvStackArray (C)
1630"""""""""""""""""""""""""""""
1631Finds calls to the ``putenv`` function which pass a pointer to a stack-allocated
1632(automatic) array as the argument. Function ``putenv`` does not copy the passed
1633string, only a pointer to the data is stored and this data can be read even by
1634other threads. Content of a stack-allocated array is likely to be overwritten
1635after exiting from the function.
1636
1637The problem can be solved by using a static array variable or dynamically
1638allocated memory. Even better is to avoid using ``putenv`` (it has other
1639problems related to memory leaks) and use ``setenv`` instead.
1640
1641The check corresponds to CERT rule
1642`POS34-C. Do not call putenv() with a pointer to an automatic variable as the argument
1643<https://wiki.sei.cmu.edu/confluence/display/c/POS34-C.+Do+not+call+putenv%28%29+with+a+pointer+to+an+automatic+variable+as+the+argument>`_.
1644
1645.. code-block:: c
1646
1647  int f() {
1648    char env[] = "NAME=value";
1649    return putenv(env); // putenv function should not be called with stack-allocated string
1650  }
1651
1652There is one case where the checker can report a false positive. This is when
1653the stack-allocated array is used at `putenv` in a function or code branch that
1654does not return (process is terminated on all execution paths).
1655
1656Another special case is if the `putenv` is called from function `main`. Here
1657the stack is deallocated at the end of the program and it should be no problem
1658to use the stack-allocated string (a multi-threaded program may require more
1659attention). The checker does not warn for cases when stack space of `main` is
1660used at the `putenv` call.
1661
1662security.SetgidSetuidOrder (C)
1663""""""""""""""""""""""""""""""
1664When dropping user-level and group-level privileges in a program by using
1665``setuid`` and ``setgid`` calls, it is important to reset the group-level
1666privileges (with ``setgid``) first. Function ``setgid`` will likely fail if
1667the superuser privileges are already dropped.
1668
1669The checker checks for sequences of ``setuid(getuid())`` and
1670``setgid(getgid())`` calls (in this order). If such a sequence is found and
1671there is no other privilege-changing function call (``seteuid``, ``setreuid``,
1672``setresuid`` and the GID versions of these) in between, a warning is
1673generated. The checker finds only exactly ``setuid(getuid())`` calls (and the
1674GID versions), not for example if the result of ``getuid()`` is stored in a
1675variable.
1676
1677.. code-block:: c
1678
1679 void test1() {
1680   // ...
1681   // end of section with elevated privileges
1682   // reset privileges (user and group) to normal user
1683   if (setuid(getuid()) != 0) {
1684     handle_error();
1685     return;
1686   }
1687   if (setgid(getgid()) != 0) { // warning: A 'setgid(getgid())' call following a 'setuid(getuid())' call is likely to fail
1688     handle_error();
1689     return;
1690   }
1691   // user-ID and group-ID are reset to normal user now
1692   // ...
1693 }
1694
1695In the code above the problem is that ``setuid(getuid())`` removes superuser
1696privileges before ``setgid(getgid())`` is called. To fix the problem the
1697``setgid(getgid())`` should be called first. Further attention is needed to
1698avoid code like ``setgid(getuid())`` (this checker does not detect bugs like
1699this) and always check the return value of these calls.
1700
1701This check corresponds to SEI CERT Rule `POS36-C <https://wiki.sei.cmu.edu/confluence/display/c/POS36-C.+Observe+correct+revocation+order+while+relinquishing+privileges>`_.
1702
1703.. _unix-checkers:
1704
1705unix
1706^^^^
1707POSIX/Unix checkers.
1708
1709.. _unix-API:
1710
1711unix.API (C)
1712""""""""""""
1713Check calls to various UNIX/Posix functions: ``open, pthread_once, calloc, malloc, realloc, alloca``.
1714
1715.. literalinclude:: checkers/unix_api_example.c
1716    :language: c
1717
1718.. _unix-BlockInCriticalSection:
1719
1720unix.BlockInCriticalSection (C, C++)
1721""""""""""""""""""""""""""""""""""""
1722Check for calls to blocking functions inside a critical section.
1723Blocking functions detected by this checker: ``sleep, getc, fgets, read, recv``.
1724Critical section handling functions modeled by this checker:
1725``lock, unlock, pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock, mtx_lock, mtx_timedlock, mtx_trylock, mtx_unlock, lock_guard, unique_lock``.
1726
1727.. code-block:: c
1728
1729 void pthread_lock_example(pthread_mutex_t *m) {
1730   pthread_mutex_lock(m); // note: entering critical section here
1731   sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
1732   pthread_mutex_unlock(m);
1733 }
1734
1735.. code-block:: cpp
1736
1737 void overlapping_critical_sections(mtx_t *m1, std::mutex &m2) {
1738   std::lock_guard lg{m2}; // note: entering critical section here
1739   mtx_lock(m1); // note: entering critical section here
1740   sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
1741   mtx_unlock(m1);
1742   sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
1743              // still inside of the critical section of the std::lock_guard
1744 }
1745
1746**Limitations**
1747
1748* The ``trylock`` and ``timedlock`` versions of acquiring locks are currently assumed to always succeed.
1749  This can lead to false positives.
1750
1751.. code-block:: c
1752
1753 void trylock_example(pthread_mutex_t *m) {
1754   if (pthread_mutex_trylock(m) == 0) { // assume trylock always succeeds
1755     sleep(10); // warn: Call to blocking function 'sleep' inside of critical section
1756     pthread_mutex_unlock(m);
1757   } else {
1758     sleep(10); // false positive: Incorrect warning about blocking function inside critical section.
1759   }
1760 }
1761
1762.. _unix-Chroot:
1763
1764unix.Chroot (C)
1765"""""""""""""""
1766Check improper use of chroot described by SEI Cert C recommendation `POS05-C.
1767Limit access to files by creating a jail
1768<https://wiki.sei.cmu.edu/confluence/display/c/POS05-C.+Limit+access+to+files+by+creating+a+jail>`_.
1769The checker finds usage patterns where ``chdir("/")`` is not called immediately
1770after a call to ``chroot(path)``.
1771
1772.. code-block:: c
1773
1774 void f();
1775
1776 void test_bad() {
1777   chroot("/usr/local");
1778   f(); // warn: no call of chdir("/") immediately after chroot
1779 }
1780
1781  void test_bad_path() {
1782    chroot("/usr/local");
1783    chdir("/usr"); // warn: no call of chdir("/") immediately after chroot
1784    f();
1785  }
1786
1787 void test_good() {
1788   chroot("/usr/local");
1789   chdir("/"); // no warning
1790   f();
1791 }
1792
1793.. _unix-Errno:
1794
1795unix.Errno (C)
1796""""""""""""""
1797
1798Check for improper use of ``errno``.
1799This checker implements partially CERT rule
1800`ERR30-C. Set errno to zero before calling a library function known to set errno,
1801and check errno only after the function returns a value indicating failure
1802<https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152351>`_.
1803The checker can find the first read of ``errno`` after successful standard
1804function calls.
1805
1806The C and POSIX standards often do not define if a standard library function
1807may change value of ``errno`` if the call does not fail.
1808Therefore, ``errno`` should only be used if it is known from the return value
1809of a function that the call has failed.
1810There are exceptions to this rule (for example ``strtol``) but the affected
1811functions are not yet supported by the checker.
1812The return values for the failure cases are documented in the standard Linux man
1813pages of the functions and in the `POSIX standard <https://pubs.opengroup.org/onlinepubs/9699919799/>`_.
1814
1815.. code-block:: c
1816
1817 int unsafe_errno_read(int sock, void *data, int data_size) {
1818   if (send(sock, data, data_size, 0) != data_size) {
1819     // 'send' can be successful even if not all data was sent
1820     if (errno == 1) { // An undefined value may be read from 'errno'
1821       return 0;
1822     }
1823   }
1824   return 1;
1825 }
1826
1827The checker :ref:`unix-StdCLibraryFunctions` must be turned on to get the
1828warnings from this checker. The supported functions are the same as by
1829:ref:`unix-StdCLibraryFunctions`. The ``ModelPOSIX`` option of that
1830checker affects the set of checked functions.
1831
1832**Parameters**
1833
1834The ``AllowErrnoReadOutsideConditionExpressions`` option allows read of the
1835errno value if the value is not used in a condition (in ``if`` statements,
1836loops, conditional expressions, ``switch`` statements). For example ``errno``
1837can be stored into a variable without getting a warning by the checker.
1838
1839.. code-block:: c
1840
1841 int unsafe_errno_read(int sock, void *data, int data_size) {
1842   if (send(sock, data, data_size, 0) != data_size) {
1843     int err = errno;
1844     // warning if 'AllowErrnoReadOutsideConditionExpressions' is false
1845     // no warning if 'AllowErrnoReadOutsideConditionExpressions' is true
1846   }
1847   return 1;
1848 }
1849
1850Default value of this option is ``true``. This allows save of the errno value
1851for possible later error handling.
1852
1853**Limitations**
1854
1855 - Only the very first usage of ``errno`` is checked after an affected function
1856   call. Value of ``errno`` is not followed when it is stored into a variable
1857   or returned from a function.
1858 - Documentation of function ``lseek`` is not clear about what happens if the
1859   function returns different value than the expected file position but not -1.
1860   To avoid possible false-positives ``errno`` is allowed to be used in this
1861   case.
1862
1863.. _unix-Malloc:
1864
1865unix.Malloc (C)
1866"""""""""""""""
1867Check for memory leaks, double free, and use-after-free problems. Traces memory managed by malloc()/free().
1868
1869Custom allocation/deallocation functions can be defined using
1870:ref:`ownership attributes<analyzer-ownership-attrs>`.
1871
1872.. literalinclude:: checkers/unix_malloc_example.c
1873    :language: c
1874
1875.. _unix-MallocSizeof:
1876
1877unix.MallocSizeof (C)
1878"""""""""""""""""""""
1879Check for dubious ``malloc`` arguments involving ``sizeof``.
1880
1881Custom allocation/deallocation functions can be defined using
1882:ref:`ownership attributes<analyzer-ownership-attrs>`.
1883
1884.. code-block:: c
1885
1886 void test() {
1887   long *p = malloc(sizeof(short));
1888     // warn: result is converted to 'long *', which is
1889     // incompatible with operand type 'short'
1890   free(p);
1891 }
1892
1893.. _unix-MismatchedDeallocator:
1894
1895unix.MismatchedDeallocator (C, C++)
1896"""""""""""""""""""""""""""""""""""
1897Check for mismatched deallocators.
1898
1899Custom allocation/deallocation functions can be defined using
1900:ref:`ownership attributes<analyzer-ownership-attrs>`.
1901
1902.. literalinclude:: checkers/mismatched_deallocator_example.cpp
1903    :language: c
1904
1905.. _unix-Vfork:
1906
1907unix.Vfork (C)
1908""""""""""""""
1909Check for proper usage of ``vfork``.
1910
1911.. code-block:: c
1912
1913 int test(int x) {
1914   pid_t pid = vfork(); // warn
1915   if (pid != 0)
1916     return 0;
1917
1918   switch (x) {
1919   case 0:
1920     pid = 1;
1921     execl("", "", 0);
1922     _exit(1);
1923     break;
1924   case 1:
1925     x = 0; // warn: this assignment is prohibited
1926     break;
1927   case 2:
1928     foo(); // warn: this function call is prohibited
1929     break;
1930   default:
1931     return 0; // warn: return is prohibited
1932   }
1933
1934   while(1);
1935 }
1936
1937.. _unix-cstring-BadSizeArg:
1938
1939unix.cstring.BadSizeArg (C)
1940"""""""""""""""""""""""""""
1941Check the size argument passed into C string functions for common erroneous patterns. Use ``-Wno-strncat-size`` compiler option to mute other ``strncat``-related compiler warnings.
1942
1943.. code-block:: c
1944
1945 void test() {
1946   char dest[3];
1947   strncat(dest, """""""""""""""""""""""""*", sizeof(dest));
1948     // warn: potential buffer overflow
1949 }
1950
1951.. _unix-cstring-NotNullTerminated:
1952
1953unix.cstring.NotNullTerminated (C)
1954""""""""""""""""""""""""""""""""""
1955Check for arguments which are not null-terminated strings;
1956applies to the ``strlen``, ``strcpy``, ``strcat``, ``strcmp`` family of functions.
1957
1958Only very fundamental cases are detected where the passed memory block is
1959absolutely different from a null-terminated string. This checker does not
1960find if a memory buffer is passed where the terminating zero character
1961is missing.
1962
1963.. code-block:: c
1964
1965 void test1() {
1966   int l = strlen((char *)&test1); // warn
1967 }
1968
1969 void test2() {
1970 label:
1971   int l = strlen((char *)&&label); // warn
1972 }
1973
1974.. _unix-cstring-NullArg:
1975
1976unix.cstring.NullArg (C)
1977""""""""""""""""""""""""
1978Check for null pointers being passed as arguments to C string functions:
1979``strlen, strnlen, strcpy, strncpy, strcat, strncat, strcmp, strncmp, strcasecmp, strncasecmp, wcslen, wcsnlen``.
1980
1981.. code-block:: c
1982
1983 int test() {
1984   return strlen(0); // warn
1985 }
1986
1987.. _unix-StdCLibraryFunctions:
1988
1989unix.StdCLibraryFunctions (C)
1990"""""""""""""""""""""""""""""
1991Check for calls of standard library functions that violate predefined argument
1992constraints. For example, according to the C standard the behavior of function
1993``int isalnum(int ch)`` is undefined if the value of ``ch`` is not representable
1994as ``unsigned char`` and is not equal to ``EOF``.
1995
1996You can think of this checker as defining restrictions (pre- and postconditions)
1997on standard library functions. Preconditions are checked, and when they are
1998violated, a warning is emitted. Postconditions are added to the analysis, e.g.
1999that the return value of a function is not greater than 255. Preconditions are
2000added to the analysis too, in the case when the affected values are not known
2001before the call.
2002
2003For example, if an argument to a function must be in between 0 and 255, but the
2004value of the argument is unknown, the analyzer will assume that it is in this
2005interval. Similarly, if a function mustn't be called with a null pointer and the
2006analyzer cannot prove that it is null, then it will assume that it is non-null.
2007
2008These are the possible checks on the values passed as function arguments:
2009 - The argument has an allowed range (or multiple ranges) of values. The checker
2010   can detect if a passed value is outside of the allowed range and show the
2011   actual and allowed values.
2012 - The argument has pointer type and is not allowed to be null pointer. Many
2013   (but not all) standard functions can produce undefined behavior if a null
2014   pointer is passed, these cases can be detected by the checker.
2015 - The argument is a pointer to a memory block and the minimal size of this
2016   buffer is determined by another argument to the function, or by
2017   multiplication of two arguments (like at function ``fread``), or is a fixed
2018   value (for example ``asctime_r`` requires at least a buffer of size 26). The
2019   checker can detect if the buffer size is too small and in optimal case show
2020   the size of the buffer and the values of the corresponding arguments.
2021
2022.. code-block:: c
2023
2024  #define EOF -1
2025  void test_alnum_concrete(int v) {
2026    int ret = isalnum(256); // \
2027    // warning: Function argument outside of allowed range
2028    (void)ret;
2029  }
2030
2031  void buffer_size_violation(FILE *file) {
2032    enum { BUFFER_SIZE = 1024 };
2033    wchar_t wbuf[BUFFER_SIZE];
2034
2035    const size_t size = sizeof(*wbuf);   // 4
2036    const size_t nitems = sizeof(wbuf);  // 4096
2037
2038    // Below we receive a warning because the 3rd parameter should be the
2039    // number of elements to read, not the size in bytes. This case is a known
2040    // vulnerability described by the ARR38-C SEI-CERT rule.
2041    fread(wbuf, size, nitems, file);
2042  }
2043
2044  int test_alnum_symbolic(int x) {
2045    int ret = isalnum(x);
2046    // after the call, ret is assumed to be in the range [-1, 255]
2047
2048    if (ret > 255)      // impossible (infeasible branch)
2049      if (x == 0)
2050        return ret / x; // division by zero is not reported
2051    return ret;
2052  }
2053
2054Additionally to the argument and return value conditions, this checker also adds
2055state of the value ``errno`` if applicable to the analysis. Many system
2056functions set the ``errno`` value only if an error occurs (together with a
2057specific return value of the function), otherwise it becomes undefined. This
2058checker changes the analysis state to contain such information. This data is
2059used by other checkers, for example :ref:`unix-Errno`.
2060
2061**Limitations**
2062
2063The checker can not always provide notes about the values of the arguments.
2064Without this information it is hard to confirm if the constraint is indeed
2065violated. The argument values are shown if they are known constants or the value
2066is determined by previous (not too complicated) assumptions.
2067
2068The checker can produce false positives in cases such as if the program has
2069invariants not known to the analyzer engine or the bug report path contains
2070calls to unknown functions. In these cases the analyzer fails to detect the real
2071range of the argument.
2072
2073**Parameters**
2074
2075The ``ModelPOSIX`` option controls if functions from the POSIX standard are
2076recognized by the checker.
2077
2078With ``ModelPOSIX=true``, many POSIX functions are modeled according to the
2079`POSIX standard`_. This includes ranges of parameters and possible return
2080values. Furthermore the behavior related to ``errno`` in the POSIX case is
2081often that ``errno`` is set only if a function call fails, and it becomes
2082undefined after a successful function call.
2083
2084With ``ModelPOSIX=false``, this checker follows the C99 language standard and
2085only models the functions that are described there. It is possible that the
2086same functions are modeled differently in the two cases because differences in
2087the standards. The C standard specifies less aspects of the functions, for
2088example exact ``errno`` behavior is often unspecified (and not modeled by the
2089checker).
2090
2091Default value of the option is ``true``.
2092
2093.. _unix-Stream:
2094
2095unix.Stream (C)
2096"""""""""""""""
2097Check C stream handling functions:
2098``fopen, fdopen, freopen, tmpfile, fclose, fread, fwrite, fgetc, fgets, fputc, fputs, fprintf, fscanf, ungetc, getdelim, getline, fseek, fseeko, ftell, ftello, fflush, rewind, fgetpos, fsetpos, clearerr, feof, ferror, fileno``.
2099
2100The checker maintains information about the C stream objects (``FILE *``) and
2101can detect error conditions related to use of streams. The following conditions
2102are detected:
2103
2104* The ``FILE *`` pointer passed to the function is NULL (the single exception is
2105  ``fflush`` where NULL is allowed).
2106* Use of stream after close.
2107* Opened stream is not closed.
2108* Read from a stream after end-of-file. (This is not a fatal error but reported
2109  by the checker. Stream remains in EOF state and the read operation fails.)
2110* Use of stream when the file position is indeterminate after a previous failed
2111  operation. Some functions (like ``ferror``, ``clearerr``, ``fseek``) are
2112  allowed in this state.
2113* Invalid 3rd ("``whence``") argument to ``fseek``.
2114
2115The stream operations are by this checker usually split into two cases, a success
2116and a failure case.
2117On the success case it also assumes that the current value of ``stdout``,
2118``stderr``, or ``stdin`` can't be equal to the file pointer returned by ``fopen``.
2119Operations performed on ``stdout``, ``stderr``, or ``stdin`` are not checked by
2120this checker in contrast to the streams opened by ``fopen``.
2121
2122In the case of write operations (like ``fwrite``,
2123``fprintf`` and even ``fsetpos``) this behavior could produce a large amount of
2124unwanted reports on projects that don't have error checks around the write
2125operations, so by default the checker assumes that write operations always succeed.
2126This behavior can be controlled by the ``Pedantic`` flag: With
2127``-analyzer-config unix.Stream:Pedantic=true`` the checker will model the
2128cases where a write operation fails and report situations where this leads to
2129erroneous behavior. (The default is ``Pedantic=false``, where write operations
2130are assumed to succeed.)
2131
2132.. code-block:: c
2133
2134 void test1() {
2135   FILE *p = fopen("foo", "r");
2136 } // warn: opened file is never closed
2137
2138 void test2() {
2139   FILE *p = fopen("foo", "r");
2140   fseek(p, 1, SEEK_SET); // warn: stream pointer might be NULL
2141   fclose(p);
2142 }
2143
2144 void test3() {
2145   FILE *p = fopen("foo", "r");
2146   if (p) {
2147     fseek(p, 1, 3); // warn: third arg should be SEEK_SET, SEEK_END, or SEEK_CUR
2148     fclose(p);
2149   }
2150 }
2151
2152 void test4() {
2153   FILE *p = fopen("foo", "r");
2154   if (!p)
2155     return;
2156
2157   fclose(p);
2158   fclose(p); // warn: stream already closed
2159 }
2160
2161 void test5() {
2162   FILE *p = fopen("foo", "r");
2163   if (!p)
2164     return;
2165
2166   fgetc(p);
2167   if (!ferror(p))
2168     fgetc(p); // warn: possible read after end-of-file
2169
2170   fclose(p);
2171 }
2172
2173 void test6() {
2174   FILE *p = fopen("foo", "r");
2175   if (!p)
2176     return;
2177
2178   fgetc(p);
2179   if (!feof(p))
2180     fgetc(p); // warn: file position may be indeterminate after I/O error
2181
2182   fclose(p);
2183 }
2184
2185**Limitations**
2186
2187The checker does not track the correspondence between integer file descriptors
2188and ``FILE *`` pointers.
2189
2190.. _osx-checkers:
2191
2192osx
2193^^^
2194macOS checkers.
2195
2196.. _osx-API:
2197
2198osx.API (C)
2199"""""""""""
2200Check for proper uses of various Apple APIs.
2201
2202.. code-block:: objc
2203
2204 void test() {
2205   dispatch_once_t pred = 0;
2206   dispatch_once(&pred, ^(){}); // warn: dispatch_once uses local
2207 }
2208
2209.. _osx-NumberObjectConversion:
2210
2211osx.NumberObjectConversion (C, C++, ObjC)
2212"""""""""""""""""""""""""""""""""""""""""
2213Check for erroneous conversions of objects representing numbers into numbers.
2214
2215.. code-block:: objc
2216
2217 NSNumber *photoCount = [albumDescriptor objectForKey:@"PhotoCount"];
2218 // Warning: Comparing a pointer value of type 'NSNumber *'
2219 // to a scalar integer value
2220 if (photoCount > 0) {
2221   [self displayPhotos];
2222 }
2223
2224.. _osx-ObjCProperty:
2225
2226osx.ObjCProperty (ObjC)
2227"""""""""""""""""""""""
2228Check for proper uses of Objective-C properties.
2229
2230.. code-block:: objc
2231
2232 NSNumber *photoCount = [albumDescriptor objectForKey:@"PhotoCount"];
2233 // Warning: Comparing a pointer value of type 'NSNumber *'
2234 // to a scalar integer value
2235 if (photoCount > 0) {
2236   [self displayPhotos];
2237 }
2238
2239
2240.. _osx-SecKeychainAPI:
2241
2242osx.SecKeychainAPI (C)
2243""""""""""""""""""""""
2244Check for proper uses of Secure Keychain APIs.
2245
2246.. literalinclude:: checkers/seckeychainapi_example.m
2247    :language: objc
2248
2249.. _osx-cocoa-AtSync:
2250
2251osx.cocoa.AtSync (ObjC)
2252"""""""""""""""""""""""
2253Check for nil pointers used as mutexes for @synchronized.
2254
2255.. code-block:: objc
2256
2257 void test(id x) {
2258   if (!x)
2259     @synchronized(x) {} // warn: nil value used as mutex
2260 }
2261
2262 void test() {
2263   id y;
2264   @synchronized(y) {} // warn: uninitialized value used as mutex
2265 }
2266
2267.. _osx-cocoa-AutoreleaseWrite:
2268
2269osx.cocoa.AutoreleaseWrite
2270""""""""""""""""""""""""""
2271Warn about potentially crashing writes to autoreleasing objects from different autoreleasing pools in Objective-C.
2272
2273.. _osx-cocoa-ClassRelease:
2274
2275osx.cocoa.ClassRelease (ObjC)
2276"""""""""""""""""""""""""""""
2277Check for sending 'retain', 'release', or 'autorelease' directly to a Class.
2278
2279.. code-block:: objc
2280
2281 @interface MyClass : NSObject
2282 @end
2283
2284 void test(void) {
2285   [MyClass release]; // warn
2286 }
2287
2288.. _osx-cocoa-Dealloc:
2289
2290osx.cocoa.Dealloc (ObjC)
2291""""""""""""""""""""""""
2292Warn about Objective-C classes that lack a correct implementation of -dealloc
2293
2294.. literalinclude:: checkers/dealloc_example.m
2295    :language: objc
2296
2297.. _osx-cocoa-IncompatibleMethodTypes:
2298
2299osx.cocoa.IncompatibleMethodTypes (ObjC)
2300""""""""""""""""""""""""""""""""""""""""
2301Warn about Objective-C method signatures with type incompatibilities.
2302
2303.. code-block:: objc
2304
2305 @interface MyClass1 : NSObject
2306 - (int)foo;
2307 @end
2308
2309 @implementation MyClass1
2310 - (int)foo { return 1; }
2311 @end
2312
2313 @interface MyClass2 : MyClass1
2314 - (float)foo;
2315 @end
2316
2317 @implementation MyClass2
2318 - (float)foo { return 1.0; } // warn
2319 @end
2320
2321.. _osx-cocoa-Loops:
2322
2323osx.cocoa.Loops
2324"""""""""""""""
2325Improved modeling of loops using Cocoa collection types.
2326
2327.. _osx-cocoa-MissingSuperCall:
2328
2329osx.cocoa.MissingSuperCall (ObjC)
2330"""""""""""""""""""""""""""""""""
2331Warn about Objective-C methods that lack a necessary call to super.
2332
2333.. code-block:: objc
2334
2335 @interface Test : UIViewController
2336 @end
2337 @implementation test
2338 - (void)viewDidLoad {} // warn
2339 @end
2340
2341
2342.. _osx-cocoa-NSAutoreleasePool:
2343
2344osx.cocoa.NSAutoreleasePool (ObjC)
2345""""""""""""""""""""""""""""""""""
2346Warn for suboptimal uses of NSAutoreleasePool in Objective-C GC mode.
2347
2348.. code-block:: objc
2349
2350 void test() {
2351   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2352   [pool release]; // warn
2353 }
2354
2355.. _osx-cocoa-NSError:
2356
2357osx.cocoa.NSError (ObjC)
2358""""""""""""""""""""""""
2359Check usage of NSError parameters.
2360
2361.. code-block:: objc
2362
2363 @interface A : NSObject
2364 - (void)foo:(NSError """""""""""""""""""""""")error;
2365 @end
2366
2367 @implementation A
2368 - (void)foo:(NSError """""""""""""""""""""""")error {
2369   // warn: method accepting NSError"""""""""""""""""""""""" should have a non-void
2370   // return value
2371 }
2372 @end
2373
2374 @interface A : NSObject
2375 - (BOOL)foo:(NSError """""""""""""""""""""""")error;
2376 @end
2377
2378 @implementation A
2379 - (BOOL)foo:(NSError """""""""""""""""""""""")error {
2380   *error = 0; // warn: potential null dereference
2381   return 0;
2382 }
2383 @end
2384
2385.. _osx-cocoa-NilArg:
2386
2387osx.cocoa.NilArg (ObjC)
2388"""""""""""""""""""""""
2389Check for prohibited nil arguments to ObjC method calls.
2390
2391 - caseInsensitiveCompare:
2392 - compare:
2393 - compare:options:
2394 - compare:options:range:
2395 - compare:options:range:locale:
2396 - componentsSeparatedByCharactersInSet:
2397 - initWithFormat:
2398
2399.. code-block:: objc
2400
2401 NSComparisonResult test(NSString *s) {
2402   NSString *aString = nil;
2403   return [s caseInsensitiveCompare:aString];
2404     // warn: argument to 'NSString' method
2405     // 'caseInsensitiveCompare:' cannot be nil
2406 }
2407
2408
2409.. _osx-cocoa-NonNilReturnValue:
2410
2411osx.cocoa.NonNilReturnValue
2412"""""""""""""""""""""""""""
2413Models the APIs that are guaranteed to return a non-nil value.
2414
2415.. _osx-cocoa-ObjCGenerics:
2416
2417osx.cocoa.ObjCGenerics (ObjC)
2418"""""""""""""""""""""""""""""
2419Check for type errors when using Objective-C generics.
2420
2421.. code-block:: objc
2422
2423 NSMutableArray *names = [NSMutableArray array];
2424 NSMutableArray *birthDates = names;
2425
2426 // Warning: Conversion from value of type 'NSDate *'
2427 // to incompatible type 'NSString *'
2428 [birthDates addObject: [NSDate date]];
2429
2430.. _osx-cocoa-RetainCount:
2431
2432osx.cocoa.RetainCount (ObjC)
2433""""""""""""""""""""""""""""
2434Check for leaks and improper reference count management
2435
2436.. code-block:: objc
2437
2438 void test() {
2439   NSString *s = [[NSString alloc] init]; // warn
2440 }
2441
2442 CFStringRef test(char *bytes) {
2443   return CFStringCreateWithCStringNoCopy(
2444            0, bytes, NSNEXTSTEPStringEncoding, 0); // warn
2445 }
2446
2447
2448.. _osx-cocoa-RunLoopAutoreleaseLeak:
2449
2450osx.cocoa.RunLoopAutoreleaseLeak
2451""""""""""""""""""""""""""""""""
2452Check for leaked memory in autorelease pools that will never be drained.
2453
2454.. _osx-cocoa-SelfInit:
2455
2456osx.cocoa.SelfInit (ObjC)
2457"""""""""""""""""""""""""
2458Check that 'self' is properly initialized inside an initializer method.
2459
2460.. code-block:: objc
2461
2462 @interface MyObj : NSObject {
2463   id x;
2464 }
2465 - (id)init;
2466 @end
2467
2468 @implementation MyObj
2469 - (id)init {
2470   [super init];
2471   x = 0; // warn: instance variable used while 'self' is not
2472          // initialized
2473   return 0;
2474 }
2475 @end
2476
2477 @interface MyObj : NSObject
2478 - (id)init;
2479 @end
2480
2481 @implementation MyObj
2482 - (id)init {
2483   [super init];
2484   return self; // warn: returning uninitialized 'self'
2485 }
2486 @end
2487
2488.. _osx-cocoa-SuperDealloc:
2489
2490osx.cocoa.SuperDealloc (ObjC)
2491"""""""""""""""""""""""""""""
2492Warn about improper use of '[super dealloc]' in Objective-C.
2493
2494.. code-block:: objc
2495
2496 @interface SuperDeallocThenReleaseIvarClass : NSObject {
2497   NSObject *_ivar;
2498 }
2499 @end
2500
2501 @implementation SuperDeallocThenReleaseIvarClass
2502 - (void)dealloc {
2503   [super dealloc];
2504   [_ivar release]; // warn
2505 }
2506 @end
2507
2508.. _osx-cocoa-UnusedIvars:
2509
2510osx.cocoa.UnusedIvars (ObjC)
2511""""""""""""""""""""""""""""
2512Warn about private ivars that are never used.
2513
2514.. code-block:: objc
2515
2516 @interface MyObj : NSObject {
2517 @private
2518   id x; // warn
2519 }
2520 @end
2521
2522 @implementation MyObj
2523 @end
2524
2525.. _osx-cocoa-VariadicMethodTypes:
2526
2527osx.cocoa.VariadicMethodTypes (ObjC)
2528""""""""""""""""""""""""""""""""""""
2529Check for passing non-Objective-C types to variadic collection
2530initialization methods that expect only Objective-C types.
2531
2532.. code-block:: objc
2533
2534 void test() {
2535   [NSSet setWithObjects:@"Foo", "Bar", nil];
2536     // warn: argument should be an ObjC pointer type, not 'char *'
2537 }
2538
2539.. _osx-coreFoundation-CFError:
2540
2541osx.coreFoundation.CFError (C)
2542""""""""""""""""""""""""""""""
2543Check usage of CFErrorRef* parameters
2544
2545.. code-block:: c
2546
2547 void test(CFErrorRef *error) {
2548   // warn: function accepting CFErrorRef* should have a
2549   // non-void return
2550 }
2551
2552 int foo(CFErrorRef *error) {
2553   *error = 0; // warn: potential null dereference
2554   return 0;
2555 }
2556
2557.. _osx-coreFoundation-CFNumber:
2558
2559osx.coreFoundation.CFNumber (C)
2560"""""""""""""""""""""""""""""""
2561Check for proper uses of CFNumber APIs.
2562
2563.. code-block:: c
2564
2565 CFNumberRef test(unsigned char x) {
2566   return CFNumberCreate(0, kCFNumberSInt16Type, &x);
2567    // warn: 8 bit integer is used to initialize a 16 bit integer
2568 }
2569
2570.. _osx-coreFoundation-CFRetainRelease:
2571
2572osx.coreFoundation.CFRetainRelease (C)
2573""""""""""""""""""""""""""""""""""""""
2574Check for null arguments to CFRetain/CFRelease/CFMakeCollectable.
2575
2576.. code-block:: c
2577
2578 void test(CFTypeRef p) {
2579   if (!p)
2580     CFRetain(p); // warn
2581 }
2582
2583 void test(int x, CFTypeRef p) {
2584   if (p)
2585     return;
2586
2587   CFRelease(p); // warn
2588 }
2589
2590.. _osx-coreFoundation-containers-OutOfBounds:
2591
2592osx.coreFoundation.containers.OutOfBounds (C)
2593"""""""""""""""""""""""""""""""""""""""""""""
2594Checks for index out-of-bounds when using 'CFArray' API.
2595
2596.. code-block:: c
2597
2598 void test() {
2599   CFArrayRef A = CFArrayCreate(0, 0, 0, &kCFTypeArrayCallBacks);
2600   CFArrayGetValueAtIndex(A, 0); // warn
2601 }
2602
2603.. _osx-coreFoundation-containers-PointerSizedValues:
2604
2605osx.coreFoundation.containers.PointerSizedValues (C)
2606""""""""""""""""""""""""""""""""""""""""""""""""""""
2607Warns if 'CFArray', 'CFDictionary', 'CFSet' are created with non-pointer-size values.
2608
2609.. code-block:: c
2610
2611 void test() {
2612   int x[] = { 1 };
2613   CFArrayRef A = CFArrayCreate(0, (const void """""""""""""""""""""""")x, 1,
2614                                &kCFTypeArrayCallBacks); // warn
2615 }
2616
2617Fuchsia
2618^^^^^^^
2619
2620Fuchsia is an open source capability-based operating system currently being
2621developed by Google. This section describes checkers that can find various
2622misuses of Fuchsia APIs.
2623
2624.. _fuchsia-HandleChecker:
2625
2626fuchsia.HandleChecker
2627""""""""""""""""""""""""""""
2628Handles identify resources. Similar to pointers they can be leaked,
2629double freed, or use after freed. This check attempts to find such problems.
2630
2631.. code-block:: cpp
2632
2633 void checkLeak08(int tag) {
2634   zx_handle_t sa, sb;
2635   zx_channel_create(0, &sa, &sb);
2636   if (tag)
2637     zx_handle_close(sa);
2638   use(sb); // Warn: Potential leak of handle
2639   zx_handle_close(sb);
2640 }
2641
2642WebKit
2643^^^^^^
2644
2645WebKit is an open-source web browser engine available for macOS, iOS and Linux.
2646This section describes checkers that can find issues in WebKit codebase.
2647
2648Most of the checkers focus on memory management for which WebKit uses custom implementation of reference counted smartpointers.
2649
2650Checkers are formulated in terms related to ref-counting:
2651 - *Ref-counted type* is either ``Ref<T>`` or ``RefPtr<T>``.
2652 - *Ref-countable type* is any type that implements ``ref()`` and ``deref()`` methods as ``RefPtr<>`` is a template (i. e. relies on duck typing).
2653 - *Uncounted type* is ref-countable but not ref-counted type.
2654
2655.. _webkit-RefCntblBaseVirtualDtor:
2656
2657webkit.RefCntblBaseVirtualDtor
2658""""""""""""""""""""""""""""""""""""
2659All uncounted types used as base classes must have a virtual destructor.
2660
2661Ref-counted types hold their ref-countable data by a raw pointer and allow implicit upcasting from ref-counted pointer to derived type to ref-counted pointer to base type. This might lead to an object of (dynamic) derived type being deleted via pointer to the base class type which C++ standard defines as UB in case the base class doesn't have virtual destructor ``[expr.delete]``.
2662
2663.. code-block:: cpp
2664
2665 struct RefCntblBase {
2666   void ref() {}
2667   void deref() {}
2668 };
2669
2670 struct Derived : RefCntblBase { }; // warn
2671
2672.. _webkit-NoUncountedMemberChecker:
2673
2674webkit.NoUncountedMemberChecker
2675"""""""""""""""""""""""""""""""""""""
2676Raw pointers and references to uncounted types can't be used as class members. Only ref-counted types are allowed.
2677
2678.. code-block:: cpp
2679
2680 struct RefCntbl {
2681   void ref() {}
2682   void deref() {}
2683 };
2684
2685 struct Foo {
2686   RefCntbl * ptr; // warn
2687   RefCntbl & ptr; // warn
2688   // ...
2689 };
2690
2691.. _webkit-UncountedLambdaCapturesChecker:
2692
2693webkit.UncountedLambdaCapturesChecker
2694"""""""""""""""""""""""""""""""""""""
2695Raw pointers and references to uncounted types can't be captured in lambdas. Only ref-counted types are allowed.
2696
2697.. code-block:: cpp
2698
2699 struct RefCntbl {
2700   void ref() {}
2701   void deref() {}
2702 };
2703
2704 void foo(RefCntbl* a, RefCntbl& b) {
2705   [&, a](){ // warn about 'a'
2706     do_something(b); // warn about 'b'
2707   };
2708 };
2709
2710.. _alpha-checkers:
2711
2712Experimental Checkers
2713---------------------
2714
2715*These are checkers with known issues or limitations that keep them from being on by default. They are likely to have false positives. Bug reports and especially patches are welcome.*
2716
2717alpha.clone
2718^^^^^^^^^^^
2719
2720.. _alpha-clone-CloneChecker:
2721
2722alpha.clone.CloneChecker (C, C++, ObjC)
2723"""""""""""""""""""""""""""""""""""""""
2724Reports similar pieces of code.
2725
2726.. code-block:: c
2727
2728 void log();
2729
2730 int max(int a, int b) { // warn
2731   log();
2732   if (a > b)
2733     return a;
2734   return b;
2735 }
2736
2737 int maxClone(int x, int y) { // similar code here
2738   log();
2739   if (x > y)
2740     return x;
2741   return y;
2742 }
2743
2744alpha.core
2745^^^^^^^^^^
2746
2747.. _alpha-core-BoolAssignment:
2748
2749alpha.core.BoolAssignment (ObjC)
2750""""""""""""""""""""""""""""""""
2751Warn about assigning non-{0,1} values to boolean variables.
2752
2753.. code-block:: objc
2754
2755 void test() {
2756   BOOL b = -1; // warn
2757 }
2758
2759.. _alpha-core-C11Lock:
2760
2761alpha.core.C11Lock
2762""""""""""""""""""
2763Similarly to :ref:`alpha.unix.PthreadLock <alpha-unix-PthreadLock>`, checks for
2764the locking/unlocking of ``mtx_t`` mutexes.
2765
2766.. code-block:: cpp
2767
2768 mtx_t mtx1;
2769
2770 void bad1(void)
2771 {
2772   mtx_lock(&mtx1);
2773   mtx_lock(&mtx1); // warn: This lock has already been acquired
2774 }
2775
2776.. _alpha-core-CastSize:
2777
2778alpha.core.CastSize (C)
2779"""""""""""""""""""""""
2780Check when casting a malloc'ed type ``T``, whether the size is a multiple of the size of ``T``.
2781
2782.. code-block:: c
2783
2784 void test() {
2785   int *x = (int *) malloc(11); // warn
2786 }
2787
2788.. _alpha-core-CastToStruct:
2789
2790alpha.core.CastToStruct (C, C++)
2791""""""""""""""""""""""""""""""""
2792Check for cast from non-struct pointer to struct pointer.
2793
2794.. code-block:: cpp
2795
2796 // C
2797 struct s {};
2798
2799 void test(int *p) {
2800   struct s *ps = (struct s *) p; // warn
2801 }
2802
2803 // C++
2804 class c {};
2805
2806 void test(int *p) {
2807   c *pc = (c *) p; // warn
2808 }
2809
2810.. _alpha-core-Conversion:
2811
2812alpha.core.Conversion (C, C++, ObjC)
2813""""""""""""""""""""""""""""""""""""
2814Loss of sign/precision in implicit conversions.
2815
2816.. code-block:: c
2817
2818 void test(unsigned U, signed S) {
2819   if (S > 10) {
2820     if (U < S) {
2821     }
2822   }
2823   if (S < -10) {
2824     if (U < S) { // warn (loss of sign)
2825     }
2826   }
2827 }
2828
2829 void test() {
2830   long long A = 1LL << 60;
2831   short X = A; // warn (loss of precision)
2832 }
2833
2834.. _alpha-core-DynamicTypeChecker:
2835
2836alpha.core.DynamicTypeChecker (ObjC)
2837""""""""""""""""""""""""""""""""""""
2838Check for cases where the dynamic and the static type of an object are unrelated.
2839
2840
2841.. code-block:: objc
2842
2843 id date = [NSDate date];
2844
2845 // Warning: Object has a dynamic type 'NSDate *' which is
2846 // incompatible with static type 'NSNumber *'"
2847 NSNumber *number = date;
2848 [number doubleValue];
2849
2850.. _alpha-core-FixedAddr:
2851
2852alpha.core.FixedAddr (C)
2853""""""""""""""""""""""""
2854Check for assignment of a fixed address to a pointer.
2855
2856.. code-block:: c
2857
2858 void test() {
2859   int *p;
2860   p = (int *) 0x10000; // warn
2861 }
2862
2863.. _alpha-core-PointerArithm:
2864
2865alpha.core.PointerArithm (C)
2866""""""""""""""""""""""""""""
2867Check for pointer arithmetic on locations other than array elements.
2868
2869.. code-block:: c
2870
2871 void test() {
2872   int x;
2873   int *p;
2874   p = &x + 1; // warn
2875 }
2876
2877.. _alpha-core-StackAddressAsyncEscape:
2878
2879alpha.core.StackAddressAsyncEscape (ObjC)
2880"""""""""""""""""""""""""""""""""""""""""
2881Check that addresses to stack memory do not escape the function that involves dispatch_after or dispatch_async.
2882This checker is a part of ``core.StackAddressEscape``, but is temporarily disabled until some false positives are fixed.
2883
2884.. code-block:: c
2885
2886 dispatch_block_t test_block_inside_block_async_leak() {
2887   int x = 123;
2888   void (^inner)(void) = ^void(void) {
2889     int y = x;
2890     ++y;
2891   };
2892   void (^outer)(void) = ^void(void) {
2893     int z = x;
2894     ++z;
2895     inner();
2896   };
2897   return outer; // warn: address of stack-allocated block is captured by a
2898                 //       returned block
2899 }
2900
2901.. _alpha-core-StdVariant:
2902
2903alpha.core.StdVariant (C++)
2904"""""""""""""""""""""""""""
2905Check if a value of active type is retrieved from an ``std::variant`` instance with ``std::get``.
2906In case of bad variant type access (the accessed type differs from the active type)
2907a warning is emitted. Currently, this checker does not take exception handling into account.
2908
2909.. code-block:: cpp
2910
2911 void test() {
2912   std::variant<int, char> v = 25;
2913   char c = stg::get<char>(v); // warn: "int" is the active alternative
2914 }
2915
2916.. _alpha-core-TestAfterDivZero:
2917
2918alpha.core.TestAfterDivZero (C)
2919"""""""""""""""""""""""""""""""
2920Check for division by variable that is later compared against 0.
2921Either the comparison is useless or there is division by zero.
2922
2923.. code-block:: c
2924
2925 void test(int x) {
2926   var = 77 / x;
2927   if (x == 0) { } // warn
2928 }
2929
2930alpha.cplusplus
2931^^^^^^^^^^^^^^^
2932
2933.. _alpha-cplusplus-DeleteWithNonVirtualDtor:
2934
2935alpha.cplusplus.DeleteWithNonVirtualDtor (C++)
2936""""""""""""""""""""""""""""""""""""""""""""""
2937Reports destructions of polymorphic objects with a non-virtual destructor in their base class.
2938
2939.. code-block:: cpp
2940
2941 class NonVirtual {};
2942 class NVDerived : public NonVirtual {};
2943
2944 NonVirtual *create() {
2945   NonVirtual *x = new NVDerived(); // note: Casting from 'NVDerived' to
2946                                    //       'NonVirtual' here
2947   return x;
2948 }
2949
2950 void foo() {
2951   NonVirtual *x = create();
2952   delete x; // warn: destruction of a polymorphic object with no virtual
2953             //       destructor
2954 }
2955
2956.. _alpha-cplusplus-InvalidatedIterator:
2957
2958alpha.cplusplus.InvalidatedIterator (C++)
2959"""""""""""""""""""""""""""""""""""""""""
2960Check for use of invalidated iterators.
2961
2962.. code-block:: cpp
2963
2964 void bad_copy_assign_operator_list1(std::list &L1,
2965                                     const std::list &L2) {
2966   auto i0 = L1.cbegin();
2967   L1 = L2;
2968   *i0; // warn: invalidated iterator accessed
2969 }
2970
2971
2972.. _alpha-cplusplus-IteratorRange:
2973
2974alpha.cplusplus.IteratorRange (C++)
2975"""""""""""""""""""""""""""""""""""
2976Check for iterators used outside their valid ranges.
2977
2978.. code-block:: cpp
2979
2980 void simple_bad_end(const std::vector &v) {
2981   auto i = v.end();
2982   *i; // warn: iterator accessed outside of its range
2983 }
2984
2985.. _alpha-cplusplus-MismatchedIterator:
2986
2987alpha.cplusplus.MismatchedIterator (C++)
2988""""""""""""""""""""""""""""""""""""""""
2989Check for use of iterators of different containers where iterators of the same container are expected.
2990
2991.. code-block:: cpp
2992
2993 void bad_insert3(std::vector &v1, std::vector &v2) {
2994   v2.insert(v1.cbegin(), v2.cbegin(), v2.cend()); // warn: container accessed
2995                                                   //       using foreign
2996                                                   //       iterator argument
2997   v1.insert(v1.cbegin(), v1.cbegin(), v2.cend()); // warn: iterators of
2998                                                   //       different containers
2999                                                   //       used where the same
3000                                                   //       container is
3001                                                   //       expected
3002   v1.insert(v1.cbegin(), v2.cbegin(), v1.cend()); // warn: iterators of
3003                                                   //       different containers
3004                                                   //       used where the same
3005                                                   //       container is
3006                                                   //       expected
3007 }
3008
3009.. _alpha-cplusplus-SmartPtr:
3010
3011alpha.cplusplus.SmartPtr (C++)
3012""""""""""""""""""""""""""""""
3013Check for dereference of null smart pointers.
3014
3015.. code-block:: cpp
3016
3017 void deref_smart_ptr() {
3018   std::unique_ptr<int> P;
3019   *P; // warn: dereference of a default constructed smart unique_ptr
3020 }
3021
3022
3023alpha.deadcode
3024^^^^^^^^^^^^^^
3025.. _alpha-deadcode-UnreachableCode:
3026
3027alpha.deadcode.UnreachableCode (C, C++)
3028"""""""""""""""""""""""""""""""""""""""
3029Check unreachable code.
3030
3031.. code-block:: cpp
3032
3033 // C
3034 int test() {
3035   int x = 1;
3036   while(x);
3037   return x; // warn
3038 }
3039
3040 // C++
3041 void test() {
3042   int a = 2;
3043
3044   while (a > 1)
3045     a--;
3046
3047   if (a > 1)
3048     a++; // warn
3049 }
3050
3051 // Objective-C
3052 void test(id x) {
3053   return;
3054   [x retain]; // warn
3055 }
3056
3057alpha.fuchsia
3058^^^^^^^^^^^^^
3059
3060.. _alpha-fuchsia-lock:
3061
3062alpha.fuchsia.Lock
3063""""""""""""""""""
3064Similarly to :ref:`alpha.unix.PthreadLock <alpha-unix-PthreadLock>`, checks for
3065the locking/unlocking of fuchsia mutexes.
3066
3067.. code-block:: cpp
3068
3069 spin_lock_t mtx1;
3070
3071 void bad1(void)
3072 {
3073   spin_lock(&mtx1);
3074   spin_lock(&mtx1);	// warn: This lock has already been acquired
3075 }
3076
3077alpha.llvm
3078^^^^^^^^^^
3079
3080.. _alpha-llvm-Conventions:
3081
3082alpha.llvm.Conventions
3083""""""""""""""""""""""
3084
3085Check code for LLVM codebase conventions:
3086
3087* A StringRef should not be bound to a temporary std::string whose lifetime is shorter than the StringRef's.
3088* Clang AST nodes should not have fields that can allocate memory.
3089
3090
3091alpha.osx
3092^^^^^^^^^
3093
3094.. _alpha-osx-cocoa-DirectIvarAssignment:
3095
3096alpha.osx.cocoa.DirectIvarAssignment (ObjC)
3097"""""""""""""""""""""""""""""""""""""""""""
3098Check for direct assignments to instance variables.
3099
3100
3101.. code-block:: objc
3102
3103 @interface MyClass : NSObject {}
3104 @property (readonly) id A;
3105 - (void) foo;
3106 @end
3107
3108 @implementation MyClass
3109 - (void) foo {
3110   _A = 0; // warn
3111 }
3112 @end
3113
3114.. _alpha-osx-cocoa-DirectIvarAssignmentForAnnotatedFunctions:
3115
3116alpha.osx.cocoa.DirectIvarAssignmentForAnnotatedFunctions (ObjC)
3117""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
3118Check for direct assignments to instance variables in
3119the methods annotated with ``objc_no_direct_instance_variable_assignment``.
3120
3121.. code-block:: objc
3122
3123 @interface MyClass : NSObject {}
3124 @property (readonly) id A;
3125 - (void) fAnnotated __attribute__((
3126     annotate("objc_no_direct_instance_variable_assignment")));
3127 - (void) fNotAnnotated;
3128 @end
3129
3130 @implementation MyClass
3131 - (void) fAnnotated {
3132   _A = 0; // warn
3133 }
3134 - (void) fNotAnnotated {
3135   _A = 0; // no warn
3136 }
3137 @end
3138
3139
3140.. _alpha-osx-cocoa-InstanceVariableInvalidation:
3141
3142alpha.osx.cocoa.InstanceVariableInvalidation (ObjC)
3143"""""""""""""""""""""""""""""""""""""""""""""""""""
3144Check that the invalidatable instance variables are
3145invalidated in the methods annotated with objc_instance_variable_invalidator.
3146
3147.. code-block:: objc
3148
3149 @protocol Invalidation <NSObject>
3150 - (void) invalidate
3151   __attribute__((annotate("objc_instance_variable_invalidator")));
3152 @end
3153
3154 @interface InvalidationImpObj : NSObject <Invalidation>
3155 @end
3156
3157 @interface SubclassInvalidationImpObj : InvalidationImpObj {
3158   InvalidationImpObj *var;
3159 }
3160 - (void)invalidate;
3161 @end
3162
3163 @implementation SubclassInvalidationImpObj
3164 - (void) invalidate {}
3165 @end
3166 // warn: var needs to be invalidated or set to nil
3167
3168.. _alpha-osx-cocoa-MissingInvalidationMethod:
3169
3170alpha.osx.cocoa.MissingInvalidationMethod (ObjC)
3171""""""""""""""""""""""""""""""""""""""""""""""""
3172Check that the invalidation methods are present in classes that contain invalidatable instance variables.
3173
3174.. code-block:: objc
3175
3176 @protocol Invalidation <NSObject>
3177 - (void)invalidate
3178   __attribute__((annotate("objc_instance_variable_invalidator")));
3179 @end
3180
3181 @interface NeedInvalidation : NSObject <Invalidation>
3182 @end
3183
3184 @interface MissingInvalidationMethodDecl : NSObject {
3185   NeedInvalidation *Var; // warn
3186 }
3187 @end
3188
3189 @implementation MissingInvalidationMethodDecl
3190 @end
3191
3192.. _alpha-osx-cocoa-localizability-PluralMisuseChecker:
3193
3194alpha.osx.cocoa.localizability.PluralMisuseChecker (ObjC)
3195"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
3196Warns against using one vs. many plural pattern in code when generating localized strings.
3197
3198.. code-block:: objc
3199
3200 NSString *reminderText =
3201   NSLocalizedString(@"None", @"Indicates no reminders");
3202 if (reminderCount == 1) {
3203   // Warning: Plural cases are not supported across all languages.
3204   // Use a .stringsdict file instead
3205   reminderText =
3206     NSLocalizedString(@"1 Reminder", @"Indicates single reminder");
3207 } else if (reminderCount >= 2) {
3208   // Warning: Plural cases are not supported across all languages.
3209   // Use a .stringsdict file instead
3210   reminderText =
3211     [NSString stringWithFormat:
3212       NSLocalizedString(@"%@ Reminders", @"Indicates multiple reminders"),
3213         reminderCount];
3214 }
3215
3216alpha.security
3217^^^^^^^^^^^^^^
3218
3219.. _alpha-security-ArrayBound:
3220
3221alpha.security.ArrayBound (C)
3222"""""""""""""""""""""""""""""
3223Warn about buffer overflows (older checker).
3224
3225.. code-block:: c
3226
3227 void test() {
3228   char *s = "";
3229   char c = s[1]; // warn
3230 }
3231
3232 struct seven_words {
3233   int c[7];
3234 };
3235
3236 void test() {
3237   struct seven_words a, *p;
3238   p = &a;
3239   p[0] = a;
3240   p[1] = a;
3241   p[2] = a; // warn
3242 }
3243
3244 // note: requires unix.Malloc or
3245 // alpha.unix.MallocWithAnnotations checks enabled.
3246 void test() {
3247   int *p = malloc(12);
3248   p[3] = 4; // warn
3249 }
3250
3251 void test() {
3252   char a[2];
3253   int *b = (int*)a;
3254   b[1] = 3; // warn
3255 }
3256
3257.. _alpha-security-ArrayBoundV2:
3258
3259alpha.security.ArrayBoundV2 (C)
3260"""""""""""""""""""""""""""""""
3261Warn about buffer overflows (newer checker).
3262
3263.. code-block:: c
3264
3265 void test() {
3266   char *s = "";
3267   char c = s[1]; // warn
3268 }
3269
3270 void test() {
3271   int buf[100];
3272   int *p = buf;
3273   p = p + 99;
3274   p[1] = 1; // warn
3275 }
3276
3277 // note: compiler has internal check for this.
3278 // Use -Wno-array-bounds to suppress compiler warning.
3279 void test() {
3280   int buf[100][100];
3281   buf[0][-1] = 1; // warn
3282 }
3283
3284 // note: requires optin.taint check turned on.
3285 void test() {
3286   char s[] = "abc";
3287   int x = getchar();
3288   char c = s[x]; // warn: index is tainted
3289 }
3290
3291.. _alpha-security-ReturnPtrRange:
3292
3293alpha.security.ReturnPtrRange (C)
3294"""""""""""""""""""""""""""""""""
3295Check for an out-of-bound pointer being returned to callers.
3296
3297.. code-block:: c
3298
3299 static int A[10];
3300
3301 int *test() {
3302   int *p = A + 10;
3303   return p; // warn
3304 }
3305
3306 int test(void) {
3307   int x;
3308   return x; // warn: undefined or garbage returned
3309 }
3310
3311
3312alpha.security.cert
3313^^^^^^^^^^^^^^^^^^^
3314
3315SEI CERT checkers which tries to find errors based on their `C coding rules <https://wiki.sei.cmu.edu/confluence/display/c/2+Rules>`_.
3316
3317alpha.unix
3318^^^^^^^^^^
3319
3320.. _alpha-unix-PthreadLock:
3321
3322alpha.unix.PthreadLock (C)
3323""""""""""""""""""""""""""
3324Simple lock -> unlock checker.
3325Applies to: ``pthread_mutex_lock, pthread_rwlock_rdlock, pthread_rwlock_wrlock, lck_mtx_lock, lck_rw_lock_exclusive``
3326``lck_rw_lock_shared, pthread_mutex_trylock, pthread_rwlock_tryrdlock, pthread_rwlock_tryrwlock, lck_mtx_try_lock,
3327lck_rw_try_lock_exclusive, lck_rw_try_lock_shared, pthread_mutex_unlock, pthread_rwlock_unlock, lck_mtx_unlock, lck_rw_done``.
3328
3329
3330.. code-block:: c
3331
3332 pthread_mutex_t mtx;
3333
3334 void test() {
3335   pthread_mutex_lock(&mtx);
3336   pthread_mutex_lock(&mtx);
3337     // warn: this lock has already been acquired
3338 }
3339
3340 lck_mtx_t lck1, lck2;
3341
3342 void test() {
3343   lck_mtx_lock(&lck1);
3344   lck_mtx_lock(&lck2);
3345   lck_mtx_unlock(&lck1);
3346     // warn: this was not the most recently acquired lock
3347 }
3348
3349 lck_mtx_t lck1, lck2;
3350
3351 void test() {
3352   if (lck_mtx_try_lock(&lck1) == 0)
3353     return;
3354
3355   lck_mtx_lock(&lck2);
3356   lck_mtx_unlock(&lck1);
3357     // warn: this was not the most recently acquired lock
3358 }
3359
3360.. _alpha-unix-SimpleStream:
3361
3362alpha.unix.SimpleStream (C)
3363"""""""""""""""""""""""""""
3364Check for misuses of stream APIs. Check for misuses of stream APIs: ``fopen, fclose``
3365(demo checker, the subject of the demo (`Slides <https://llvm.org/devmtg/2012-11/Zaks-Rose-Checker24Hours.pdf>`_ ,
3366`Video <https://youtu.be/kdxlsP5QVPw>`_) by Anna Zaks and Jordan Rose presented at the
3367`2012 LLVM Developers' Meeting <https://llvm.org/devmtg/2012-11/>`_).
3368
3369.. code-block:: c
3370
3371 void test() {
3372   FILE *F = fopen("myfile.txt", "w");
3373 } // warn: opened file is never closed
3374
3375 void test() {
3376   FILE *F = fopen("myfile.txt", "w");
3377
3378   if (F)
3379     fclose(F);
3380
3381   fclose(F); // warn: closing a previously closed file stream
3382 }
3383
3384.. _alpha-unix-cstring-BufferOverlap:
3385
3386alpha.unix.cstring.BufferOverlap (C)
3387""""""""""""""""""""""""""""""""""""
3388Checks for overlap in two buffer arguments. Applies to:  ``memcpy, mempcpy, wmemcpy, wmempcpy``.
3389
3390.. code-block:: c
3391
3392 void test() {
3393   int a[4] = {0};
3394   memcpy(a + 2, a + 1, 8); // warn
3395 }
3396
3397.. _alpha-unix-cstring-OutOfBounds:
3398
3399alpha.unix.cstring.OutOfBounds (C)
3400""""""""""""""""""""""""""""""""""
3401Check for out-of-bounds access in string functions, such as:
3402``memcpy, bcopy, strcpy, strncpy, strcat, strncat, memmove, memcmp, memset`` and more.
3403
3404This check also works with string literals, except there is a known bug in that
3405the analyzer cannot detect embedded NULL characters when determining the string length.
3406
3407.. code-block:: c
3408
3409 void test1() {
3410   const char str[] = "Hello world";
3411   char buffer[] = "Hello world";
3412   memcpy(buffer, str, sizeof(str) + 1); // warn
3413 }
3414
3415 void test2() {
3416   const char str[] = "Hello world";
3417   char buffer[] = "Helloworld";
3418   memcpy(buffer, str, sizeof(str)); // warn
3419 }
3420
3421.. _alpha-unix-cstring-UninitializedRead:
3422
3423alpha.unix.cstring.UninitializedRead (C)
3424""""""""""""""""""""""""""""""""""""""""
3425Check for uninitialized reads from common memory copy/manipulation functions such as:
3426 ``memcpy, mempcpy, memmove, memcmp, strcmp, strncmp, strcpy, strlen, strsep`` and many more.
3427
3428.. code-block:: c
3429
3430 void test() {
3431  char src[10];
3432  char dst[5];
3433  memcpy(dst,src,sizeof(dst)); // warn: Bytes string function accesses uninitialized/garbage values
3434 }
3435
3436Limitations:
3437
3438   - Due to limitations of the memory modeling in the analyzer, one can likely
3439     observe a lot of false-positive reports like this:
3440
3441      .. code-block:: c
3442
3443        void false_positive() {
3444          int src[] = {1, 2, 3, 4};
3445          int dst[5] = {0};
3446          memcpy(dst, src, 4 * sizeof(int)); // false-positive:
3447          // The 'src' buffer was correctly initialized, yet we cannot conclude
3448          // that since the analyzer could not see a direct initialization of the
3449          // very last byte of the source buffer.
3450        }
3451
3452     More details at the corresponding `GitHub issue <https://github.com/llvm/llvm-project/issues/43459>`_.
3453
3454alpha.WebKit
3455^^^^^^^^^^^^
3456
3457.. _alpha-webkit-NoUncheckedPtrMemberChecker:
3458
3459alpha.webkit.MemoryUnsafeCastChecker
3460""""""""""""""""""""""""""""""""""""""
3461Check for all casts from a base type to its derived type as these might be memory-unsafe.
3462
3463Example:
3464
3465.. code-block:: cpp
3466
3467    class Base { };
3468    class Derived : public Base { };
3469
3470    void f(Base* base) {
3471        Derived* derived = static_cast<Derived*>(base); // ERROR
3472    }
3473
3474For all cast operations (C-style casts, static_cast, reinterpret_cast, dynamic_cast), if the source type a `Base*` and the destination type is `Derived*`, where `Derived` inherits from `Base`, the static analyzer should signal an error.
3475
3476This applies to:
3477
3478- C structs, C++ structs and classes, and Objective-C classes and protocols.
3479- Pointers and references.
3480- Inside template instantiations and macro expansions that are visible to the compiler.
3481
3482For types like this, instead of using built in casts, the programmer will use helper functions that internally perform the appropriate type check and disable static analysis.
3483
3484alpha.webkit.NoUncheckedPtrMemberChecker
3485""""""""""""""""""""""""""""""""""""""""
3486Raw pointers and references to an object which supports CheckedPtr or CheckedRef can't be used as class members. Only CheckedPtr, CheckedRef, RefPtr, or Ref are allowed.
3487
3488.. code-block:: cpp
3489
3490 struct CheckableObj {
3491   void incrementCheckedPtrCount() {}
3492   void decrementCheckedPtrCount() {}
3493 };
3494
3495 struct Foo {
3496   CheckableObj* ptr; // warn
3497   CheckableObj& ptr; // warn
3498   // ...
3499 };
3500
3501See `WebKit Guidelines for Safer C++ Programming <https://github.com/WebKit/WebKit/wiki/Safer-CPP-Guidelines>`_ for details.
3502
3503.. _alpha-webkit-UncountedCallArgsChecker:
3504
3505alpha.webkit.UncountedCallArgsChecker
3506"""""""""""""""""""""""""""""""""""""
3507The goal of this rule is to make sure that lifetime of any dynamically allocated ref-countable object passed as a call argument spans past the end of the call. This applies to call to any function, method, lambda, function pointer or functor. Ref-countable types aren't supposed to be allocated on stack so we check arguments for parameters of raw pointers and references to uncounted types.
3508
3509Here are some examples of situations that we warn about as they *might* be potentially unsafe. The logic is that either we're able to guarantee that an argument is safe or it's considered if not a bug then bug-prone.
3510
3511  .. code-block:: cpp
3512
3513    RefCountable* provide_uncounted();
3514    void consume(RefCountable*);
3515
3516    // In these cases we can't make sure callee won't directly or indirectly call `deref()` on the argument which could make it unsafe from such point until the end of the call.
3517
3518    void foo1() {
3519      consume(provide_uncounted()); // warn
3520    }
3521
3522    void foo2() {
3523      RefCountable* uncounted = provide_uncounted();
3524      consume(uncounted); // warn
3525    }
3526
3527Although we are enforcing member variables to be ref-counted by `webkit.NoUncountedMemberChecker` any method of the same class still has unrestricted access to these. Since from a caller's perspective we can't guarantee a particular member won't get modified by callee (directly or indirectly) we don't consider values obtained from members safe.
3528
3529Note: It's likely this heuristic could be made more precise with fewer false positives - for example calls to free functions that don't have any parameter other than the pointer should be safe as the callee won't be able to tamper with the member unless it's a global variable.
3530
3531  .. code-block:: cpp
3532
3533    struct Foo {
3534      RefPtr<RefCountable> member;
3535      void consume(RefCountable*) { /* ... */ }
3536      void bugprone() {
3537        consume(member.get()); // warn
3538      }
3539    };
3540
3541The implementation of this rule is a heuristic - we define a whitelist of kinds of values that are considered safe to be passed as arguments. If we can't prove an argument is safe it's considered an error.
3542
3543Allowed kinds of arguments:
3544
3545- values obtained from ref-counted objects (including temporaries as those survive the call too)
3546
3547  .. code-block:: cpp
3548
3549    RefCountable* provide_uncounted();
3550    void consume(RefCountable*);
3551
3552    void foo() {
3553      RefPtr<RefCountable> rc = makeRef(provide_uncounted());
3554      consume(rc.get()); // ok
3555      consume(makeRef(provide_uncounted()).get()); // ok
3556    }
3557
3558- forwarding uncounted arguments from caller to callee
3559
3560  .. code-block:: cpp
3561
3562    void foo(RefCountable& a) {
3563      bar(a); // ok
3564    }
3565
3566  Caller of ``foo()`` is responsible for  ``a``'s lifetime.
3567
3568- ``this`` pointer
3569
3570  .. code-block:: cpp
3571
3572    void Foo::foo() {
3573      baz(this);  // ok
3574    }
3575
3576  Caller of ``foo()`` is responsible for keeping the memory pointed to by ``this`` pointer safe.
3577
3578- constants
3579
3580  .. code-block:: cpp
3581
3582    foo(nullptr, NULL, 0); // ok
3583
3584We also define a set of safe transformations which if passed a safe value as an input provide (usually it's the return value) a safe value (or an object that provides safe values). This is also a heuristic.
3585
3586- constructors of ref-counted types (including factory methods)
3587- getters of ref-counted types
3588- member overloaded operators
3589- casts
3590- unary operators like ``&`` or ``*``
3591
3592alpha.webkit.UncheckedCallArgsChecker
3593"""""""""""""""""""""""""""""""""""""
3594The goal of this rule is to make sure that lifetime of any dynamically allocated CheckedPtr capable object passed as a call argument keeps its memory region past the end of the call. This applies to call to any function, method, lambda, function pointer or functor. CheckedPtr capable objects aren't supposed to be allocated on stack so we check arguments for parameters of raw pointers and references to unchecked types.
3595
3596The rules of when to use and not to use CheckedPtr / CheckedRef are same as alpha.webkit.UncountedCallArgsChecker for ref-counted objects.
3597
3598alpha.webkit.UncountedLocalVarsChecker
3599""""""""""""""""""""""""""""""""""""""
3600The goal of this rule is to make sure that any uncounted local variable is backed by a ref-counted object with lifetime that is strictly larger than the scope of the uncounted local variable. To be on the safe side we require the scope of an uncounted variable to be embedded in the scope of ref-counted object that backs it.
3601
3602These are examples of cases that we consider safe:
3603
3604  .. code-block:: cpp
3605
3606    void foo1() {
3607      RefPtr<RefCountable> counted;
3608      // The scope of uncounted is EMBEDDED in the scope of counted.
3609      {
3610        RefCountable* uncounted = counted.get(); // ok
3611      }
3612    }
3613
3614    void foo2(RefPtr<RefCountable> counted_param) {
3615      RefCountable* uncounted = counted_param.get(); // ok
3616    }
3617
3618    void FooClass::foo_method() {
3619      RefCountable* uncounted = this; // ok
3620    }
3621
3622Here are some examples of situations that we warn about as they *might* be potentially unsafe. The logic is that either we're able to guarantee that a local variable is safe or it's considered unsafe.
3623
3624  .. code-block:: cpp
3625
3626    void foo1() {
3627      RefCountable* uncounted = new RefCountable; // warn
3628    }
3629
3630    RefCountable* global_uncounted;
3631    void foo2() {
3632      RefCountable* uncounted = global_uncounted; // warn
3633    }
3634
3635    void foo3() {
3636      RefPtr<RefCountable> counted;
3637      // The scope of uncounted is not EMBEDDED in the scope of counted.
3638      RefCountable* uncounted = counted.get(); // warn
3639    }
3640
3641alpha.webkit.UncheckedLocalVarsChecker
3642""""""""""""""""""""""""""""""""""""""
3643The goal of this rule is to make sure that any unchecked local variable is backed by a CheckedPtr or CheckedRef with lifetime that is strictly larger than the scope of the unchecked local variable. To be on the safe side we require the scope of an unchecked variable to be embedded in the scope of CheckedPtr/CheckRef object that backs it.
3644
3645These are examples of cases that we consider safe:
3646
3647  .. code-block:: cpp
3648
3649    void foo1() {
3650      CheckedPtr<RefCountable> counted;
3651      // The scope of uncounted is EMBEDDED in the scope of counted.
3652      {
3653        RefCountable* uncounted = counted.get(); // ok
3654      }
3655    }
3656
3657    void foo2(CheckedPtr<RefCountable> counted_param) {
3658      RefCountable* uncounted = counted_param.get(); // ok
3659    }
3660
3661    void FooClass::foo_method() {
3662      RefCountable* uncounted = this; // ok
3663    }
3664
3665Here are some examples of situations that we warn about as they *might* be potentially unsafe. The logic is that either we're able to guarantee that a local variable is safe or it's considered unsafe.
3666
3667  .. code-block:: cpp
3668
3669    void foo1() {
3670      RefCountable* uncounted = new RefCountable; // warn
3671    }
3672
3673    RefCountable* global_uncounted;
3674    void foo2() {
3675      RefCountable* uncounted = global_uncounted; // warn
3676    }
3677
3678    void foo3() {
3679      RefPtr<RefCountable> counted;
3680      // The scope of uncounted is not EMBEDDED in the scope of counted.
3681      RefCountable* uncounted = counted.get(); // warn
3682    }
3683
3684Debug Checkers
3685---------------
3686
3687.. _debug-checkers:
3688
3689
3690debug
3691^^^^^
3692
3693Checkers used for debugging the analyzer.
3694:doc:`developer-docs/DebugChecks` page contains a detailed description.
3695
3696.. _debug-AnalysisOrder:
3697
3698debug.AnalysisOrder
3699"""""""""""""""""""
3700Print callbacks that are called during analysis in order.
3701
3702.. _debug-ConfigDumper:
3703
3704debug.ConfigDumper
3705""""""""""""""""""
3706Dump config table.
3707
3708.. _debug-DumpCFG Display:
3709
3710debug.DumpCFG Display
3711"""""""""""""""""""""
3712Control-Flow Graphs.
3713
3714.. _debug-DumpCallGraph:
3715
3716debug.DumpCallGraph
3717"""""""""""""""""""
3718Display Call Graph.
3719
3720.. _debug-DumpCalls:
3721
3722debug.DumpCalls
3723"""""""""""""""
3724Print calls as they are traversed by the engine.
3725
3726.. _debug-DumpDominators:
3727
3728debug.DumpDominators
3729""""""""""""""""""""
3730Print the dominance tree for a given CFG.
3731
3732.. _debug-DumpLiveVars:
3733
3734debug.DumpLiveVars
3735""""""""""""""""""
3736Print results of live variable analysis.
3737
3738.. _debug-DumpTraversal:
3739
3740debug.DumpTraversal
3741"""""""""""""""""""
3742Print branch conditions as they are traversed by the engine.
3743
3744.. _debug-ExprInspection:
3745
3746debug.ExprInspection
3747""""""""""""""""""""
3748Check the analyzer's understanding of expressions.
3749
3750.. _debug-Stats:
3751
3752debug.Stats
3753"""""""""""
3754Emit warnings with analyzer statistics.
3755
3756.. _debug-TaintTest:
3757
3758debug.TaintTest
3759"""""""""""""""
3760Mark tainted symbols as such.
3761
3762.. _debug-ViewCFG:
3763
3764debug.ViewCFG
3765"""""""""""""
3766View Control-Flow Graphs using GraphViz.
3767
3768.. _debug-ViewCallGraph:
3769
3770debug.ViewCallGraph
3771"""""""""""""""""""
3772View Call Graph using GraphViz.
3773
3774.. _debug-ViewExplodedGraph:
3775
3776debug.ViewExplodedGraph
3777"""""""""""""""""""""""
3778View Exploded Graphs using GraphViz.
3779