[M68k] always use movem for register spills (#106715)
[llvm-project.git] / clang / docs / analyzer / checkers.rst
blobe093b2d672a74e308efa1e235eebae4558acd120
1 ==================
2 Available Checkers
3 ==================
5 The analyzer performs checks that are categorized into families or "checkers".
7 The default set of checkers covers a variety of checks targeted at finding security and API usage bugs,
8 dead code, and other logic errors. See the :ref:`default-checkers` checkers list below.
10 In addition to these, the analyzer contains a number of :ref:`alpha-checkers` (aka *alpha* checkers).
11 These checkers are under development and are switched off by default. They may crash or emit a higher number of false positives.
13 The :ref:`debug-checkers` package contains checkers for analyzer developers for debugging purposes.
15 .. contents:: Table of Contents
16    :depth: 4
19 .. _default-checkers:
21 Default Checkers
22 ----------------
24 .. _core-checkers:
26 core
27 ^^^^
28 Models core language features and contains general-purpose checkers such as division by zero,
29 null pointer dereference, usage of uninitialized values, etc.
30 *These checkers must be always switched on as other checker rely on them.*
32 .. _core-BitwiseShift:
34 core.BitwiseShift (C, C++)
35 """"""""""""""""""""""""""
37 Finds undefined behavior caused by the bitwise left- and right-shift operator
38 operating on integer types.
40 By default, this checker only reports situations when the right operand is
41 either negative or larger than the bit width of the type of the left operand;
42 these are logically unsound.
44 Moreover, if the pedantic mode is activated by
45 ``-analyzer-config core.BitwiseShift:Pedantic=true``, then this checker also
46 reports situations where the _left_ operand of a shift operator is negative or
47 overflow occurs during the right shift of a signed value. (Most compilers
48 handle these predictably, but the C standard and the C++ standards before C++20
49 say that they're undefined behavior. In the C++20 standard these constructs are
50 well-defined, so activating pedantic mode in C++20 has no effect.)
52 **Examples**
54 .. code-block:: cpp
56  static_assert(sizeof(int) == 4, "assuming 32-bit int")
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  }
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  }
78 **Solution**
80 Ensure the shift operands are in proper range before shifting.
82 .. _core-CallAndMessage:
84 core.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).
88 .. literalinclude:: checkers/callandmessage_example.c
89     :language: objc
91 .. _core-DivideZero:
93 core.DivideZero (C, C++, ObjC)
94 """"""""""""""""""""""""""""""
95  Check for division by zero.
97 .. literalinclude:: checkers/dividezero_example.c
98     :language: c
100 .. _core-NonNullParamChecker:
102 core.NonNullParamChecker (C, C++, ObjC)
103 """""""""""""""""""""""""""""""""""""""
104 Check for null pointers passed as arguments to a function whose arguments are references or marked with the 'nonnull' attribute.
106 .. code-block:: cpp
108  int f(int *p) __attribute__((nonnull));
110  void test(int *p) {
111    if (!p)
112      f(p); // warn
115 .. _core-NullDereference:
117 core.NullDereference (C, C++, ObjC)
118 """""""""""""""""""""""""""""""""""
119 Check for dereferences of null pointers.
121 This checker specifically does
122 not report null pointer dereferences for x86 and x86-64 targets when the
123 address space is 256 (x86 GS Segment), 257 (x86 FS Segment), or 258 (x86 SS
124 segment). See `X86/X86-64 Language Extensions
125 <https://clang.llvm.org/docs/LanguageExtensions.html#memory-references-to-specified-segments>`__
126 for reference.
128 The ``SuppressAddressSpaces`` option suppresses
129 warnings for null dereferences of all pointers with address spaces. You can
130 disable this behavior with the option
131 ``-analyzer-config core.NullDereference:SuppressAddressSpaces=false``.
132 *Defaults to true*.
134 .. code-block:: objc
136  // C
137  void test(int *p) {
138    if (p)
139      return;
141    int x = p[0]; // warn
144  // C
145  void test(int *p) {
146    if (!p)
147      *p = 0; // warn
150  // C++
151  class C {
152  public:
153    int x;
154  };
156  void test() {
157    C *pc = 0;
158    int k = pc->x; // warn
161  // Objective-C
162  @interface MyClass {
163  @public
164    int x;
166  @end
168  void test() {
169    MyClass *obj = 0;
170    obj->x = 1; // warn
173 .. _core-StackAddressEscape:
175 core.StackAddressEscape (C)
176 """""""""""""""""""""""""""
177 Check that addresses to stack memory do not escape the function.
179 .. code-block:: c
181  char const *p;
183  void test() {
184    char const str[] = "string";
185    p = str; // warn
188  void* test() {
189     return __builtin_alloca(12); // warn
192  void test() {
193    static int *x;
194    int y;
195    x = &y; // warn
199 .. _core-UndefinedBinaryOperatorResult:
201 core.UndefinedBinaryOperatorResult (C)
202 """"""""""""""""""""""""""""""""""""""
203 Check for undefined results of binary operators.
205 .. code-block:: c
207  void test() {
208    int x;
209    int y = x + 1; // warn: left operand is garbage
212 .. _core-VLASize:
214 core.VLASize (C)
215 """"""""""""""""
216 Check for declarations of Variable Length Arrays (VLA) of undefined, zero or negative
217 size.
219 .. code-block:: c
221  void test() {
222    int x;
223    int vla1[x]; // warn: garbage as size
226  void test() {
227    int x = 0;
228    int vla2[x]; // warn: zero size
232 The checker also gives warning if the `TaintPropagation` checker is switched on
233 and an unbound, attacker controlled (tainted) value is used to define
234 the size of the VLA.
236 .. code-block:: c
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
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.
253 .. _core-uninitialized-ArraySubscript:
255 core.uninitialized.ArraySubscript (C)
256 """""""""""""""""""""""""""""""""""""
257 Check for uninitialized values used as array subscripts.
259 .. code-block:: c
261  void test() {
262    int i, a[10];
263    int x = a[i]; // warn: array subscript is undefined
266 .. _core-uninitialized-Assign:
268 core.uninitialized.Assign (C)
269 """""""""""""""""""""""""""""
270 Check for assigning uninitialized values.
272 .. code-block:: c
274  void test() {
275    int x;
276    x |= 1; // warn: left expression is uninitialized
279 .. _core-uninitialized-Branch:
281 core.uninitialized.Branch (C)
282 """""""""""""""""""""""""""""
283 Check for uninitialized values used as branch conditions.
285 .. code-block:: c
287  void test() {
288    int x;
289    if (x) // warn
290      return;
293 .. _core-uninitialized-CapturedBlockVariable:
295 core.uninitialized.CapturedBlockVariable (C)
296 """"""""""""""""""""""""""""""""""""""""""""
297 Check for blocks that capture uninitialized values.
299 .. code-block:: c
301  void test() {
302    int x;
303    ^{ int y = x; }(); // warn
306 .. _core-uninitialized-UndefReturn:
308 core.uninitialized.UndefReturn (C)
309 """"""""""""""""""""""""""""""""""
310 Check for uninitialized values being returned to the caller.
312 .. code-block:: c
314  int test() {
315    int x;
316    return x; // warn
319 .. _core-uninitialized-NewArraySize:
321 core.uninitialized.NewArraySize (C++)
322 """""""""""""""""""""""""""""""""""""
324 Check if the element count in new[] is garbage or undefined.
326 .. code-block:: cpp
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   }
335 .. _cplusplus-checkers:
338 cplusplus
339 ^^^^^^^^^
341 C++ Checkers.
343 .. _cplusplus-ArrayDelete:
345 cplusplus.ArrayDelete (C++)
346 """""""""""""""""""""""""""
348 Reports destructions of arrays of polymorphic objects that are destructed as
349 their base class. If the dynamic type of the array is different from its static
350 type, calling `delete[]` is undefined.
352 This 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>`_.
354 .. code-block:: cpp
356  class Base {
357  public:
358    virtual ~Base() {}
359  };
360  class Derived : public Base {};
362  Base *create() {
363    Base *x = new Derived[10]; // note: Casting from 'Derived' to 'Base' here
364    return x;
367  void foo() {
368    Base *x = create();
369    delete[] x; // warn: Deleting an array of 'Derived' objects as their base class 'Base' is undefined
372 **Limitations**
374 The checker does not emit note tags when casting to and from reference types,
375 even though the pointer values are tracked across references.
377 .. code-block:: cpp
379  void foo() {
380    Derived *d = new Derived[10];
381    Derived &dref = *d;
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
388 .. _cplusplus-InnerPointer:
390 cplusplus.InnerPointer (C++)
391 """"""""""""""""""""""""""""
392 Check for inner pointers of C++ containers used after re/deallocation.
394 Many container methods in the C++ standard library are known to invalidate
395 "references" (including actual references, iterators and raw pointers) to
396 elements of the container. Using such references after they are invalidated
397 causes undefined behavior, which is a common source of memory errors in C++ that
398 this checker is capable of finding.
400 The checker is currently limited to ``std::string`` objects and doesn't
401 recognize some of the more sophisticated approaches to passing unowned pointers
402 around, such as ``std::string_view``.
404 .. code-block:: cpp
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
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
419 .. _cplusplus-Move:
421 cplusplus.Move (C++)
422 """"""""""""""""""""
423 Find use-after-move bugs in C++. This includes method calls on moved-from
424 objects, assignment of a moved-from object, and repeated move of a moved-from
425 object.
427 .. code-block:: cpp
429  struct A {
430    void foo() {}
431  };
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'
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
445  void f3() {
446    A a;
447    A b = std::move(a);
448    b = a; // warn: copy of moved-from object
451 The checker option ``WarnOn`` controls on what objects the use-after-move is
452 checked:
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.
464 Default value is ``KnownsAndLocals``.
466 Calls of methods named ``empty()`` or ``isEmpty()`` are allowed on moved-from
467 objects because these methods are considered as move-safe. Functions called
468 ``reset()``, ``destroy()``, ``clear()``, ``assign``, ``resize``,  ``shrink`` are
469 treated as state-reset functions and are allowed on moved-from objects, these
470 make the object valid again. This applies to any type of object (not only STL
471 ones).
473 .. _cplusplus-NewDelete:
475 cplusplus.NewDelete (C++)
476 """""""""""""""""""""""""
477 Check for double-free and use-after-free problems. Traces memory managed by new/delete.
479 Custom allocation/deallocation functions can be defined using
480 :ref:`ownership attributes<analyzer-ownership-attrs>`.
482 .. literalinclude:: checkers/newdelete_example.cpp
483     :language: cpp
485 .. _cplusplus-NewDeleteLeaks:
487 cplusplus.NewDeleteLeaks (C++)
488 """"""""""""""""""""""""""""""
489 Check for memory leaks. Traces memory managed by new/delete.
491 Custom allocation/deallocation functions can be defined using
492 :ref:`ownership attributes<analyzer-ownership-attrs>`.
494 .. code-block:: cpp
496  void test() {
497    int *p = new int;
498  } // warn
500 .. _cplusplus-PlacementNew:
502 cplusplus.PlacementNew (C++)
503 """"""""""""""""""""""""""""
504 Check if default placement new is provided with pointers to sufficient storage capacity.
506 .. code-block:: cpp
508  #include <new>
510  void f() {
511    short s;
512    long *lp = ::new (&s) long; // warn
515 .. _cplusplus-SelfAssignment:
517 cplusplus.SelfAssignment (C++)
518 """"""""""""""""""""""""""""""
519 Checks C++ copy and move assignment operators for self assignment.
521 .. _cplusplus-StringChecker:
523 cplusplus.StringChecker (C++)
524 """""""""""""""""""""""""""""
525 Checks std::string operations.
527 Checks if the cstring pointer from which the ``std::string`` object is
528 constructed is ``NULL`` or not.
529 If the checker cannot reason about the nullness of the pointer it will assume
530 that it was non-null to satisfy the precondition of the constructor.
532 This checker is capable of checking the `SEI CERT C++ coding rule STR51-CPP.
533 Do not attempt to create a std::string from a null pointer
534 <https://wiki.sei.cmu.edu/confluence/x/E3s-BQ>`__.
536 .. code-block:: cpp
538  #include <string>
540  void f(const char *p) {
541    if (!p) {
542      std::string msg(p); // warn: The parameter must not be null
543    }
546 .. _deadcode-checkers:
548 deadcode
549 ^^^^^^^^
551 Dead Code Checkers.
553 .. _deadcode-DeadStores:
555 deadcode.DeadStores (C)
556 """""""""""""""""""""""
557 Check for values stored to variables that are never read afterwards.
559 .. code-block:: c
561  void test() {
562    int x;
563    x = 1; // warn
566 The ``WarnForDeadNestedAssignments`` option enables the checker to emit
567 warnings for nested dead assignments. You can disable with the
568 ``-analyzer-config deadcode.DeadStores:WarnForDeadNestedAssignments=false``.
569 *Defaults to true*.
571 Would warn for this e.g.:
572 if ((y = make_int())) {
575 .. _nullability-checkers:
577 nullability
578 ^^^^^^^^^^^
580 Checkers (mostly Objective C) that warn for null pointer passing and dereferencing errors.
582 .. _nullability-NullPassedToNonnull:
584 nullability.NullPassedToNonnull (ObjC)
585 """"""""""""""""""""""""""""""""""""""
586 Warns when a null pointer is passed to a pointer which has a _Nonnull type.
588 .. code-block:: objc
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];
595 .. _nullability-NullReturnedFromNonnull:
597 nullability.NullReturnedFromNonnull (C, C++, ObjC)
598 """"""""""""""""""""""""""""""""""""""""""""""""""
599 Warns when a null pointer is returned from a function that has _Nonnull return type.
601 .. code-block:: objc
603  - (nonnull id)firstChild {
604    id result = nil;
605    if ([_children count] > 0)
606      result = _children[0];
608    // Warning: nil returned from a method that is expected
609    // to return a non-null value
610    return result;
613 Warns when a null pointer is returned from a function annotated with ``__attribute__((returns_nonnull))``
615 .. code-block:: cpp
617  int global;
618  __attribute__((returns_nonnull)) void* getPtr(void* p);
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;
629 .. _nullability-NullableDereferenced:
631 nullability.NullableDereferenced (ObjC)
632 """""""""""""""""""""""""""""""""""""""
633 Warns when a nullable pointer is dereferenced.
635 .. code-block:: objc
637  struct LinkedList {
638    int data;
639    struct LinkedList *next;
640  };
642  struct LinkedList * _Nullable getNext(struct LinkedList *l);
644  void updateNextData(struct LinkedList *list, int newData) {
645    struct LinkedList *next = getNext(list);
646    // Warning: Nullable pointer is dereferenced
647    next->data = 7;
650 .. _nullability-NullablePassedToNonnull:
652 nullability.NullablePassedToNonnull (ObjC)
653 """"""""""""""""""""""""""""""""""""""""""
654 Warns when a nullable pointer is passed to a pointer which has a _Nonnull type.
656 .. code-block:: objc
658  typedef struct Dummy { int val; } Dummy;
659  Dummy *_Nullable returnsNullable();
660  void takesNonnull(Dummy *_Nonnull);
662  void test() {
663    Dummy *p = returnsNullable();
664    takesNonnull(p); // warn
667 .. _nullability-NullableReturnedFromNonnull:
669 nullability.NullableReturnedFromNonnull (ObjC)
670 """"""""""""""""""""""""""""""""""""""""""""""
671 Warns when a nullable pointer is returned from a function that has _Nonnull return type.
673 .. _optin-checkers:
675 optin
676 ^^^^^
678 Checkers for portability, performance, optional security and coding style specific rules.
680 .. _optin-core-EnumCastOutOfRange:
682 optin.core.EnumCastOutOfRange (C, C++)
683 """"""""""""""""""""""""""""""""""""""
684 Check for integer to enumeration casts that would produce a value with no
685 corresponding enumerator. This is not necessarily undefined behavior, but can
686 lead to nasty surprises, so projects may decide to use a coding standard that
687 disallows these "unusual" conversions.
689 Note that no warnings are produced when the enum type (e.g. `std::byte`) has no
690 enumerators at all.
692 .. code-block:: cpp
694  enum WidgetKind { A=1, B, C, X=99 };
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
702 **Limitations**
704 This checker does not accept the coding pattern where an enum type is used to
705 store combinations of flag values:
707 .. code-block:: cpp
709  enum AnimalFlags
711      HasClaws   = 1,
712      CanFly     = 2,
713      EatsFish   = 4,
714      Endangered = 8
715  };
717  AnimalFlags operator|(AnimalFlags a, AnimalFlags b)
719      return static_cast<AnimalFlags>(static_cast<int>(a) | static_cast<int>(b));
722  auto flags = HasClaws | CanFly;
724 Projects that use this pattern should not enable this optin checker.
726 .. _optin-cplusplus-UninitializedObject:
728 optin.cplusplus.UninitializedObject (C++)
729 """""""""""""""""""""""""""""""""""""""""
731 This checker reports uninitialized fields in objects created after a constructor
732 call. It doesn't only find direct uninitialized fields, but rather makes a deep
733 inspection of the object, analyzing all of its fields' subfields.
734 The checker regards inherited fields as direct fields, so one will receive
735 warnings for uninitialized inherited data members as well.
737 .. code-block:: cpp
739  // With Pedantic and CheckPointeeInitialization set to true
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'
753    A (B *bptr, char *cptr) : bptr(bptr), cptr(cptr) {}
754  };
756  void f() {
757    A::B b;
758    char c;
759    A a(&b, &c); // warning: 6 uninitialized fields
760   //          after the constructor call
763  // With Pedantic set to false and
764  // CheckPointeeInitialization set to true
765  // (every field is uninitialized)
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;
777    A (B *bptr, char *cptr) : bptr(bptr), cptr(cptr) {}
778  };
780  void f() {
781    A::B b;
782    char c;
783    A a(&b, &c); // no warning
786  // With Pedantic set to true and
787  // CheckPointeeInitialization set to false
788  // (pointees are regarded as initialized)
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;
800    A (B *bptr, char *cptr) : bptr(bptr), cptr(cptr) {}
801  };
803  void f() {
804    A::B b;
805    char c;
806    A a(&b, &c); // warning: 3 uninitialized fields
807   //          after the constructor call
811 **Options**
813 This checker has several options which can be set from command line (e.g.
814 ``-analyzer-config optin.cplusplus.UninitializedObject:Pedantic=true``):
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.
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*.
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*.
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 ""*.
832 .. _optin-cplusplus-VirtualCall:
834 optin.cplusplus.VirtualCall (C++)
835 """""""""""""""""""""""""""""""""
836 Check virtual function calls during construction or destruction.
838 .. code-block:: cpp
840  class A {
841  public:
842    A() {
843      f(); // warn
844    }
845    virtual void f();
846  };
848  class A {
849  public:
850    ~A() {
851      this->f(); // warn
852    }
853    virtual void f();
854  };
856 .. _optin-mpi-MPI-Checker:
858 optin.mpi.MPI-Checker (C)
859 """""""""""""""""""""""""
860 Checks MPI code.
862 .. code-block:: c
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.
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);
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
887 .. _optin-osx-cocoa-localizability-EmptyLocalizationContextChecker:
889 optin.osx.cocoa.localizability.EmptyLocalizationContextChecker (ObjC)
890 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
891 Check that NSLocalizedString macros include a comment for context.
893 .. code-block:: objc
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
902 .. _optin-osx-cocoa-localizability-NonLocalizedStringChecker:
904 optin.osx.cocoa.localizability.NonLocalizedStringChecker (ObjC)
905 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
906 Warns about uses of non-localized NSStrings passed to UI methods expecting localized NSStrings.
908 .. code-block:: objc
910  NSString *alarmText =
911    NSLocalizedString(@"Enabled", @"Indicates alarm is turned on");
912  if (!isEnabled) {
913    alarmText = @"Disabled";
915  UILabel *alarmStateLabel = [[UILabel alloc] init];
917  // Warning: User-facing text should use localized string macro
918  [alarmStateLabel setText:alarmText];
920 .. _optin-performance-GCDAntipattern:
922 optin.performance.GCDAntipattern
923 """"""""""""""""""""""""""""""""
924 Check for performance anti-patterns when using Grand Central Dispatch.
926 .. _optin-performance-Padding:
928 optin.performance.Padding (C, C++, ObjC)
929 """"""""""""""""""""""""""""""""""""""""
930 Check for excessively padded structs.
932 This checker detects structs with excessive padding, which can lead to wasted
933 memory thus decreased performance by reducing the effectiveness of the
934 processor cache. Padding bytes are added by compilers to align data accesses
935 as some processors require data to be aligned to certain boundaries. On others,
936 unaligned data access are possible, but impose significantly larger latencies.
938 To avoid padding bytes, the fields of a struct should be ordered by decreasing
939 by alignment. Usually, its easier to think of the ``sizeof`` of the fields, and
940 ordering the fields by ``sizeof`` would usually also lead to the same optimal
941 layout.
943 In rare cases, one can use the ``#pragma pack(1)`` directive to enforce a packed
944 layout too, but it can significantly increase the access times, so reordering the
945 fields is usually a better solution.
948 .. code-block:: cpp
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);
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);
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);
1000 The ``AllowedPad`` option can be used to specify a threshold for the number
1001 padding bytes raising the warning. If the number of padding bytes of the struct
1002 and the optimal number of padding bytes differ by more than the threshold value,
1003 a warning will be raised.
1005 By default, the ``AllowedPad`` threshold is 24 bytes.
1007 To override this threshold to e.g. 4 bytes, use the
1008 ``-analyzer-config optin.performance.Padding:AllowedPad=4`` option.
1011 .. _optin-portability-UnixAPI:
1013 optin.portability.UnixAPI
1014 """""""""""""""""""""""""
1015 Finds implementation-defined behavior in UNIX/Posix functions.
1018 optin.taint
1019 ^^^^^^^^^^^
1021 Checkers implementing
1022 `taint analysis <https://en.wikipedia.org/wiki/Taint_checking>`_.
1024 .. _optin-taint-GenericTaint:
1026 optin.taint.GenericTaint (C, C++)
1027 """""""""""""""""""""""""""""""""
1029 Taint analysis identifies potential security vulnerabilities where the
1030 attacker can inject malicious data to the program to execute an attack
1031 (privilege escalation, command injection, SQL injection etc.).
1033 The malicious data is injected at the taint source (e.g. ``getenv()`` call)
1034 which is then propagated through function calls and being used as arguments of
1035 sensitive operations, also called as taint sinks (e.g. ``system()`` call).
1037 One can defend against this type of vulnerability by always checking and
1038 sanitizing the potentially malicious, untrusted user input.
1040 The goal of the checker is to discover and show to the user these potential
1041 taint source-sink pairs and the propagation call chain.
1043 The most notable examples of taint sources are:
1045   - data from network
1046   - files or standard input
1047   - environment variables
1048   - data from databases
1050 Let us examine a practical example of a Command Injection attack.
1052 .. code-block:: c
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   }
1064 The program prints the content of any user specified file.
1065 Unfortunately the attacker can execute arbitrary commands
1066 with shell escapes. For example with the following input the `ls` command is also
1067 executed after the contents of `/etc/shadow` is printed.
1068 `Input: /etc/shadow ; ls /`
1070 The analysis implemented in this checker points out this problem.
1072 One can protect against such attack by for example checking if the provided
1073 input refers to a valid file and removing any invalid user input.
1075 .. code-block:: c
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   }
1096 Unfortunately, the checker cannot discover automatically that the programmer
1097 have performed data sanitation, so it still emits the warning.
1099 One can get rid of this superfluous warning by telling by specifying the
1100 sanitation functions in the taint configuration file (see
1101 :doc:`user-docs/TaintAnalysisConfiguration`).
1103 .. code-block:: YAML
1105   Filters:
1106   - Name: sanitizeFileName
1107     Args: [0]
1109 The clang invocation to pass the configuration file location:
1111 .. code-block:: bash
1113   clang  --analyze -Xclang -analyzer-config  -Xclang optin.taint.TaintPropagation:Config=`pwd`/taint_config.yml ...
1115 If you are validating your inputs instead of sanitizing them, or don't want to
1116 mention each sanitizing function in our configuration,
1117 you can use a more generic approach.
1119 Introduce a generic no-op `csa_mark_sanitized(..)` function to
1120 tell the Clang Static Analyzer
1121 that the variable is safe to be used on that analysis path.
1123 .. code-block:: c
1125   // Marking sanitized variables safe.
1126   // No vulnerability anymore, no warning.
1128   // User csa_mark_sanitize function is for the analyzer only
1129   #ifdef __clang_analyzer__
1130     void csa_mark_sanitized(const void *);
1131   #endif
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   }
1149 Similarly to the previous example, you need to
1150 define a `Filter` function in a `YAML` configuration file
1151 and add the `csa_mark_sanitized` function.
1153 .. code-block:: YAML
1155   Filters:
1156   - Name: csa_mark_sanitized
1157     Args: [0]
1159 Then calling `csa_mark_sanitized(X)` will tell the analyzer that `X` is safe to
1160 be used after this point, because its contents are verified. It is the
1161 responsibility of the programmer to ensure that this verification was indeed
1162 correct. Please note that `csa_mark_sanitized` function is only declared and
1163 used during Clang Static Analysis and skipped in (production) builds.
1165 Further examples of injection vulnerabilities this checker can find.
1167 .. code-block:: c
1169   void test() {
1170     char x = getchar(); // 'x' marked as tainted
1171     system(&x); // warn: untrusted data is passed to a system call
1172   }
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
1181     sprintf(buf, s); // warn: untrusted data used as a format string
1182   }
1184 There are built-in sources, propagations and sinks even if no external taint
1185 configuration is provided.
1187 Default 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``
1194 Default 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``
1209 Default sinks:
1210  ``printf``, ``setproctitle``, ``system``, ``popen``, ``execl``, ``execle``,
1211  ``execlp``, ``execv``, ``execvp``, ``execvP``, ``execve``, ``dlopen``
1213 Please note that there are no built-in filter functions.
1215 One can configure their own taint sources, sinks, and propagation rules by
1216 providing 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
1219 taint-related options defined in the config file extend but do not override the
1220 built-in sources, rules, sinks. The format of the external taint configuration
1221 file is not stable, and could change without any notice even in a non-backward
1222 compatible way.
1224 For 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`.
1228 **Configuration**
1230 * `Config`  Specifies the name of the YAML configuration file. The user can
1231   define their own taint sources and sinks.
1233 **Related Guidelines**
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>`_
1244 **Limitations**
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.
1252 .. _optin-taint-TaintedAlloc:
1254 optin.taint.TaintedAlloc (C, C++)
1255 """""""""""""""""""""""""""""""""
1257 This checker warns for cases when the ``size`` parameter of the ``malloc`` ,
1258 ``calloc``, ``realloc``, ``alloca`` or the size parameter of the
1259 array new C++ operator is tainted (potentially attacker controlled).
1260 If an attacker can inject a large value as the size parameter, memory exhaustion
1261 denial of service attack can be carried out.
1263 The analyzer emits warning only if it cannot prove that the size parameter is
1264 within reasonable bounds (``<= SIZE_MAX/4``). This functionality partially
1265 covers 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>`_.
1268 You can silence this warning either by bound checking the ``size`` parameter, or
1269 by explicitly marking the ``size`` parameter as sanitized. See the
1270 :ref:`optin-taint-GenericTaint` checker for an example.
1272 Custom allocation/deallocation functions can be defined using
1273 :ref:`ownership attributes<analyzer-ownership-attrs>`.
1275 .. code-block:: c
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   }
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   }
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   }
1300 .. _optin-taint-TaintedDiv:
1302 optin.taint.TaintedDiv (C, C++, ObjC)
1303 """""""""""""""""""""""""""""""""""""
1304 This checker warns when the denominator in a division
1305 operation is a tainted (potentially attacker controlled) value.
1306 If the attacker can set the denominator to 0, a runtime error can
1307 be triggered. The checker warns when the denominator is a tainted
1308 value and the analyzer cannot prove that it is not 0. This warning
1309 is more pessimistic than the :ref:`core-DivideZero` checker
1310 which warns only when it can prove that the denominator is 0.
1312 .. code-block:: c
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   }
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   }
1328 .. _security-checkers:
1330 security
1331 ^^^^^^^^
1333 Security related checkers.
1335 .. _security-cert-env-InvalidPtr:
1337 security.cert.env.InvalidPtr
1338 """"""""""""""""""""""""""""""""""
1340 Corresponds 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>`_.
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.
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``
1354 .. code-block:: c
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   }
1372   void previous_call_invalidation() {
1373     char *p, *pp;
1375     p = getenv("VAR");
1376     setenv("SOMEVAR", "VALUE", /*overwrite = */1);
1377     // call to 'setenv' may invalidate p
1379     *p;
1380     // dereferencing invalid pointer
1381   }
1384 The ``InvalidatingGetEnv`` option is available for treating ``getenv`` calls as
1385 invalidating. When enabled, the checker issues a warning if ``getenv`` is called
1386 multiple times and their results are used without first creating a copy.
1387 This level of strictness might be considered overly pedantic for the commonly
1388 used ``getenv`` implementations.
1390 To enable this option, use:
1391 ``-analyzer-config security.cert.env.InvalidPtr:InvalidatingGetEnv=true``.
1393 By default, this option is set to *false*.
1395 When this option is enabled, warnings will be generated for scenarios like the
1396 following:
1398 .. code-block:: c
1400   char* p = getenv("VAR");
1401   char* pp = getenv("VAR2"); // assumes this call can invalidate `env`
1402   strlen(p); // warns about accessing invalid ptr
1404 .. _security-FloatLoopCounter:
1406 security.FloatLoopCounter (C)
1407 """""""""""""""""""""""""""""
1408 Warn on using a floating point value as a loop counter (CERT: FLP30-C, FLP30-CPP).
1410 .. code-block:: c
1412  void test() {
1413    for (float x = 0.1f; x <= 1.0f; x += 0.1f) {} // warn
1416 .. _security-insecureAPI-UncheckedReturn:
1418 security.insecureAPI.UncheckedReturn (C)
1419 """"""""""""""""""""""""""""""""""""""""
1420 Warn on uses of functions whose return values must be always checked.
1422 .. code-block:: c
1424  void test() {
1425    setuid(1); // warn
1428 .. _security-insecureAPI-bcmp:
1430 security.insecureAPI.bcmp (C)
1431 """""""""""""""""""""""""""""
1432 Warn on uses of the 'bcmp' function.
1434 .. code-block:: c
1436  void test() {
1437    bcmp(ptr0, ptr1, n); // warn
1440 .. _security-insecureAPI-bcopy:
1442 security.insecureAPI.bcopy (C)
1443 """"""""""""""""""""""""""""""
1444 Warn on uses of the 'bcopy' function.
1446 .. code-block:: c
1448  void test() {
1449    bcopy(src, dst, n); // warn
1452 .. _security-insecureAPI-bzero:
1454 security.insecureAPI.bzero (C)
1455 """"""""""""""""""""""""""""""
1456 Warn on uses of the 'bzero' function.
1458 .. code-block:: c
1460  void test() {
1461    bzero(ptr, n); // warn
1464 .. _security-insecureAPI-getpw:
1466 security.insecureAPI.getpw (C)
1467 """"""""""""""""""""""""""""""
1468 Warn on uses of the 'getpw' function.
1470 .. code-block:: c
1472  void test() {
1473    char buff[1024];
1474    getpw(2, buff); // warn
1477 .. _security-insecureAPI-gets:
1479 security.insecureAPI.gets (C)
1480 """""""""""""""""""""""""""""
1481 Warn on uses of the 'gets' function.
1483 .. code-block:: c
1485  void test() {
1486    char buff[1024];
1487    gets(buff); // warn
1490 .. _security-insecureAPI-mkstemp:
1492 security.insecureAPI.mkstemp (C)
1493 """"""""""""""""""""""""""""""""
1494 Warn when 'mkstemp' is passed fewer than 6 X's in the format string.
1496 .. code-block:: c
1498  void test() {
1499    mkstemp("XX"); // warn
1502 .. _security-insecureAPI-mktemp:
1504 security.insecureAPI.mktemp (C)
1505 """""""""""""""""""""""""""""""
1506 Warn on uses of the ``mktemp`` function.
1508 .. code-block:: c
1510  void test() {
1511    char *x = mktemp("/tmp/zxcv"); // warn: insecure, use mkstemp
1514 .. _security-insecureAPI-rand:
1516 security.insecureAPI.rand (C)
1517 """""""""""""""""""""""""""""
1518 Warn 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``.
1521 .. code-block:: c
1523  void test() {
1524    random(); // warn
1527 .. _security-insecureAPI-strcpy:
1529 security.insecureAPI.strcpy (C)
1530 """""""""""""""""""""""""""""""
1531 Warn on uses of the ``strcpy`` and ``strcat`` functions.
1533 .. code-block:: c
1535  void test() {
1536    char x[4];
1537    char *y = "abcd";
1539    strcpy(x, y); // warn
1543 .. _security-insecureAPI-vfork:
1545 security.insecureAPI.vfork (C)
1546 """"""""""""""""""""""""""""""
1547  Warn on uses of the 'vfork' function.
1549 .. code-block:: c
1551  void test() {
1552    vfork(); // warn
1555 .. _security-insecureAPI-DeprecatedOrUnsafeBufferHandling:
1557 security.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``
1561 .. code-block:: c
1563  void test() {
1564    char buf [5];
1565    strncpy(buf, "a", 1); // warn
1568 .. _security-MmapWriteExec:
1570 security.MmapWriteExec (C)
1571 """"""""""""""""""""""""""
1572 Warn on ``mmap()`` calls with both writable and executable access.
1574 .. code-block:: c
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
1584 .. _security-PointerSub:
1586 security.PointerSub (C)
1587 """""""""""""""""""""""
1588 Check for pointer subtractions on two pointers pointing to different memory
1589 chunks. According to the C standard Â§6.5.6 only subtraction of pointers that
1590 point into (or one past the end) the same array object is valid (for this
1591 purpose non-array variables are like arrays of size 1). This checker only
1592 searches for different memory objects at subtraction, but does not check if the
1593 array index is correct. Furthermore, only cases are reported where
1594 stack-allocated objects are involved (no warnings on pointers to memory
1595 allocated by `malloc`).
1597 .. code-block:: c
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
1607  struct S {
1608    int x[10];
1609    int y[10];
1610  };
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
1622 There may be existing applications that use code like above for calculating
1623 offsets of members in a structure, using pointer subtractions. This is still
1624 undefined behavior according to the standard and code like this can be replaced
1625 with the `offsetof` macro.
1627 .. _security-putenv-stack-array:
1629 security.PutenvStackArray (C)
1630 """""""""""""""""""""""""""""
1631 Finds 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
1633 string, only a pointer to the data is stored and this data can be read even by
1634 other threads. Content of a stack-allocated array is likely to be overwritten
1635 after exiting from the function.
1637 The problem can be solved by using a static array variable or dynamically
1638 allocated memory. Even better is to avoid using ``putenv`` (it has other
1639 problems related to memory leaks) and use ``setenv`` instead.
1641 The 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>`_.
1645 .. code-block:: c
1647   int f() {
1648     char env[] = "NAME=value";
1649     return putenv(env); // putenv function should not be called with stack-allocated string
1650   }
1652 There is one case where the checker can report a false positive. This is when
1653 the stack-allocated array is used at `putenv` in a function or code branch that
1654 does not return (process is terminated on all execution paths).
1656 Another special case is if the `putenv` is called from function `main`. Here
1657 the stack is deallocated at the end of the program and it should be no problem
1658 to use the stack-allocated string (a multi-threaded program may require more
1659 attention). The checker does not warn for cases when stack space of `main` is
1660 used at the `putenv` call.
1662 security.SetgidSetuidOrder (C)
1663 """"""""""""""""""""""""""""""
1664 When 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
1666 privileges (with ``setgid``) first. Function ``setgid`` will likely fail if
1667 the superuser privileges are already dropped.
1669 The checker checks for sequences of ``setuid(getuid())`` and
1670 ``setgid(getgid())`` calls (in this order). If such a sequence is found and
1671 there is no other privilege-changing function call (``seteuid``, ``setreuid``,
1672 ``setresuid`` and the GID versions of these) in between, a warning is
1673 generated. The checker finds only exactly ``setuid(getuid())`` calls (and the
1674 GID versions), not for example if the result of ``getuid()`` is stored in a
1675 variable.
1677 .. code-block:: c
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    // ...
1695 In the code above the problem is that ``setuid(getuid())`` removes superuser
1696 privileges before ``setgid(getgid())`` is called. To fix the problem the
1697 ``setgid(getgid())`` should be called first. Further attention is needed to
1698 avoid code like ``setgid(getuid())`` (this checker does not detect bugs like
1699 this) and always check the return value of these calls.
1701 This 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>`_.
1703 .. _unix-checkers:
1705 unix
1706 ^^^^
1707 POSIX/Unix checkers.
1709 .. _unix-API:
1711 unix.API (C)
1712 """"""""""""
1713 Check calls to various UNIX/Posix functions: ``open, pthread_once, calloc, malloc, realloc, alloca``.
1715 .. literalinclude:: checkers/unix_api_example.c
1716     :language: c
1718 .. _unix-BlockInCriticalSection:
1720 unix.BlockInCriticalSection (C, C++)
1721 """"""""""""""""""""""""""""""""""""
1722 Check for calls to blocking functions inside a critical section.
1723 Blocking functions detected by this checker: ``sleep, getc, fgets, read, recv``.
1724 Critical 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``.
1727 .. code-block:: c
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);
1735 .. code-block:: cpp
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
1746 **Limitations**
1748 * The ``trylock`` and ``timedlock`` versions of acquiring locks are currently assumed to always succeed.
1749   This can lead to false positives.
1751 .. code-block:: c
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    }
1762 .. _unix-Chroot:
1764 unix.Chroot (C)
1765 """""""""""""""
1766 Check improper use of chroot described by SEI Cert C recommendation `POS05-C.
1767 Limit 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>`_.
1769 The checker finds usage patterns where ``chdir("/")`` is not called immediately
1770 after a call to ``chroot(path)``.
1772 .. code-block:: c
1774  void f();
1776  void test_bad() {
1777    chroot("/usr/local");
1778    f(); // warn: no call of chdir("/") immediately after chroot
1781   void test_bad_path() {
1782     chroot("/usr/local");
1783     chdir("/usr"); // warn: no call of chdir("/") immediately after chroot
1784     f();
1785   }
1787  void test_good() {
1788    chroot("/usr/local");
1789    chdir("/"); // no warning
1790    f();
1793 .. _unix-Errno:
1795 unix.Errno (C)
1796 """"""""""""""
1798 Check for improper use of ``errno``.
1799 This checker implements partially CERT rule
1800 `ERR30-C. Set errno to zero before calling a library function known to set errno,
1801 and check errno only after the function returns a value indicating failure
1802 <https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152351>`_.
1803 The checker can find the first read of ``errno`` after successful standard
1804 function calls.
1806 The C and POSIX standards often do not define if a standard library function
1807 may change value of ``errno`` if the call does not fail.
1808 Therefore, ``errno`` should only be used if it is known from the return value
1809 of a function that the call has failed.
1810 There are exceptions to this rule (for example ``strtol``) but the affected
1811 functions are not yet supported by the checker.
1812 The return values for the failure cases are documented in the standard Linux man
1813 pages of the functions and in the `POSIX standard <https://pubs.opengroup.org/onlinepubs/9699919799/>`_.
1815 .. code-block:: c
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;
1827 The checker :ref:`unix-StdCLibraryFunctions` must be turned on to get the
1828 warnings from this checker. The supported functions are the same as by
1829 :ref:`unix-StdCLibraryFunctions`. The ``ModelPOSIX`` option of that
1830 checker affects the set of checked functions.
1832 **Parameters**
1834 The ``AllowErrnoReadOutsideConditionExpressions`` option allows read of the
1835 errno value if the value is not used in a condition (in ``if`` statements,
1836 loops, conditional expressions, ``switch`` statements). For example ``errno``
1837 can be stored into a variable without getting a warning by the checker.
1839 .. code-block:: c
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;
1850 Default value of this option is ``true``. This allows save of the errno value
1851 for possible later error handling.
1853 **Limitations**
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.
1863 .. _unix-Malloc:
1865 unix.Malloc (C)
1866 """""""""""""""
1867 Check for memory leaks, double free, and use-after-free problems. Traces memory managed by malloc()/free().
1869 Custom allocation/deallocation functions can be defined using
1870 :ref:`ownership attributes<analyzer-ownership-attrs>`.
1872 .. literalinclude:: checkers/unix_malloc_example.c
1873     :language: c
1875 .. _unix-MallocSizeof:
1877 unix.MallocSizeof (C)
1878 """""""""""""""""""""
1879 Check for dubious ``malloc`` arguments involving ``sizeof``.
1881 Custom allocation/deallocation functions can be defined using
1882 :ref:`ownership attributes<analyzer-ownership-attrs>`.
1884 .. code-block:: c
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);
1893 .. _unix-MismatchedDeallocator:
1895 unix.MismatchedDeallocator (C, C++)
1896 """""""""""""""""""""""""""""""""""
1897 Check for mismatched deallocators.
1899 Custom allocation/deallocation functions can be defined using
1900 :ref:`ownership attributes<analyzer-ownership-attrs>`.
1902 .. literalinclude:: checkers/mismatched_deallocator_example.cpp
1903     :language: c
1905 .. _unix-Vfork:
1907 unix.Vfork (C)
1908 """"""""""""""
1909 Check for proper usage of ``vfork``.
1911 .. code-block:: c
1913  int test(int x) {
1914    pid_t pid = vfork(); // warn
1915    if (pid != 0)
1916      return 0;
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    }
1934    while(1);
1937 .. _unix-cstring-BadSizeArg:
1939 unix.cstring.BadSizeArg (C)
1940 """""""""""""""""""""""""""
1941 Check 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.
1943 .. code-block:: c
1945  void test() {
1946    char dest[3];
1947    strncat(dest, """""""""""""""""""""""""*", sizeof(dest));
1948      // warn: potential buffer overflow
1951 .. _unix-cstring-NotNullTerminated:
1953 unix.cstring.NotNullTerminated (C)
1954 """"""""""""""""""""""""""""""""""
1955 Check for arguments which are not null-terminated strings;
1956 applies to the ``strlen``, ``strcpy``, ``strcat``, ``strcmp`` family of functions.
1958 Only very fundamental cases are detected where the passed memory block is
1959 absolutely different from a null-terminated string. This checker does not
1960 find if a memory buffer is passed where the terminating zero character
1961 is missing.
1963 .. code-block:: c
1965  void test1() {
1966    int l = strlen((char *)&test1); // warn
1969  void test2() {
1970  label:
1971    int l = strlen((char *)&&label); // warn
1974 .. _unix-cstring-NullArg:
1976 unix.cstring.NullArg (C)
1977 """"""""""""""""""""""""
1978 Check for null pointers being passed as arguments to C string functions:
1979 ``strlen, strnlen, strcpy, strncpy, strcat, strncat, strcmp, strncmp, strcasecmp, strncasecmp, wcslen, wcsnlen``.
1981 .. code-block:: c
1983  int test() {
1984    return strlen(0); // warn
1987 .. _unix-StdCLibraryFunctions:
1989 unix.StdCLibraryFunctions (C)
1990 """""""""""""""""""""""""""""
1991 Check for calls of standard library functions that violate predefined argument
1992 constraints. 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
1994 as ``unsigned char`` and is not equal to ``EOF``.
1996 You can think of this checker as defining restrictions (pre- and postconditions)
1997 on standard library functions. Preconditions are checked, and when they are
1998 violated, a warning is emitted. Postconditions are added to the analysis, e.g.
1999 that the return value of a function is not greater than 255. Preconditions are
2000 added to the analysis too, in the case when the affected values are not known
2001 before the call.
2003 For example, if an argument to a function must be in between 0 and 255, but the
2004 value of the argument is unknown, the analyzer will assume that it is in this
2005 interval. Similarly, if a function mustn't be called with a null pointer and the
2006 analyzer cannot prove that it is null, then it will assume that it is non-null.
2008 These 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.
2022 .. code-block:: c
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   }
2031   void buffer_size_violation(FILE *file) {
2032     enum { BUFFER_SIZE = 1024 };
2033     wchar_t wbuf[BUFFER_SIZE];
2035     const size_t size = sizeof(*wbuf);   // 4
2036     const size_t nitems = sizeof(wbuf);  // 4096
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   }
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]
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   }
2054 Additionally to the argument and return value conditions, this checker also adds
2055 state of the value ``errno`` if applicable to the analysis. Many system
2056 functions set the ``errno`` value only if an error occurs (together with a
2057 specific return value of the function), otherwise it becomes undefined. This
2058 checker changes the analysis state to contain such information. This data is
2059 used by other checkers, for example :ref:`unix-Errno`.
2061 **Limitations**
2063 The checker can not always provide notes about the values of the arguments.
2064 Without this information it is hard to confirm if the constraint is indeed
2065 violated. The argument values are shown if they are known constants or the value
2066 is determined by previous (not too complicated) assumptions.
2068 The checker can produce false positives in cases such as if the program has
2069 invariants not known to the analyzer engine or the bug report path contains
2070 calls to unknown functions. In these cases the analyzer fails to detect the real
2071 range of the argument.
2073 **Parameters**
2075 The ``ModelPOSIX`` option controls if functions from the POSIX standard are
2076 recognized by the checker.
2078 With ``ModelPOSIX=true``, many POSIX functions are modeled according to the
2079 `POSIX standard`_. This includes ranges of parameters and possible return
2080 values. Furthermore the behavior related to ``errno`` in the POSIX case is
2081 often that ``errno`` is set only if a function call fails, and it becomes
2082 undefined after a successful function call.
2084 With ``ModelPOSIX=false``, this checker follows the C99 language standard and
2085 only models the functions that are described there. It is possible that the
2086 same functions are modeled differently in the two cases because differences in
2087 the standards. The C standard specifies less aspects of the functions, for
2088 example exact ``errno`` behavior is often unspecified (and not modeled by the
2089 checker).
2091 Default value of the option is ``true``.
2093 .. _unix-Stream:
2095 unix.Stream (C)
2096 """""""""""""""
2097 Check 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``.
2100 The checker maintains information about the C stream objects (``FILE *``) and
2101 can detect error conditions related to use of streams. The following conditions
2102 are detected:
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``.
2115 The stream operations are by this checker usually split into two cases, a success
2116 and a failure case.
2117 On 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``.
2119 Operations performed on ``stdout``, ``stderr``, or ``stdin`` are not checked by
2120 this checker in contrast to the streams opened by ``fopen``.
2122 In the case of write operations (like ``fwrite``,
2123 ``fprintf`` and even ``fsetpos``) this behavior could produce a large amount of
2124 unwanted reports on projects that don't have error checks around the write
2125 operations, so by default the checker assumes that write operations always succeed.
2126 This behavior can be controlled by the ``Pedantic`` flag: With
2127 ``-analyzer-config unix.Stream:Pedantic=true`` the checker will model the
2128 cases where a write operation fails and report situations where this leads to
2129 erroneous behavior. (The default is ``Pedantic=false``, where write operations
2130 are assumed to succeed.)
2132 .. code-block:: c
2134  void test1() {
2135    FILE *p = fopen("foo", "r");
2136  } // warn: opened file is never closed
2138  void test2() {
2139    FILE *p = fopen("foo", "r");
2140    fseek(p, 1, SEEK_SET); // warn: stream pointer might be NULL
2141    fclose(p);
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    }
2152  void test4() {
2153    FILE *p = fopen("foo", "r");
2154    if (!p)
2155      return;
2157    fclose(p);
2158    fclose(p); // warn: stream already closed
2161  void test5() {
2162    FILE *p = fopen("foo", "r");
2163    if (!p)
2164      return;
2166    fgetc(p);
2167    if (!ferror(p))
2168      fgetc(p); // warn: possible read after end-of-file
2170    fclose(p);
2173  void test6() {
2174    FILE *p = fopen("foo", "r");
2175    if (!p)
2176      return;
2178    fgetc(p);
2179    if (!feof(p))
2180      fgetc(p); // warn: file position may be indeterminate after I/O error
2182    fclose(p);
2185 **Limitations**
2187 The checker does not track the correspondence between integer file descriptors
2188 and ``FILE *`` pointers.
2190 .. _osx-checkers:
2194 macOS checkers.
2196 .. _osx-API:
2198 osx.API (C)
2199 """""""""""
2200 Check for proper uses of various Apple APIs.
2202 .. code-block:: objc
2204  void test() {
2205    dispatch_once_t pred = 0;
2206    dispatch_once(&pred, ^(){}); // warn: dispatch_once uses local
2209 .. _osx-NumberObjectConversion:
2211 osx.NumberObjectConversion (C, C++, ObjC)
2212 """""""""""""""""""""""""""""""""""""""""
2213 Check for erroneous conversions of objects representing numbers into numbers.
2215 .. code-block:: objc
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];
2224 .. _osx-ObjCProperty:
2226 osx.ObjCProperty (ObjC)
2227 """""""""""""""""""""""
2228 Check for proper uses of Objective-C properties.
2230 .. code-block:: objc
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];
2240 .. _osx-SecKeychainAPI:
2242 osx.SecKeychainAPI (C)
2243 """"""""""""""""""""""
2244 Check for proper uses of Secure Keychain APIs.
2246 .. literalinclude:: checkers/seckeychainapi_example.m
2247     :language: objc
2249 .. _osx-cocoa-AtSync:
2251 osx.cocoa.AtSync (ObjC)
2252 """""""""""""""""""""""
2253 Check for nil pointers used as mutexes for @synchronized.
2255 .. code-block:: objc
2257  void test(id x) {
2258    if (!x)
2259      @synchronized(x) {} // warn: nil value used as mutex
2262  void test() {
2263    id y;
2264    @synchronized(y) {} // warn: uninitialized value used as mutex
2267 .. _osx-cocoa-AutoreleaseWrite:
2269 osx.cocoa.AutoreleaseWrite
2270 """"""""""""""""""""""""""
2271 Warn about potentially crashing writes to autoreleasing objects from different autoreleasing pools in Objective-C.
2273 .. _osx-cocoa-ClassRelease:
2275 osx.cocoa.ClassRelease (ObjC)
2276 """""""""""""""""""""""""""""
2277 Check for sending 'retain', 'release', or 'autorelease' directly to a Class.
2279 .. code-block:: objc
2281  @interface MyClass : NSObject
2282  @end
2284  void test(void) {
2285    [MyClass release]; // warn
2288 .. _osx-cocoa-Dealloc:
2290 osx.cocoa.Dealloc (ObjC)
2291 """"""""""""""""""""""""
2292 Warn about Objective-C classes that lack a correct implementation of -dealloc
2294 .. literalinclude:: checkers/dealloc_example.m
2295     :language: objc
2297 .. _osx-cocoa-IncompatibleMethodTypes:
2299 osx.cocoa.IncompatibleMethodTypes (ObjC)
2300 """"""""""""""""""""""""""""""""""""""""
2301 Warn about Objective-C method signatures with type incompatibilities.
2303 .. code-block:: objc
2305  @interface MyClass1 : NSObject
2306  - (int)foo;
2307  @end
2309  @implementation MyClass1
2310  - (int)foo { return 1; }
2311  @end
2313  @interface MyClass2 : MyClass1
2314  - (float)foo;
2315  @end
2317  @implementation MyClass2
2318  - (float)foo { return 1.0; } // warn
2319  @end
2321 .. _osx-cocoa-Loops:
2323 osx.cocoa.Loops
2324 """""""""""""""
2325 Improved modeling of loops using Cocoa collection types.
2327 .. _osx-cocoa-MissingSuperCall:
2329 osx.cocoa.MissingSuperCall (ObjC)
2330 """""""""""""""""""""""""""""""""
2331 Warn about Objective-C methods that lack a necessary call to super.
2333 .. code-block:: objc
2335  @interface Test : UIViewController
2336  @end
2337  @implementation test
2338  - (void)viewDidLoad {} // warn
2339  @end
2342 .. _osx-cocoa-NSAutoreleasePool:
2344 osx.cocoa.NSAutoreleasePool (ObjC)
2345 """"""""""""""""""""""""""""""""""
2346 Warn for suboptimal uses of NSAutoreleasePool in Objective-C GC mode.
2348 .. code-block:: objc
2350  void test() {
2351    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2352    [pool release]; // warn
2355 .. _osx-cocoa-NSError:
2357 osx.cocoa.NSError (ObjC)
2358 """"""""""""""""""""""""
2359 Check usage of NSError parameters.
2361 .. code-block:: objc
2363  @interface A : NSObject
2364  - (void)foo:(NSError """""""""""""""""""""""")error;
2365  @end
2367  @implementation A
2368  - (void)foo:(NSError """""""""""""""""""""""")error {
2369    // warn: method accepting NSError"""""""""""""""""""""""" should have a non-void
2370    // return value
2372  @end
2374  @interface A : NSObject
2375  - (BOOL)foo:(NSError """""""""""""""""""""""")error;
2376  @end
2378  @implementation A
2379  - (BOOL)foo:(NSError """""""""""""""""""""""")error {
2380    *error = 0; // warn: potential null dereference
2381    return 0;
2383  @end
2385 .. _osx-cocoa-NilArg:
2387 osx.cocoa.NilArg (ObjC)
2388 """""""""""""""""""""""
2389 Check for prohibited nil arguments to ObjC method calls.
2391  - caseInsensitiveCompare:
2392  - compare:
2393  - compare:options:
2394  - compare:options:range:
2395  - compare:options:range:locale:
2396  - componentsSeparatedByCharactersInSet:
2397  - initWithFormat:
2399 .. code-block:: objc
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
2409 .. _osx-cocoa-NonNilReturnValue:
2411 osx.cocoa.NonNilReturnValue
2412 """""""""""""""""""""""""""
2413 Models the APIs that are guaranteed to return a non-nil value.
2415 .. _osx-cocoa-ObjCGenerics:
2417 osx.cocoa.ObjCGenerics (ObjC)
2418 """""""""""""""""""""""""""""
2419 Check for type errors when using Objective-C generics.
2421 .. code-block:: objc
2423  NSMutableArray *names = [NSMutableArray array];
2424  NSMutableArray *birthDates = names;
2426  // Warning: Conversion from value of type 'NSDate *'
2427  // to incompatible type 'NSString *'
2428  [birthDates addObject: [NSDate date]];
2430 .. _osx-cocoa-RetainCount:
2432 osx.cocoa.RetainCount (ObjC)
2433 """"""""""""""""""""""""""""
2434 Check for leaks and improper reference count management
2436 .. code-block:: objc
2438  void test() {
2439    NSString *s = [[NSString alloc] init]; // warn
2442  CFStringRef test(char *bytes) {
2443    return CFStringCreateWithCStringNoCopy(
2444             0, bytes, NSNEXTSTEPStringEncoding, 0); // warn
2448 .. _osx-cocoa-RunLoopAutoreleaseLeak:
2450 osx.cocoa.RunLoopAutoreleaseLeak
2451 """"""""""""""""""""""""""""""""
2452 Check for leaked memory in autorelease pools that will never be drained.
2454 .. _osx-cocoa-SelfInit:
2456 osx.cocoa.SelfInit (ObjC)
2457 """""""""""""""""""""""""
2458 Check that 'self' is properly initialized inside an initializer method.
2460 .. code-block:: objc
2462  @interface MyObj : NSObject {
2463    id x;
2465  - (id)init;
2466  @end
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;
2475  @end
2477  @interface MyObj : NSObject
2478  - (id)init;
2479  @end
2481  @implementation MyObj
2482  - (id)init {
2483    [super init];
2484    return self; // warn: returning uninitialized 'self'
2486  @end
2488 .. _osx-cocoa-SuperDealloc:
2490 osx.cocoa.SuperDealloc (ObjC)
2491 """""""""""""""""""""""""""""
2492 Warn about improper use of '[super dealloc]' in Objective-C.
2494 .. code-block:: objc
2496  @interface SuperDeallocThenReleaseIvarClass : NSObject {
2497    NSObject *_ivar;
2499  @end
2501  @implementation SuperDeallocThenReleaseIvarClass
2502  - (void)dealloc {
2503    [super dealloc];
2504    [_ivar release]; // warn
2506  @end
2508 .. _osx-cocoa-UnusedIvars:
2510 osx.cocoa.UnusedIvars (ObjC)
2511 """"""""""""""""""""""""""""
2512 Warn about private ivars that are never used.
2514 .. code-block:: objc
2516  @interface MyObj : NSObject {
2517  @private
2518    id x; // warn
2520  @end
2522  @implementation MyObj
2523  @end
2525 .. _osx-cocoa-VariadicMethodTypes:
2527 osx.cocoa.VariadicMethodTypes (ObjC)
2528 """"""""""""""""""""""""""""""""""""
2529 Check for passing non-Objective-C types to variadic collection
2530 initialization methods that expect only Objective-C types.
2532 .. code-block:: objc
2534  void test() {
2535    [NSSet setWithObjects:@"Foo", "Bar", nil];
2536      // warn: argument should be an ObjC pointer type, not 'char *'
2539 .. _osx-coreFoundation-CFError:
2541 osx.coreFoundation.CFError (C)
2542 """"""""""""""""""""""""""""""
2543 Check usage of CFErrorRef* parameters
2545 .. code-block:: c
2547  void test(CFErrorRef *error) {
2548    // warn: function accepting CFErrorRef* should have a
2549    // non-void return
2552  int foo(CFErrorRef *error) {
2553    *error = 0; // warn: potential null dereference
2554    return 0;
2557 .. _osx-coreFoundation-CFNumber:
2559 osx.coreFoundation.CFNumber (C)
2560 """""""""""""""""""""""""""""""
2561 Check for proper uses of CFNumber APIs.
2563 .. code-block:: c
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
2570 .. _osx-coreFoundation-CFRetainRelease:
2572 osx.coreFoundation.CFRetainRelease (C)
2573 """"""""""""""""""""""""""""""""""""""
2574 Check for null arguments to CFRetain/CFRelease/CFMakeCollectable.
2576 .. code-block:: c
2578  void test(CFTypeRef p) {
2579    if (!p)
2580      CFRetain(p); // warn
2583  void test(int x, CFTypeRef p) {
2584    if (p)
2585      return;
2587    CFRelease(p); // warn
2590 .. _osx-coreFoundation-containers-OutOfBounds:
2592 osx.coreFoundation.containers.OutOfBounds (C)
2593 """""""""""""""""""""""""""""""""""""""""""""
2594 Checks for index out-of-bounds when using 'CFArray' API.
2596 .. code-block:: c
2598  void test() {
2599    CFArrayRef A = CFArrayCreate(0, 0, 0, &kCFTypeArrayCallBacks);
2600    CFArrayGetValueAtIndex(A, 0); // warn
2603 .. _osx-coreFoundation-containers-PointerSizedValues:
2605 osx.coreFoundation.containers.PointerSizedValues (C)
2606 """"""""""""""""""""""""""""""""""""""""""""""""""""
2607 Warns if 'CFArray', 'CFDictionary', 'CFSet' are created with non-pointer-size values.
2609 .. code-block:: c
2611  void test() {
2612    int x[] = { 1 };
2613    CFArrayRef A = CFArrayCreate(0, (const void """""""""""""""""""""""")x, 1,
2614                                 &kCFTypeArrayCallBacks); // warn
2617 Fuchsia
2618 ^^^^^^^
2620 Fuchsia is an open source capability-based operating system currently being
2621 developed by Google. This section describes checkers that can find various
2622 misuses of Fuchsia APIs.
2624 .. _fuchsia-HandleChecker:
2626 fuchsia.HandleChecker
2627 """"""""""""""""""""""""""""
2628 Handles identify resources. Similar to pointers they can be leaked,
2629 double freed, or use after freed. This check attempts to find such problems.
2631 .. code-block:: cpp
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);
2642 WebKit
2643 ^^^^^^
2645 WebKit is an open-source web browser engine available for macOS, iOS and Linux.
2646 This section describes checkers that can find issues in WebKit codebase.
2648 Most of the checkers focus on memory management for which WebKit uses custom implementation of reference counted smartpointers.
2650 Checkers 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.
2655 .. _webkit-RefCntblBaseVirtualDtor:
2657 webkit.RefCntblBaseVirtualDtor
2658 """"""""""""""""""""""""""""""""""""
2659 All uncounted types used as base classes must have a virtual destructor.
2661 Ref-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]``.
2663 .. code-block:: cpp
2665  struct RefCntblBase {
2666    void ref() {}
2667    void deref() {}
2668  };
2670  struct Derived : RefCntblBase { }; // warn
2672 .. _webkit-NoUncountedMemberChecker:
2674 webkit.NoUncountedMemberChecker
2675 """""""""""""""""""""""""""""""""""""
2676 Raw pointers and references to uncounted types can't be used as class members. Only ref-counted types are allowed.
2678 .. code-block:: cpp
2680  struct RefCntbl {
2681    void ref() {}
2682    void deref() {}
2683  };
2685  struct Foo {
2686    RefCntbl * ptr; // warn
2687    RefCntbl & ptr; // warn
2688    // ...
2689  };
2691 .. _webkit-UncountedLambdaCapturesChecker:
2693 webkit.UncountedLambdaCapturesChecker
2694 """""""""""""""""""""""""""""""""""""
2695 Raw pointers and references to uncounted types can't be captured in lambdas. Only ref-counted types are allowed.
2697 .. code-block:: cpp
2699  struct RefCntbl {
2700    void ref() {}
2701    void deref() {}
2702  };
2704  void foo(RefCntbl* a, RefCntbl& b) {
2705    [&, a](){ // warn about 'a'
2706      do_something(b); // warn about 'b'
2707    };
2708  };
2710 .. _alpha-checkers:
2712 Experimental Checkers
2713 ---------------------
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.*
2717 alpha.clone
2718 ^^^^^^^^^^^
2720 .. _alpha-clone-CloneChecker:
2722 alpha.clone.CloneChecker (C, C++, ObjC)
2723 """""""""""""""""""""""""""""""""""""""
2724 Reports similar pieces of code.
2726 .. code-block:: c
2728  void log();
2730  int max(int a, int b) { // warn
2731    log();
2732    if (a > b)
2733      return a;
2734    return b;
2737  int maxClone(int x, int y) { // similar code here
2738    log();
2739    if (x > y)
2740      return x;
2741    return y;
2744 alpha.core
2745 ^^^^^^^^^^
2747 .. _alpha-core-BoolAssignment:
2749 alpha.core.BoolAssignment (ObjC)
2750 """"""""""""""""""""""""""""""""
2751 Warn about assigning non-{0,1} values to boolean variables.
2753 .. code-block:: objc
2755  void test() {
2756    BOOL b = -1; // warn
2759 .. _alpha-core-C11Lock:
2761 alpha.core.C11Lock
2762 """"""""""""""""""
2763 Similarly to :ref:`alpha.unix.PthreadLock <alpha-unix-PthreadLock>`, checks for
2764 the locking/unlocking of ``mtx_t`` mutexes.
2766 .. code-block:: cpp
2768  mtx_t mtx1;
2770  void bad1(void)
2772    mtx_lock(&mtx1);
2773    mtx_lock(&mtx1); // warn: This lock has already been acquired
2776 .. _alpha-core-CastSize:
2778 alpha.core.CastSize (C)
2779 """""""""""""""""""""""
2780 Check when casting a malloc'ed type ``T``, whether the size is a multiple of the size of ``T``.
2782 .. code-block:: c
2784  void test() {
2785    int *x = (int *) malloc(11); // warn
2788 .. _alpha-core-CastToStruct:
2790 alpha.core.CastToStruct (C, C++)
2791 """"""""""""""""""""""""""""""""
2792 Check for cast from non-struct pointer to struct pointer.
2794 .. code-block:: cpp
2796  // C
2797  struct s {};
2799  void test(int *p) {
2800    struct s *ps = (struct s *) p; // warn
2803  // C++
2804  class c {};
2806  void test(int *p) {
2807    c *pc = (c *) p; // warn
2810 .. _alpha-core-Conversion:
2812 alpha.core.Conversion (C, C++, ObjC)
2813 """"""""""""""""""""""""""""""""""""
2814 Loss of sign/precision in implicit conversions.
2816 .. code-block:: c
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    }
2829  void test() {
2830    long long A = 1LL << 60;
2831    short X = A; // warn (loss of precision)
2834 .. _alpha-core-DynamicTypeChecker:
2836 alpha.core.DynamicTypeChecker (ObjC)
2837 """"""""""""""""""""""""""""""""""""
2838 Check for cases where the dynamic and the static type of an object are unrelated.
2841 .. code-block:: objc
2843  id date = [NSDate date];
2845  // Warning: Object has a dynamic type 'NSDate *' which is
2846  // incompatible with static type 'NSNumber *'"
2847  NSNumber *number = date;
2848  [number doubleValue];
2850 .. _alpha-core-FixedAddr:
2852 alpha.core.FixedAddr (C)
2853 """"""""""""""""""""""""
2854 Check for assignment of a fixed address to a pointer.
2856 .. code-block:: c
2858  void test() {
2859    int *p;
2860    p = (int *) 0x10000; // warn
2863 .. _alpha-core-PointerArithm:
2865 alpha.core.PointerArithm (C)
2866 """"""""""""""""""""""""""""
2867 Check for pointer arithmetic on locations other than array elements.
2869 .. code-block:: c
2871  void test() {
2872    int x;
2873    int *p;
2874    p = &x + 1; // warn
2877 .. _alpha-core-StackAddressAsyncEscape:
2879 alpha.core.StackAddressAsyncEscape (ObjC)
2880 """""""""""""""""""""""""""""""""""""""""
2881 Check that addresses to stack memory do not escape the function that involves dispatch_after or dispatch_async.
2882 This checker is a part of ``core.StackAddressEscape``, but is temporarily disabled until some false positives are fixed.
2884 .. code-block:: c
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
2901 .. _alpha-core-StdVariant:
2903 alpha.core.StdVariant (C++)
2904 """""""""""""""""""""""""""
2905 Check if a value of active type is retrieved from an ``std::variant`` instance with ``std::get``.
2906 In case of bad variant type access (the accessed type differs from the active type)
2907 a warning is emitted. Currently, this checker does not take exception handling into account.
2909 .. code-block:: cpp
2911  void test() {
2912    std::variant<int, char> v = 25;
2913    char c = stg::get<char>(v); // warn: "int" is the active alternative
2916 .. _alpha-core-TestAfterDivZero:
2918 alpha.core.TestAfterDivZero (C)
2919 """""""""""""""""""""""""""""""
2920 Check for division by variable that is later compared against 0.
2921 Either the comparison is useless or there is division by zero.
2923 .. code-block:: c
2925  void test(int x) {
2926    var = 77 / x;
2927    if (x == 0) { } // warn
2930 alpha.cplusplus
2931 ^^^^^^^^^^^^^^^
2933 .. _alpha-cplusplus-DeleteWithNonVirtualDtor:
2935 alpha.cplusplus.DeleteWithNonVirtualDtor (C++)
2936 """"""""""""""""""""""""""""""""""""""""""""""
2937 Reports destructions of polymorphic objects with a non-virtual destructor in their base class.
2939 .. code-block:: cpp
2941  class NonVirtual {};
2942  class NVDerived : public NonVirtual {};
2944  NonVirtual *create() {
2945    NonVirtual *x = new NVDerived(); // note: Casting from 'NVDerived' to
2946                                     //       'NonVirtual' here
2947    return x;
2950  void foo() {
2951    NonVirtual *x = create();
2952    delete x; // warn: destruction of a polymorphic object with no virtual
2953              //       destructor
2956 .. _alpha-cplusplus-InvalidatedIterator:
2958 alpha.cplusplus.InvalidatedIterator (C++)
2959 """""""""""""""""""""""""""""""""""""""""
2960 Check for use of invalidated iterators.
2962 .. code-block:: cpp
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
2972 .. _alpha-cplusplus-IteratorRange:
2974 alpha.cplusplus.IteratorRange (C++)
2975 """""""""""""""""""""""""""""""""""
2976 Check for iterators used outside their valid ranges.
2978 .. code-block:: cpp
2980  void simple_bad_end(const std::vector &v) {
2981    auto i = v.end();
2982    *i; // warn: iterator accessed outside of its range
2985 .. _alpha-cplusplus-MismatchedIterator:
2987 alpha.cplusplus.MismatchedIterator (C++)
2988 """"""""""""""""""""""""""""""""""""""""
2989 Check for use of iterators of different containers where iterators of the same container are expected.
2991 .. code-block:: cpp
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
3009 .. _alpha-cplusplus-SmartPtr:
3011 alpha.cplusplus.SmartPtr (C++)
3012 """"""""""""""""""""""""""""""
3013 Check for dereference of null smart pointers.
3015 .. code-block:: cpp
3017  void deref_smart_ptr() {
3018    std::unique_ptr<int> P;
3019    *P; // warn: dereference of a default constructed smart unique_ptr
3023 alpha.deadcode
3024 ^^^^^^^^^^^^^^
3025 .. _alpha-deadcode-UnreachableCode:
3027 alpha.deadcode.UnreachableCode (C, C++)
3028 """""""""""""""""""""""""""""""""""""""
3029 Check unreachable code.
3031 .. code-block:: cpp
3033  // C
3034  int test() {
3035    int x = 1;
3036    while(x);
3037    return x; // warn
3040  // C++
3041  void test() {
3042    int a = 2;
3044    while (a > 1)
3045      a--;
3047    if (a > 1)
3048      a++; // warn
3051  // Objective-C
3052  void test(id x) {
3053    return;
3054    [x retain]; // warn
3057 alpha.fuchsia
3058 ^^^^^^^^^^^^^
3060 .. _alpha-fuchsia-lock:
3062 alpha.fuchsia.Lock
3063 """"""""""""""""""
3064 Similarly to :ref:`alpha.unix.PthreadLock <alpha-unix-PthreadLock>`, checks for
3065 the locking/unlocking of fuchsia mutexes.
3067 .. code-block:: cpp
3069  spin_lock_t mtx1;
3071  void bad1(void)
3073    spin_lock(&mtx1);
3074    spin_lock(&mtx1);    // warn: This lock has already been acquired
3077 alpha.llvm
3078 ^^^^^^^^^^
3080 .. _alpha-llvm-Conventions:
3082 alpha.llvm.Conventions
3083 """"""""""""""""""""""
3085 Check code for LLVM codebase conventions:
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.
3091 alpha.osx
3092 ^^^^^^^^^
3094 .. _alpha-osx-cocoa-DirectIvarAssignment:
3096 alpha.osx.cocoa.DirectIvarAssignment (ObjC)
3097 """""""""""""""""""""""""""""""""""""""""""
3098 Check for direct assignments to instance variables.
3101 .. code-block:: objc
3103  @interface MyClass : NSObject {}
3104  @property (readonly) id A;
3105  - (void) foo;
3106  @end
3108  @implementation MyClass
3109  - (void) foo {
3110    _A = 0; // warn
3112  @end
3114 .. _alpha-osx-cocoa-DirectIvarAssignmentForAnnotatedFunctions:
3116 alpha.osx.cocoa.DirectIvarAssignmentForAnnotatedFunctions (ObjC)
3117 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
3118 Check for direct assignments to instance variables in
3119 the methods annotated with ``objc_no_direct_instance_variable_assignment``.
3121 .. code-block:: objc
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
3130  @implementation MyClass
3131  - (void) fAnnotated {
3132    _A = 0; // warn
3134  - (void) fNotAnnotated {
3135    _A = 0; // no warn
3137  @end
3140 .. _alpha-osx-cocoa-InstanceVariableInvalidation:
3142 alpha.osx.cocoa.InstanceVariableInvalidation (ObjC)
3143 """""""""""""""""""""""""""""""""""""""""""""""""""
3144 Check that the invalidatable instance variables are
3145 invalidated in the methods annotated with objc_instance_variable_invalidator.
3147 .. code-block:: objc
3149  @protocol Invalidation <NSObject>
3150  - (void) invalidate
3151    __attribute__((annotate("objc_instance_variable_invalidator")));
3152  @end
3154  @interface InvalidationImpObj : NSObject <Invalidation>
3155  @end
3157  @interface SubclassInvalidationImpObj : InvalidationImpObj {
3158    InvalidationImpObj *var;
3160  - (void)invalidate;
3161  @end
3163  @implementation SubclassInvalidationImpObj
3164  - (void) invalidate {}
3165  @end
3166  // warn: var needs to be invalidated or set to nil
3168 .. _alpha-osx-cocoa-MissingInvalidationMethod:
3170 alpha.osx.cocoa.MissingInvalidationMethod (ObjC)
3171 """"""""""""""""""""""""""""""""""""""""""""""""
3172 Check that the invalidation methods are present in classes that contain invalidatable instance variables.
3174 .. code-block:: objc
3176  @protocol Invalidation <NSObject>
3177  - (void)invalidate
3178    __attribute__((annotate("objc_instance_variable_invalidator")));
3179  @end
3181  @interface NeedInvalidation : NSObject <Invalidation>
3182  @end
3184  @interface MissingInvalidationMethodDecl : NSObject {
3185    NeedInvalidation *Var; // warn
3187  @end
3189  @implementation MissingInvalidationMethodDecl
3190  @end
3192 .. _alpha-osx-cocoa-localizability-PluralMisuseChecker:
3194 alpha.osx.cocoa.localizability.PluralMisuseChecker (ObjC)
3195 """""""""""""""""""""""""""""""""""""""""""""""""""""""""
3196 Warns against using one vs. many plural pattern in code when generating localized strings.
3198 .. code-block:: objc
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];
3216 alpha.security
3217 ^^^^^^^^^^^^^^
3219 .. _alpha-security-ArrayBound:
3221 alpha.security.ArrayBound (C)
3222 """""""""""""""""""""""""""""
3223 Warn about buffer overflows (older checker).
3225 .. code-block:: c
3227  void test() {
3228    char *s = "";
3229    char c = s[1]; // warn
3232  struct seven_words {
3233    int c[7];
3234  };
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
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
3251  void test() {
3252    char a[2];
3253    int *b = (int*)a;
3254    b[1] = 3; // warn
3257 .. _alpha-security-ArrayBoundV2:
3259 alpha.security.ArrayBoundV2 (C)
3260 """""""""""""""""""""""""""""""
3261 Warn about buffer overflows (newer checker).
3263 .. code-block:: c
3265  void test() {
3266    char *s = "";
3267    char c = s[1]; // warn
3270  void test() {
3271    int buf[100];
3272    int *p = buf;
3273    p = p + 99;
3274    p[1] = 1; // warn
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
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
3291 .. _alpha-security-ReturnPtrRange:
3293 alpha.security.ReturnPtrRange (C)
3294 """""""""""""""""""""""""""""""""
3295 Check for an out-of-bound pointer being returned to callers.
3297 .. code-block:: c
3299  static int A[10];
3301  int *test() {
3302    int *p = A + 10;
3303    return p; // warn
3306  int test(void) {
3307    int x;
3308    return x; // warn: undefined or garbage returned
3312 alpha.security.cert
3313 ^^^^^^^^^^^^^^^^^^^
3315 SEI CERT checkers which tries to find errors based on their `C coding rules <https://wiki.sei.cmu.edu/confluence/display/c/2+Rules>`_.
3317 alpha.unix
3318 ^^^^^^^^^^
3320 .. _alpha-unix-PthreadLock:
3322 alpha.unix.PthreadLock (C)
3323 """"""""""""""""""""""""""
3324 Simple lock -> unlock checker.
3325 Applies 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,
3327 lck_rw_try_lock_exclusive, lck_rw_try_lock_shared, pthread_mutex_unlock, pthread_rwlock_unlock, lck_mtx_unlock, lck_rw_done``.
3330 .. code-block:: c
3332  pthread_mutex_t mtx;
3334  void test() {
3335    pthread_mutex_lock(&mtx);
3336    pthread_mutex_lock(&mtx);
3337      // warn: this lock has already been acquired
3340  lck_mtx_t lck1, lck2;
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
3349  lck_mtx_t lck1, lck2;
3351  void test() {
3352    if (lck_mtx_try_lock(&lck1) == 0)
3353      return;
3355    lck_mtx_lock(&lck2);
3356    lck_mtx_unlock(&lck1);
3357      // warn: this was not the most recently acquired lock
3360 .. _alpha-unix-SimpleStream:
3362 alpha.unix.SimpleStream (C)
3363 """""""""""""""""""""""""""
3364 Check 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/>`_).
3369 .. code-block:: c
3371  void test() {
3372    FILE *F = fopen("myfile.txt", "w");
3373  } // warn: opened file is never closed
3375  void test() {
3376    FILE *F = fopen("myfile.txt", "w");
3378    if (F)
3379      fclose(F);
3381    fclose(F); // warn: closing a previously closed file stream
3384 .. _alpha-unix-cstring-BufferOverlap:
3386 alpha.unix.cstring.BufferOverlap (C)
3387 """"""""""""""""""""""""""""""""""""
3388 Checks for overlap in two buffer arguments. Applies to:  ``memcpy, mempcpy, wmemcpy, wmempcpy``.
3390 .. code-block:: c
3392  void test() {
3393    int a[4] = {0};
3394    memcpy(a + 2, a + 1, 8); // warn
3397 .. _alpha-unix-cstring-OutOfBounds:
3399 alpha.unix.cstring.OutOfBounds (C)
3400 """"""""""""""""""""""""""""""""""
3401 Check for out-of-bounds access in string functions, such as:
3402 ``memcpy, bcopy, strcpy, strncpy, strcat, strncat, memmove, memcmp, memset`` and more.
3404 This check also works with string literals, except there is a known bug in that
3405 the analyzer cannot detect embedded NULL characters when determining the string length.
3407 .. code-block:: c
3409  void test1() {
3410    const char str[] = "Hello world";
3411    char buffer[] = "Hello world";
3412    memcpy(buffer, str, sizeof(str) + 1); // warn
3415  void test2() {
3416    const char str[] = "Hello world";
3417    char buffer[] = "Helloworld";
3418    memcpy(buffer, str, sizeof(str)); // warn
3421 .. _alpha-unix-cstring-UninitializedRead:
3423 alpha.unix.cstring.UninitializedRead (C)
3424 """"""""""""""""""""""""""""""""""""""""
3425 Check for uninitialized reads from common memory copy/manipulation functions such as:
3426  ``memcpy, mempcpy, memmove, memcmp, strcmp, strncmp, strcpy, strlen, strsep`` and many more.
3428 .. code-block:: c
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
3436 Limitations:
3438    - Due to limitations of the memory modeling in the analyzer, one can likely
3439      observe a lot of false-positive reports like this:
3441       .. code-block:: c
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         }
3452      More details at the corresponding `GitHub issue <https://github.com/llvm/llvm-project/issues/43459>`_.
3454 alpha.WebKit
3455 ^^^^^^^^^^^^
3457 .. _alpha-webkit-NoUncheckedPtrMemberChecker:
3459 alpha.webkit.MemoryUnsafeCastChecker
3460 """"""""""""""""""""""""""""""""""""""
3461 Check for all casts from a base type to its derived type as these might be memory-unsafe.
3463 Example:
3465 .. code-block:: cpp
3467     class Base { };
3468     class Derived : public Base { };
3470     void f(Base* base) {
3471         Derived* derived = static_cast<Derived*>(base); // ERROR
3472     }
3474 For 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.
3476 This applies to:
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.
3482 For 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.
3484 alpha.webkit.NoUncheckedPtrMemberChecker
3485 """"""""""""""""""""""""""""""""""""""""
3486 Raw 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.
3488 .. code-block:: cpp
3490  struct CheckableObj {
3491    void incrementCheckedPtrCount() {}
3492    void decrementCheckedPtrCount() {}
3493  };
3495  struct Foo {
3496    CheckableObj* ptr; // warn
3497    CheckableObj& ptr; // warn
3498    // ...
3499  };
3501 See `WebKit Guidelines for Safer C++ Programming <https://github.com/WebKit/WebKit/wiki/Safer-CPP-Guidelines>`_ for details.
3503 .. _alpha-webkit-UncountedCallArgsChecker:
3505 alpha.webkit.UncountedCallArgsChecker
3506 """""""""""""""""""""""""""""""""""""
3507 The 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.
3509 Here 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.
3511   .. code-block:: cpp
3513     RefCountable* provide_uncounted();
3514     void consume(RefCountable*);
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.
3518     void foo1() {
3519       consume(provide_uncounted()); // warn
3520     }
3522     void foo2() {
3523       RefCountable* uncounted = provide_uncounted();
3524       consume(uncounted); // warn
3525     }
3527 Although 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.
3529 Note: 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.
3531   .. code-block:: cpp
3533     struct Foo {
3534       RefPtr<RefCountable> member;
3535       void consume(RefCountable*) { /* ... */ }
3536       void bugprone() {
3537         consume(member.get()); // warn
3538       }
3539     };
3541 The 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.
3543 Allowed kinds of arguments:
3545 - values obtained from ref-counted objects (including temporaries as those survive the call too)
3547   .. code-block:: cpp
3549     RefCountable* provide_uncounted();
3550     void consume(RefCountable*);
3552     void foo() {
3553       RefPtr<RefCountable> rc = makeRef(provide_uncounted());
3554       consume(rc.get()); // ok
3555       consume(makeRef(provide_uncounted()).get()); // ok
3556     }
3558 - forwarding uncounted arguments from caller to callee
3560   .. code-block:: cpp
3562     void foo(RefCountable& a) {
3563       bar(a); // ok
3564     }
3566   Caller of ``foo()`` is responsible for  ``a``'s lifetime.
3568 - ``this`` pointer
3570   .. code-block:: cpp
3572     void Foo::foo() {
3573       baz(this);  // ok
3574     }
3576   Caller of ``foo()`` is responsible for keeping the memory pointed to by ``this`` pointer safe.
3578 - constants
3580   .. code-block:: cpp
3582     foo(nullptr, NULL, 0); // ok
3584 We 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.
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 ``*``
3592 alpha.webkit.UncheckedCallArgsChecker
3593 """""""""""""""""""""""""""""""""""""
3594 The 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.
3596 The rules of when to use and not to use CheckedPtr / CheckedRef are same as alpha.webkit.UncountedCallArgsChecker for ref-counted objects.
3598 alpha.webkit.UncountedLocalVarsChecker
3599 """"""""""""""""""""""""""""""""""""""
3600 The 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.
3602 These are examples of cases that we consider safe:
3604   .. code-block:: cpp
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     }
3614     void foo2(RefPtr<RefCountable> counted_param) {
3615       RefCountable* uncounted = counted_param.get(); // ok
3616     }
3618     void FooClass::foo_method() {
3619       RefCountable* uncounted = this; // ok
3620     }
3622 Here 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.
3624   .. code-block:: cpp
3626     void foo1() {
3627       RefCountable* uncounted = new RefCountable; // warn
3628     }
3630     RefCountable* global_uncounted;
3631     void foo2() {
3632       RefCountable* uncounted = global_uncounted; // warn
3633     }
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     }
3641 alpha.webkit.UncheckedLocalVarsChecker
3642 """"""""""""""""""""""""""""""""""""""
3643 The 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.
3645 These are examples of cases that we consider safe:
3647   .. code-block:: cpp
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     }
3657     void foo2(CheckedPtr<RefCountable> counted_param) {
3658       RefCountable* uncounted = counted_param.get(); // ok
3659     }
3661     void FooClass::foo_method() {
3662       RefCountable* uncounted = this; // ok
3663     }
3665 Here 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.
3667   .. code-block:: cpp
3669     void foo1() {
3670       RefCountable* uncounted = new RefCountable; // warn
3671     }
3673     RefCountable* global_uncounted;
3674     void foo2() {
3675       RefCountable* uncounted = global_uncounted; // warn
3676     }
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     }
3684 Debug Checkers
3685 ---------------
3687 .. _debug-checkers:
3690 debug
3691 ^^^^^
3693 Checkers used for debugging the analyzer.
3694 :doc:`developer-docs/DebugChecks` page contains a detailed description.
3696 .. _debug-AnalysisOrder:
3698 debug.AnalysisOrder
3699 """""""""""""""""""
3700 Print callbacks that are called during analysis in order.
3702 .. _debug-ConfigDumper:
3704 debug.ConfigDumper
3705 """"""""""""""""""
3706 Dump config table.
3708 .. _debug-DumpCFG Display:
3710 debug.DumpCFG Display
3711 """""""""""""""""""""
3712 Control-Flow Graphs.
3714 .. _debug-DumpCallGraph:
3716 debug.DumpCallGraph
3717 """""""""""""""""""
3718 Display Call Graph.
3720 .. _debug-DumpCalls:
3722 debug.DumpCalls
3723 """""""""""""""
3724 Print calls as they are traversed by the engine.
3726 .. _debug-DumpDominators:
3728 debug.DumpDominators
3729 """"""""""""""""""""
3730 Print the dominance tree for a given CFG.
3732 .. _debug-DumpLiveVars:
3734 debug.DumpLiveVars
3735 """"""""""""""""""
3736 Print results of live variable analysis.
3738 .. _debug-DumpTraversal:
3740 debug.DumpTraversal
3741 """""""""""""""""""
3742 Print branch conditions as they are traversed by the engine.
3744 .. _debug-ExprInspection:
3746 debug.ExprInspection
3747 """"""""""""""""""""
3748 Check the analyzer's understanding of expressions.
3750 .. _debug-Stats:
3752 debug.Stats
3753 """""""""""
3754 Emit warnings with analyzer statistics.
3756 .. _debug-TaintTest:
3758 debug.TaintTest
3759 """""""""""""""
3760 Mark tainted symbols as such.
3762 .. _debug-ViewCFG:
3764 debug.ViewCFG
3765 """""""""""""
3766 View Control-Flow Graphs using GraphViz.
3768 .. _debug-ViewCallGraph:
3770 debug.ViewCallGraph
3771 """""""""""""""""""
3772 View Call Graph using GraphViz.
3774 .. _debug-ViewExplodedGraph:
3776 debug.ViewExplodedGraph
3777 """""""""""""""""""""""
3778 View Exploded Graphs using GraphViz.