Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1 | ================== |
| 2 | Available Checkers |
| 3 | ================== |
| 4 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 5 | The analyzer performs checks that are categorized into families or "checkers". |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 6 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 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. |
| 9 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 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. |
| 12 | |
| 13 | The :ref:`debug-checkers` package contains checkers for analyzer developers for debugging purposes. |
| 14 | |
| 15 | .. contents:: Table of Contents |
| 16 | :depth: 4 |
| 17 | |
| 18 | |
| 19 | .. _default-checkers: |
| 20 | |
| 21 | Default Checkers |
| 22 | ---------------- |
| 23 | |
| 24 | .. _core-checkers: |
| 25 | |
| 26 | core |
| 27 | ^^^^ |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 28 | Models core language features and contains general-purpose checkers such as division by zero, |
| 29 | null pointer dereference, usage of uninitialized values, etc. |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 30 | *These checkers must be always switched on as other checker rely on them.* |
| 31 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 32 | .. _core-CallAndMessage: |
| 33 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 34 | core.CallAndMessage (C, C++, ObjC) |
| 35 | """""""""""""""""""""""""""""""""" |
| 36 | Check for logical errors for function calls and Objective-C message expressions (e.g., uninitialized arguments, null function pointers). |
| 37 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 38 | .. literalinclude:: checkers/callandmessage_example.c |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 39 | :language: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 40 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 41 | .. _core-DivideZero: |
| 42 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 43 | core.DivideZero (C, C++, ObjC) |
| 44 | """""""""""""""""""""""""""""" |
| 45 | Check for division by zero. |
| 46 | |
| 47 | .. literalinclude:: checkers/dividezero_example.c |
| 48 | :language: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 49 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 50 | .. _core-NonNullParamChecker: |
| 51 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 52 | core.NonNullParamChecker (C, C++, ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 53 | """"""""""""""""""""""""""""""""""""""" |
| 54 | Check for null pointers passed as arguments to a function whose arguments are references or marked with the 'nonnull' attribute. |
| 55 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 56 | .. code-block:: cpp |
| 57 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 58 | int f(int *p) __attribute__((nonnull)); |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 59 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 60 | void test(int *p) { |
| 61 | if (!p) |
| 62 | f(p); // warn |
| 63 | } |
| 64 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 65 | .. _core-NullDereference: |
| 66 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 67 | core.NullDereference (C, C++, ObjC) |
| 68 | """"""""""""""""""""""""""""""""""" |
| 69 | Check for dereferences of null pointers. |
| 70 | |
| 71 | .. code-block:: objc |
| 72 | |
| 73 | // C |
| 74 | void test(int *p) { |
| 75 | if (p) |
| 76 | return; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 77 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 78 | int x = p[0]; // warn |
| 79 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 80 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 81 | // C |
| 82 | void test(int *p) { |
| 83 | if (!p) |
| 84 | *p = 0; // warn |
| 85 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 86 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 87 | // C++ |
| 88 | class C { |
| 89 | public: |
| 90 | int x; |
| 91 | }; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 92 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 93 | void test() { |
| 94 | C *pc = 0; |
| 95 | int k = pc->x; // warn |
| 96 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 97 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 98 | // Objective-C |
| 99 | @interface MyClass { |
| 100 | @public |
| 101 | int x; |
| 102 | } |
| 103 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 104 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 105 | void test() { |
| 106 | MyClass *obj = 0; |
| 107 | obj->x = 1; // warn |
| 108 | } |
| 109 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 110 | .. _core-StackAddressEscape: |
| 111 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 112 | core.StackAddressEscape (C) |
| 113 | """"""""""""""""""""""""""" |
| 114 | Check that addresses to stack memory do not escape the function. |
| 115 | |
| 116 | .. code-block:: c |
| 117 | |
| 118 | char const *p; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 119 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 120 | void test() { |
| 121 | char const str[] = "string"; |
| 122 | p = str; // warn |
| 123 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 124 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 125 | void* test() { |
| 126 | return __builtin_alloca(12); // warn |
| 127 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 128 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 129 | void test() { |
| 130 | static int *x; |
| 131 | int y; |
| 132 | x = &y; // warn |
| 133 | } |
| 134 | |
| 135 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 136 | .. _core-UndefinedBinaryOperatorResult: |
| 137 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 138 | core.UndefinedBinaryOperatorResult (C) |
| 139 | """""""""""""""""""""""""""""""""""""" |
| 140 | Check for undefined results of binary operators. |
| 141 | |
| 142 | .. code-block:: c |
| 143 | |
| 144 | void test() { |
| 145 | int x; |
| 146 | int y = x + 1; // warn: left operand is garbage |
| 147 | } |
| 148 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 149 | .. _core-VLASize: |
| 150 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 151 | core.VLASize (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 152 | """""""""""""""" |
| 153 | Check for declarations of Variable Length Arrays of undefined or zero size. |
| 154 | |
| 155 | Check for declarations of VLA of undefined or zero size. |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 156 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 157 | .. code-block:: c |
| 158 | |
| 159 | void test() { |
| 160 | int x; |
| 161 | int vla1[x]; // warn: garbage as size |
| 162 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 163 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 164 | void test() { |
| 165 | int x = 0; |
| 166 | int vla2[x]; // warn: zero size |
| 167 | } |
| 168 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 169 | .. _core-uninitialized-ArraySubscript: |
| 170 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 171 | core.uninitialized.ArraySubscript (C) |
| 172 | """"""""""""""""""""""""""""""""""""" |
| 173 | Check for uninitialized values used as array subscripts. |
| 174 | |
| 175 | .. code-block:: c |
| 176 | |
| 177 | void test() { |
| 178 | int i, a[10]; |
| 179 | int x = a[i]; // warn: array subscript is undefined |
| 180 | } |
| 181 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 182 | .. _core-uninitialized-Assign: |
| 183 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 184 | core.uninitialized.Assign (C) |
| 185 | """"""""""""""""""""""""""""" |
| 186 | Check for assigning uninitialized values. |
| 187 | |
| 188 | .. code-block:: c |
| 189 | |
| 190 | void test() { |
| 191 | int x; |
| 192 | x |= 1; // warn: left expression is uninitialized |
| 193 | } |
| 194 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 195 | .. _core-uninitialized-Branch: |
| 196 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 197 | core.uninitialized.Branch (C) |
| 198 | """"""""""""""""""""""""""""" |
| 199 | Check for uninitialized values used as branch conditions. |
| 200 | |
| 201 | .. code-block:: c |
| 202 | |
| 203 | void test() { |
| 204 | int x; |
| 205 | if (x) // warn |
| 206 | return; |
| 207 | } |
| 208 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 209 | .. _core-uninitialized-CapturedBlockVariable: |
| 210 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 211 | core.uninitialized.CapturedBlockVariable (C) |
| 212 | """""""""""""""""""""""""""""""""""""""""""" |
| 213 | Check for blocks that capture uninitialized values. |
| 214 | |
| 215 | .. code-block:: c |
| 216 | |
| 217 | void test() { |
| 218 | int x; |
| 219 | ^{ int y = x; }(); // warn |
| 220 | } |
| 221 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 222 | .. _core-uninitialized-UndefReturn: |
| 223 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 224 | core.uninitialized.UndefReturn (C) |
| 225 | """""""""""""""""""""""""""""""""" |
| 226 | Check for uninitialized values being returned to the caller. |
| 227 | |
| 228 | .. code-block:: c |
| 229 | |
| 230 | int test() { |
| 231 | int x; |
| 232 | return x; // warn |
| 233 | } |
| 234 | |
| 235 | .. _cplusplus-checkers: |
| 236 | |
| 237 | |
Mandeep Singh Grang | 0cdc5dd | 2019-05-24 19:24:08 +0000 | [diff] [blame] | 238 | cplusplus |
| 239 | ^^^^^^^^^ |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 240 | |
| 241 | C++ Checkers. |
| 242 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 243 | .. _cplusplus-InnerPointer: |
| 244 | |
Kristof Umann | e6e133b | 2019-08-15 08:52:10 +0000 | [diff] [blame] | 245 | cplusplus.InnerPointer (C++) |
| 246 | """""""""""""""""""""""""""" |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 247 | Check for inner pointers of C++ containers used after re/deallocation. |
| 248 | |
Kristof Umann | e6e133b | 2019-08-15 08:52:10 +0000 | [diff] [blame] | 249 | Many container methods in the C++ standard library are known to invalidate |
| 250 | "references" (including actual references, iterators and raw pointers) to |
| 251 | elements of the container. Using such references after they are invalidated |
| 252 | causes undefined behavior, which is a common source of memory errors in C++ that |
| 253 | this checker is capable of finding. |
| 254 | |
| 255 | The checker is currently limited to ``std::string`` objects and doesn't |
| 256 | recognize some of the more sophisticated approaches to passing unowned pointers |
| 257 | around, such as ``std::string_view``. |
| 258 | |
| 259 | .. code-block:: cpp |
| 260 | |
| 261 | void deref_after_assignment() { |
| 262 | std::string s = "llvm"; |
| 263 | const char *c = s.data(); // note: pointer to inner buffer of 'std::string' obtained here |
| 264 | s = "clang"; // note: inner buffer of 'std::string' reallocated by call to 'operator=' |
| 265 | consume(c); // warn: inner pointer of container used after re/deallocation |
| 266 | } |
| 267 | |
| 268 | const char *return_temp(int x) { |
| 269 | return std::to_string(x).c_str(); // warn: inner pointer of container used after re/deallocation |
| 270 | // note: pointer to inner buffer of 'std::string' obtained here |
| 271 | // note: inner buffer of 'std::string' deallocated by call to destructor |
| 272 | } |
| 273 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 274 | .. _cplusplus-NewDelete: |
| 275 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 276 | cplusplus.NewDelete (C++) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 277 | """"""""""""""""""""""""" |
| 278 | Check for double-free and use-after-free problems. Traces memory managed by new/delete. |
| 279 | |
| 280 | .. literalinclude:: checkers/newdelete_example.cpp |
| 281 | :language: cpp |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 282 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 283 | .. _cplusplus-NewDeleteLeaks: |
| 284 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 285 | cplusplus.NewDeleteLeaks (C++) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 286 | """""""""""""""""""""""""""""" |
| 287 | Check for memory leaks. Traces memory managed by new/delete. |
| 288 | |
| 289 | .. code-block:: cpp |
| 290 | |
| 291 | void test() { |
| 292 | int *p = new int; |
| 293 | } // warn |
| 294 | |
| 295 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 296 | .. _cplusplus-SelfAssignment: |
| 297 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 298 | cplusplus.SelfAssignment (C++) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 299 | """""""""""""""""""""""""""""" |
| 300 | Checks C++ copy and move assignment operators for self assignment. |
| 301 | |
| 302 | .. _deadcode-checkers: |
| 303 | |
| 304 | deadcode |
| 305 | ^^^^^^^^ |
| 306 | |
| 307 | Dead Code Checkers. |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 308 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 309 | .. _deadcode-DeadStores: |
| 310 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 311 | deadcode.DeadStores (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 312 | """"""""""""""""""""""" |
| 313 | Check for values stored to variables that are never read afterwards. |
| 314 | |
| 315 | .. code-block:: c |
| 316 | |
| 317 | void test() { |
| 318 | int x; |
| 319 | x = 1; // warn |
| 320 | } |
| 321 | |
Kristof Umann | 3b18b05 | 2019-09-03 15:22:43 +0000 | [diff] [blame] | 322 | The ``WarnForDeadNestedAssignments`` option enables the checker to emit |
| 323 | warnings for nested dead assignments. You can disable with the |
| 324 | ``-analyzer-config deadcode.DeadStores:WarnForDeadNestedAssignments=false``. |
| 325 | *Defaults to true*. |
| 326 | |
| 327 | Would warn for this e.g.: |
| 328 | if ((y = make_int())) { |
| 329 | } |
| 330 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 331 | .. _nullability-checkers: |
| 332 | |
| 333 | nullability |
| 334 | ^^^^^^^^^^^ |
| 335 | |
| 336 | Objective C checkers that warn for null pointer passing and dereferencing errors. |
| 337 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 338 | .. _nullability-NullPassedToNonnull: |
| 339 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 340 | nullability.NullPassedToNonnull (ObjC) |
| 341 | """""""""""""""""""""""""""""""""""""" |
| 342 | Warns when a null pointer is passed to a pointer which has a _Nonnull type. |
| 343 | |
| 344 | .. code-block:: objc |
| 345 | |
| 346 | if (name != nil) |
| 347 | return; |
| 348 | // Warning: nil passed to a callee that requires a non-null 1st parameter |
| 349 | NSString *greeting = [@"Hello " stringByAppendingString:name]; |
| 350 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 351 | .. _nullability-NullReturnedFromNonnull: |
| 352 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 353 | nullability.NullReturnedFromNonnull (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 354 | """""""""""""""""""""""""""""""""""""""""" |
| 355 | Warns when a null pointer is returned from a function that has _Nonnull return type. |
| 356 | |
| 357 | .. code-block:: objc |
| 358 | |
| 359 | - (nonnull id)firstChild { |
| 360 | id result = nil; |
| 361 | if ([_children count] > 0) |
| 362 | result = _children[0]; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 363 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 364 | // Warning: nil returned from a method that is expected |
| 365 | // to return a non-null value |
| 366 | return result; |
| 367 | } |
| 368 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 369 | .. _nullability-NullableDereferenced: |
| 370 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 371 | nullability.NullableDereferenced (ObjC) |
| 372 | """"""""""""""""""""""""""""""""""""""" |
| 373 | Warns when a nullable pointer is dereferenced. |
| 374 | |
| 375 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 376 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 377 | struct LinkedList { |
| 378 | int data; |
| 379 | struct LinkedList *next; |
| 380 | }; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 381 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 382 | struct LinkedList * _Nullable getNext(struct LinkedList *l); |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 383 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 384 | void updateNextData(struct LinkedList *list, int newData) { |
| 385 | struct LinkedList *next = getNext(list); |
| 386 | // Warning: Nullable pointer is dereferenced |
| 387 | next->data = 7; |
| 388 | } |
| 389 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 390 | .. _nullability-NullablePassedToNonnull: |
| 391 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 392 | nullability.NullablePassedToNonnull (ObjC) |
| 393 | """""""""""""""""""""""""""""""""""""""""" |
| 394 | Warns when a nullable pointer is passed to a pointer which has a _Nonnull type. |
| 395 | |
| 396 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 397 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 398 | typedef struct Dummy { int val; } Dummy; |
| 399 | Dummy *_Nullable returnsNullable(); |
| 400 | void takesNonnull(Dummy *_Nonnull); |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 401 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 402 | void test() { |
| 403 | Dummy *p = returnsNullable(); |
| 404 | takesNonnull(p); // warn |
| 405 | } |
| 406 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 407 | .. _nullability-NullableReturnedFromNonnull: |
| 408 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 409 | nullability.NullableReturnedFromNonnull (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 410 | """""""""""""""""""""""""""""""""""""""""""""" |
| 411 | Warns when a nullable pointer is returned from a function that has _Nonnull return type. |
| 412 | |
| 413 | .. _optin-checkers: |
| 414 | |
| 415 | optin |
| 416 | ^^^^^ |
| 417 | |
| 418 | Checkers for portability, performance or coding style specific rules. |
| 419 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 420 | .. _optin-cplusplus-UninitializedObject: |
| 421 | |
Kristof Umann | 85e0ff7 | 2019-04-19 23:33:50 +0000 | [diff] [blame] | 422 | optin.cplusplus.UninitializedObject (C++) |
Nico Weber | 83c95b1 | 2019-05-03 18:54:18 +0000 | [diff] [blame] | 423 | """"""""""""""""""""""""""""""""""""""""" |
Kristof Umann | 85e0ff7 | 2019-04-19 23:33:50 +0000 | [diff] [blame] | 424 | |
| 425 | This checker reports uninitialized fields in objects created after a constructor |
| 426 | call. It doesn't only find direct uninitialized fields, but rather makes a deep |
| 427 | inspection of the object, analyzing all of it's fields subfields. |
| 428 | The checker regards inherited fields as direct fields, so one will recieve |
| 429 | warnings for uninitialized inherited data members as well. |
| 430 | |
| 431 | .. code-block:: cpp |
| 432 | |
| 433 | // With Pedantic and CheckPointeeInitialization set to true |
| 434 | |
| 435 | struct A { |
| 436 | struct B { |
| 437 | int x; // note: uninitialized field 'this->b.x' |
| 438 | // note: uninitialized field 'this->bptr->x' |
| 439 | int y; // note: uninitialized field 'this->b.y' |
| 440 | // note: uninitialized field 'this->bptr->y' |
| 441 | }; |
| 442 | int *iptr; // note: uninitialized pointer 'this->iptr' |
| 443 | B b; |
| 444 | B *bptr; |
| 445 | char *cptr; // note: uninitialized pointee 'this->cptr' |
| 446 | |
| 447 | A (B *bptr, char *cptr) : bptr(bptr), cptr(cptr) {} |
| 448 | }; |
| 449 | |
| 450 | void f() { |
| 451 | A::B b; |
| 452 | char c; |
| 453 | A a(&b, &c); // warning: 6 uninitialized fields |
| 454 | // after the constructor call |
| 455 | } |
| 456 | |
| 457 | // With Pedantic set to false and |
| 458 | // CheckPointeeInitialization set to true |
| 459 | // (every field is uninitialized) |
| 460 | |
| 461 | struct A { |
| 462 | struct B { |
| 463 | int x; |
| 464 | int y; |
| 465 | }; |
| 466 | int *iptr; |
| 467 | B b; |
| 468 | B *bptr; |
| 469 | char *cptr; |
| 470 | |
| 471 | A (B *bptr, char *cptr) : bptr(bptr), cptr(cptr) {} |
| 472 | }; |
| 473 | |
| 474 | void f() { |
| 475 | A::B b; |
| 476 | char c; |
| 477 | A a(&b, &c); // no warning |
| 478 | } |
| 479 | |
| 480 | // With Pedantic set to true and |
| 481 | // CheckPointeeInitialization set to false |
| 482 | // (pointees are regarded as initialized) |
| 483 | |
| 484 | struct A { |
| 485 | struct B { |
| 486 | int x; // note: uninitialized field 'this->b.x' |
| 487 | int y; // note: uninitialized field 'this->b.y' |
| 488 | }; |
| 489 | int *iptr; // note: uninitialized pointer 'this->iptr' |
| 490 | B b; |
| 491 | B *bptr; |
| 492 | char *cptr; |
| 493 | |
| 494 | A (B *bptr, char *cptr) : bptr(bptr), cptr(cptr) {} |
| 495 | }; |
| 496 | |
| 497 | void f() { |
| 498 | A::B b; |
| 499 | char c; |
| 500 | A a(&b, &c); // warning: 3 uninitialized fields |
| 501 | // after the constructor call |
| 502 | } |
| 503 | |
| 504 | |
| 505 | **Options** |
| 506 | |
| 507 | This checker has several options which can be set from command line (e.g. |
| 508 | ``-analyzer-config optin.cplusplus.UninitializedObject:Pedantic=true``): |
| 509 | |
| 510 | * ``Pedantic`` (boolean). If to false, the checker won't emit warnings for |
| 511 | objects that don't have at least one initialized field. Defaults to false. |
| 512 | |
| 513 | * ``NotesAsWarnings`` (boolean). If set to true, the checker will emit a |
| 514 | warning for each uninitalized field, as opposed to emitting one warning per |
| 515 | constructor call, and listing the uninitialized fields that belongs to it in |
| 516 | notes. *Defaults to false*. |
| 517 | |
| 518 | * ``CheckPointeeInitialization`` (boolean). If set to false, the checker will |
| 519 | not analyze the pointee of pointer/reference fields, and will only check |
| 520 | whether the object itself is initialized. *Defaults to false*. |
| 521 | |
| 522 | * ``IgnoreRecordsWithField`` (string). If supplied, the checker will not analyze |
| 523 | structures that have a field with a name or type name that matches the given |
| 524 | pattern. *Defaults to ""*. |
| 525 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 526 | .. _optin-cplusplus-VirtualCall: |
| 527 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 528 | optin.cplusplus.VirtualCall (C++) |
| 529 | """"""""""""""""""""""""""""""""" |
| 530 | Check virtual function calls during construction or destruction. |
| 531 | |
| 532 | .. code-block:: cpp |
| 533 | |
| 534 | class A { |
| 535 | public: |
| 536 | A() { |
| 537 | f(); // warn |
| 538 | } |
| 539 | virtual void f(); |
| 540 | }; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 541 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 542 | class A { |
| 543 | public: |
| 544 | ~A() { |
| 545 | this->f(); // warn |
| 546 | } |
| 547 | virtual void f(); |
| 548 | }; |
| 549 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 550 | .. _optin-mpi-MPI-Checker: |
| 551 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 552 | optin.mpi.MPI-Checker (C) |
| 553 | """"""""""""""""""""""""" |
| 554 | Checks MPI code. |
| 555 | |
| 556 | .. code-block:: c |
| 557 | |
| 558 | void test() { |
| 559 | double buf = 0; |
| 560 | MPI_Request sendReq1; |
| 561 | MPI_Ireduce(MPI_IN_PLACE, &buf, 1, MPI_DOUBLE, MPI_SUM, |
| 562 | 0, MPI_COMM_WORLD, &sendReq1); |
| 563 | } // warn: request 'sendReq1' has no matching wait. |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 564 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 565 | void test() { |
| 566 | double buf = 0; |
| 567 | MPI_Request sendReq; |
| 568 | MPI_Isend(&buf, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &sendReq); |
| 569 | MPI_Irecv(&buf, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &sendReq); // warn |
| 570 | MPI_Isend(&buf, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &sendReq); // warn |
| 571 | MPI_Wait(&sendReq, MPI_STATUS_IGNORE); |
| 572 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 573 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 574 | void missingNonBlocking() { |
| 575 | int rank = 0; |
| 576 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 577 | MPI_Request sendReq1[10][10][10]; |
| 578 | MPI_Wait(&sendReq1[1][7][9], MPI_STATUS_IGNORE); // warn |
| 579 | } |
| 580 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 581 | .. _optin-osx-cocoa-localizability-EmptyLocalizationContextChecker: |
| 582 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 583 | optin.osx.cocoa.localizability.EmptyLocalizationContextChecker (ObjC) |
| 584 | """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" |
| 585 | Check that NSLocalizedString macros include a comment for context. |
| 586 | |
| 587 | .. code-block:: objc |
| 588 | |
| 589 | - (void)test { |
| 590 | NSString *string = NSLocalizedString(@"LocalizedString", nil); // warn |
| 591 | NSString *string2 = NSLocalizedString(@"LocalizedString", @" "); // warn |
| 592 | NSString *string3 = NSLocalizedStringWithDefaultValue( |
| 593 | @"LocalizedString", nil, [[NSBundle alloc] init], nil,@""); // warn |
| 594 | } |
| 595 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 596 | .. _optin-osx-cocoa-localizability-NonLocalizedStringChecker: |
| 597 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 598 | optin.osx.cocoa.localizability.NonLocalizedStringChecker (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 599 | """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" |
| 600 | Warns about uses of non-localized NSStrings passed to UI methods expecting localized NSStrings. |
| 601 | |
| 602 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 603 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 604 | NSString *alarmText = |
| 605 | NSLocalizedString(@"Enabled", @"Indicates alarm is turned on"); |
| 606 | if (!isEnabled) { |
| 607 | alarmText = @"Disabled"; |
| 608 | } |
| 609 | UILabel *alarmStateLabel = [[UILabel alloc] init]; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 610 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 611 | // Warning: User-facing text should use localized string macro |
| 612 | [alarmStateLabel setText:alarmText]; |
| 613 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 614 | .. _optin-performance-GCDAntipattern: |
| 615 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 616 | optin.performance.GCDAntipattern |
| 617 | """""""""""""""""""""""""""""""" |
| 618 | Check for performance anti-patterns when using Grand Central Dispatch. |
| 619 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 620 | .. _optin-performance-Padding: |
| 621 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 622 | optin.performance.Padding |
| 623 | """"""""""""""""""""""""" |
| 624 | Check for excessively padded structs. |
| 625 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 626 | .. _optin-portability-UnixAPI: |
| 627 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 628 | optin.portability.UnixAPI |
| 629 | """"""""""""""""""""""""" |
| 630 | Finds implementation-defined behavior in UNIX/Posix functions. |
| 631 | |
| 632 | |
| 633 | .. _security-checkers: |
| 634 | |
| 635 | security |
| 636 | ^^^^^^^^ |
| 637 | |
| 638 | Security related checkers. |
| 639 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 640 | .. _security-FloatLoopCounter: |
| 641 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 642 | security.FloatLoopCounter (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 643 | """"""""""""""""""""""""""""" |
| 644 | Warn on using a floating point value as a loop counter (CERT: FLP30-C, FLP30-CPP). |
| 645 | |
| 646 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 647 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 648 | void test() { |
| 649 | for (float x = 0.1f; x <= 1.0f; x += 0.1f) {} // warn |
| 650 | } |
| 651 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 652 | .. _security-insecureAPI-UncheckedReturn: |
| 653 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 654 | security.insecureAPI.UncheckedReturn (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 655 | """""""""""""""""""""""""""""""""""""""" |
| 656 | Warn on uses of functions whose return values must be always checked. |
| 657 | |
| 658 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 659 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 660 | void test() { |
| 661 | setuid(1); // warn |
| 662 | } |
| 663 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 664 | .. _security-insecureAPI-bcmp: |
| 665 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 666 | security.insecureAPI.bcmp (C) |
| 667 | """"""""""""""""""""""""""""" |
| 668 | Warn on uses of the 'bcmp' function. |
| 669 | |
| 670 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 671 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 672 | void test() { |
| 673 | bcmp(ptr0, ptr1, n); // warn |
| 674 | } |
| 675 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 676 | .. _security-insecureAPI-bcopy: |
| 677 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 678 | security.insecureAPI.bcopy (C) |
| 679 | """""""""""""""""""""""""""""" |
| 680 | Warn on uses of the 'bcopy' function. |
| 681 | |
| 682 | .. code-block:: c |
| 683 | |
| 684 | void test() { |
| 685 | bcopy(src, dst, n); // warn |
| 686 | } |
| 687 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 688 | .. _security-insecureAPI-bzero: |
| 689 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 690 | security.insecureAPI.bzero (C) |
| 691 | """""""""""""""""""""""""""""" |
| 692 | Warn on uses of the 'bzero' function. |
| 693 | |
| 694 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 695 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 696 | void test() { |
| 697 | bzero(ptr, n); // warn |
| 698 | } |
| 699 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 700 | .. _security-insecureAPI-getpw: |
| 701 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 702 | security.insecureAPI.getpw (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 703 | """""""""""""""""""""""""""""" |
| 704 | Warn on uses of the 'getpw' function. |
| 705 | |
| 706 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 707 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 708 | void test() { |
| 709 | char buff[1024]; |
| 710 | getpw(2, buff); // warn |
| 711 | } |
| 712 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 713 | .. _security-insecureAPI-gets: |
| 714 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 715 | security.insecureAPI.gets (C) |
| 716 | """"""""""""""""""""""""""""" |
| 717 | Warn on uses of the 'gets' function. |
| 718 | |
| 719 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 720 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 721 | void test() { |
| 722 | char buff[1024]; |
| 723 | gets(buff); // warn |
| 724 | } |
| 725 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 726 | .. _security-insecureAPI-mkstemp: |
| 727 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 728 | security.insecureAPI.mkstemp (C) |
| 729 | """""""""""""""""""""""""""""""" |
| 730 | Warn when 'mkstemp' is passed fewer than 6 X's in the format string. |
| 731 | |
| 732 | .. code-block:: c |
| 733 | |
| 734 | void test() { |
| 735 | mkstemp("XX"); // warn |
| 736 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 737 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 738 | .. _security-insecureAPI-mktemp: |
| 739 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 740 | security.insecureAPI.mktemp (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 741 | """"""""""""""""""""""""""""""" |
| 742 | Warn on uses of the ``mktemp`` function. |
| 743 | |
| 744 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 745 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 746 | void test() { |
| 747 | char *x = mktemp("/tmp/zxcv"); // warn: insecure, use mkstemp |
| 748 | } |
| 749 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 750 | .. _security-insecureAPI-rand: |
| 751 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 752 | security.insecureAPI.rand (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 753 | """"""""""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 754 | Warn on uses of inferior random number generating functions (only if arc4random function is available): |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 755 | ``drand48, erand48, jrand48, lcong48, lrand48, mrand48, nrand48, random, rand_r``. |
| 756 | |
| 757 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 758 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 759 | void test() { |
| 760 | random(); // warn |
| 761 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 762 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 763 | .. _security-insecureAPI-strcpy: |
| 764 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 765 | security.insecureAPI.strcpy (C) |
| 766 | """"""""""""""""""""""""""""""" |
| 767 | Warn on uses of the ``strcpy`` and ``strcat`` functions. |
| 768 | |
| 769 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 770 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 771 | void test() { |
| 772 | char x[4]; |
| 773 | char *y = "abcd"; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 774 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 775 | strcpy(x, y); // warn |
| 776 | } |
| 777 | |
| 778 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 779 | .. _security-insecureAPI-vfork: |
| 780 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 781 | security.insecureAPI.vfork (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 782 | """""""""""""""""""""""""""""" |
| 783 | Warn on uses of the 'vfork' function. |
| 784 | |
| 785 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 786 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 787 | void test() { |
| 788 | vfork(); // warn |
| 789 | } |
| 790 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 791 | .. _security-insecureAPI-DeprecatedOrUnsafeBufferHandling: |
| 792 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 793 | security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C) |
| 794 | """"""""""""""""""""""""""""""""""""""""""""""""""""""""" |
Kristof Umann | 8d23999 | 2019-02-11 13:46:43 +0000 | [diff] [blame] | 795 | Warn on occurrences of unsafe or deprecated buffer handling functions, which now have a secure variant: ``sprintf, vsprintf, scanf, wscanf, fscanf, fwscanf, vscanf, vwscanf, vfscanf, vfwscanf, sscanf, swscanf, vsscanf, vswscanf, swprintf, snprintf, vswprintf, vsnprintf, memcpy, memmove, strncpy, strncat, memset`` |
| 796 | |
| 797 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 798 | |
Kristof Umann | 8d23999 | 2019-02-11 13:46:43 +0000 | [diff] [blame] | 799 | void test() { |
| 800 | char buf [5]; |
| 801 | strncpy(buf, "a", 1); // warn |
| 802 | } |
| 803 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 804 | .. _unix-checkers: |
| 805 | |
| 806 | unix |
| 807 | ^^^^ |
| 808 | POSIX/Unix checkers. |
| 809 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 810 | .. _unix-API: |
| 811 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 812 | unix.API (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 813 | """""""""""" |
| 814 | Check calls to various UNIX/Posix functions: ``open, pthread_once, calloc, malloc, realloc, alloca``. |
| 815 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 816 | .. literalinclude:: checkers/unix_api_example.c |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 817 | :language: c |
| 818 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 819 | .. _unix-Malloc: |
| 820 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 821 | unix.Malloc (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 822 | """"""""""""""" |
| 823 | Check for memory leaks, double free, and use-after-free problems. Traces memory managed by malloc()/free(). |
| 824 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 825 | .. literalinclude:: checkers/unix_malloc_example.c |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 826 | :language: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 827 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 828 | .. _unix-MallocSizeof: |
| 829 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 830 | unix.MallocSizeof (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 831 | """"""""""""""""""""" |
| 832 | Check for dubious ``malloc`` arguments involving ``sizeof``. |
| 833 | |
| 834 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 835 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 836 | void test() { |
| 837 | long *p = malloc(sizeof(short)); |
| 838 | // warn: result is converted to 'long *', which is |
| 839 | // incompatible with operand type 'short' |
| 840 | free(p); |
| 841 | } |
| 842 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 843 | .. _unix-MismatchedDeallocator: |
| 844 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 845 | unix.MismatchedDeallocator (C, C++) |
| 846 | """"""""""""""""""""""""""""""""""" |
| 847 | Check for mismatched deallocators. |
| 848 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 849 | .. literalinclude:: checkers/mismatched_deallocator_example.cpp |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 850 | :language: c |
| 851 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 852 | .. _unix-Vfork: |
| 853 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 854 | unix.Vfork (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 855 | """""""""""""" |
| 856 | Check for proper usage of ``vfork``. |
| 857 | |
| 858 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 859 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 860 | int test(int x) { |
| 861 | pid_t pid = vfork(); // warn |
| 862 | if (pid != 0) |
| 863 | return 0; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 864 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 865 | switch (x) { |
| 866 | case 0: |
| 867 | pid = 1; |
| 868 | execl("", "", 0); |
| 869 | _exit(1); |
| 870 | break; |
| 871 | case 1: |
| 872 | x = 0; // warn: this assignment is prohibited |
| 873 | break; |
| 874 | case 2: |
| 875 | foo(); // warn: this function call is prohibited |
| 876 | break; |
| 877 | default: |
| 878 | return 0; // warn: return is prohibited |
| 879 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 880 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 881 | while(1); |
| 882 | } |
| 883 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 884 | .. _unix-cstring-BadSizeArg: |
| 885 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 886 | unix.cstring.BadSizeArg (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 887 | """"""""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 888 | 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. |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 889 | |
| 890 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 891 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 892 | void test() { |
| 893 | char dest[3]; |
| 894 | strncat(dest, """""""""""""""""""""""""*", sizeof(dest)); |
| 895 | // warn: potential buffer overflow |
| 896 | } |
| 897 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 898 | .. _unix-cstrisng-NullArg: |
| 899 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 900 | unix.cstrisng.NullArg (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 901 | """"""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 902 | Check for null pointers being passed as arguments to C string functions: |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 903 | ``strlen, strnlen, strcpy, strncpy, strcat, strncat, strcmp, strncmp, strcasecmp, strncasecmp``. |
| 904 | |
| 905 | .. code-block:: c |
| 906 | |
| 907 | int test() { |
| 908 | return strlen(0); // warn |
| 909 | } |
| 910 | |
| 911 | .. _osx-checkers: |
| 912 | |
| 913 | osx |
| 914 | ^^^ |
J. Ryan Stinnett | d45eaf9 | 2019-05-30 16:46:22 +0000 | [diff] [blame] | 915 | macOS checkers. |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 916 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 917 | .. _osx-API: |
| 918 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 919 | osx.API (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 920 | """"""""""" |
| 921 | Check for proper uses of various Apple APIs. |
| 922 | |
| 923 | .. code-block:: objc |
| 924 | |
| 925 | void test() { |
| 926 | dispatch_once_t pred = 0; |
| 927 | dispatch_once(&pred, ^(){}); // warn: dispatch_once uses local |
| 928 | } |
| 929 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 930 | .. _osx-NumberObjectConversion: |
| 931 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 932 | osx.NumberObjectConversion (C, C++, ObjC) |
| 933 | """"""""""""""""""""""""""""""""""""""""" |
| 934 | Check for erroneous conversions of objects representing numbers into numbers. |
| 935 | |
| 936 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 937 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 938 | NSNumber *photoCount = [albumDescriptor objectForKey:@"PhotoCount"]; |
| 939 | // Warning: Comparing a pointer value of type 'NSNumber *' |
| 940 | // to a scalar integer value |
| 941 | if (photoCount > 0) { |
| 942 | [self displayPhotos]; |
| 943 | } |
| 944 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 945 | .. _osx-ObjCProperty: |
| 946 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 947 | osx.ObjCProperty (ObjC) |
| 948 | """"""""""""""""""""""" |
| 949 | Check for proper uses of Objective-C properties. |
| 950 | |
| 951 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 952 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 953 | NSNumber *photoCount = [albumDescriptor objectForKey:@"PhotoCount"]; |
| 954 | // Warning: Comparing a pointer value of type 'NSNumber *' |
| 955 | // to a scalar integer value |
| 956 | if (photoCount > 0) { |
| 957 | [self displayPhotos]; |
| 958 | } |
| 959 | |
| 960 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 961 | .. _osx-SecKeychainAPI: |
| 962 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 963 | osx.SecKeychainAPI (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 964 | """""""""""""""""""""" |
| 965 | Check for proper uses of Secure Keychain APIs. |
| 966 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 967 | .. literalinclude:: checkers/seckeychainapi_example.m |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 968 | :language: objc |
| 969 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 970 | .. _osx-cocoa-AtSync: |
| 971 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 972 | osx.cocoa.AtSync (ObjC) |
| 973 | """"""""""""""""""""""" |
| 974 | Check for nil pointers used as mutexes for @synchronized. |
| 975 | |
| 976 | .. code-block:: objc |
| 977 | |
| 978 | void test(id x) { |
| 979 | if (!x) |
| 980 | @synchronized(x) {} // warn: nil value used as mutex |
| 981 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 982 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 983 | void test() { |
| 984 | id y; |
| 985 | @synchronized(y) {} // warn: uninitialized value used as mutex |
| 986 | } |
| 987 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 988 | .. _osx-cocoa-AutoreleaseWrite: |
| 989 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 990 | osx.cocoa.AutoreleaseWrite |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 991 | """""""""""""""""""""""""" |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 992 | Warn about potentially crashing writes to autoreleasing objects from different autoreleasing pools in Objective-C. |
| 993 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 994 | .. _osx-cocoa-ClassRelease: |
| 995 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 996 | osx.cocoa.ClassRelease (ObjC) |
| 997 | """"""""""""""""""""""""""""" |
| 998 | Check for sending 'retain', 'release', or 'autorelease' directly to a Class. |
| 999 | |
| 1000 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1001 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1002 | @interface MyClass : NSObject |
| 1003 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1004 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1005 | void test(void) { |
| 1006 | [MyClass release]; // warn |
| 1007 | } |
| 1008 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1009 | .. _osx-cocoa-Dealloc: |
| 1010 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1011 | osx.cocoa.Dealloc (ObjC) |
| 1012 | """""""""""""""""""""""" |
| 1013 | Warn about Objective-C classes that lack a correct implementation of -dealloc |
| 1014 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1015 | .. literalinclude:: checkers/dealloc_example.m |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1016 | :language: objc |
| 1017 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1018 | .. _osx-cocoa-IncompatibleMethodTypes: |
| 1019 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1020 | osx.cocoa.IncompatibleMethodTypes (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1021 | """""""""""""""""""""""""""""""""""""""" |
| 1022 | Warn about Objective-C method signatures with type incompatibilities. |
| 1023 | |
| 1024 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1025 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1026 | @interface MyClass1 : NSObject |
| 1027 | - (int)foo; |
| 1028 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1029 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1030 | @implementation MyClass1 |
| 1031 | - (int)foo { return 1; } |
| 1032 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1033 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1034 | @interface MyClass2 : MyClass1 |
| 1035 | - (float)foo; |
| 1036 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1037 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1038 | @implementation MyClass2 |
| 1039 | - (float)foo { return 1.0; } // warn |
| 1040 | @end |
| 1041 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1042 | .. _osx-cocoa-Loops: |
| 1043 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1044 | osx.cocoa.Loops |
| 1045 | """"""""""""""" |
| 1046 | Improved modeling of loops using Cocoa collection types. |
| 1047 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1048 | .. _osx-cocoa-MissingSuperCall: |
| 1049 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1050 | osx.cocoa.MissingSuperCall (ObjC) |
| 1051 | """"""""""""""""""""""""""""""""" |
| 1052 | Warn about Objective-C methods that lack a necessary call to super. |
| 1053 | |
| 1054 | .. code-block:: objc |
| 1055 | |
| 1056 | @interface Test : UIViewController |
| 1057 | @end |
| 1058 | @implementation test |
| 1059 | - (void)viewDidLoad {} // warn |
| 1060 | @end |
| 1061 | |
| 1062 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1063 | .. _osx-cocoa-NSAutoreleasePool: |
| 1064 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1065 | osx.cocoa.NSAutoreleasePool (ObjC) |
| 1066 | """""""""""""""""""""""""""""""""" |
| 1067 | Warn for suboptimal uses of NSAutoreleasePool in Objective-C GC mode. |
| 1068 | |
| 1069 | .. code-block:: objc |
| 1070 | |
| 1071 | void test() { |
| 1072 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 1073 | [pool release]; // warn |
| 1074 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1075 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1076 | .. _osx-cocoa-NSError: |
| 1077 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1078 | osx.cocoa.NSError (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1079 | """""""""""""""""""""""" |
| 1080 | Check usage of NSError parameters. |
| 1081 | |
| 1082 | .. code-block:: objc |
| 1083 | |
| 1084 | @interface A : NSObject |
| 1085 | - (void)foo:(NSError """""""""""""""""""""""")error; |
| 1086 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1087 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1088 | @implementation A |
| 1089 | - (void)foo:(NSError """""""""""""""""""""""")error { |
| 1090 | // warn: method accepting NSError"""""""""""""""""""""""" should have a non-void |
| 1091 | // return value |
| 1092 | } |
| 1093 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1094 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1095 | @interface A : NSObject |
| 1096 | - (BOOL)foo:(NSError """""""""""""""""""""""")error; |
| 1097 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1098 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1099 | @implementation A |
| 1100 | - (BOOL)foo:(NSError """""""""""""""""""""""")error { |
| 1101 | *error = 0; // warn: potential null dereference |
| 1102 | return 0; |
| 1103 | } |
| 1104 | @end |
| 1105 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1106 | .. _osx-cocoa-NilArg: |
| 1107 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1108 | osx.cocoa.NilArg (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1109 | """"""""""""""""""""""" |
| 1110 | Check for prohibited nil arguments to ObjC method calls. |
| 1111 | |
| 1112 | - caseInsensitiveCompare: |
| 1113 | - compare: |
| 1114 | - compare:options: |
| 1115 | - compare:options:range: |
| 1116 | - compare:options:range:locale: |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1117 | - componentsSeparatedByCharactersInSet: |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1118 | - initWithFormat: |
| 1119 | |
| 1120 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1121 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1122 | NSComparisonResult test(NSString *s) { |
| 1123 | NSString *aString = nil; |
| 1124 | return [s caseInsensitiveCompare:aString]; |
| 1125 | // warn: argument to 'NSString' method |
| 1126 | // 'caseInsensitiveCompare:' cannot be nil |
| 1127 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1128 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1129 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1130 | .. _osx-cocoa-NonNilReturnValue: |
| 1131 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1132 | osx.cocoa.NonNilReturnValue |
| 1133 | """"""""""""""""""""""""""" |
| 1134 | Models the APIs that are guaranteed to return a non-nil value. |
| 1135 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1136 | .. _osx-cocoa-ObjCGenerics: |
| 1137 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1138 | osx.cocoa.ObjCGenerics (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1139 | """"""""""""""""""""""""""""" |
| 1140 | Check for type errors when using Objective-C generics. |
| 1141 | |
| 1142 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1143 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1144 | NSMutableArray *names = [NSMutableArray array]; |
| 1145 | NSMutableArray *birthDates = names; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1146 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1147 | // Warning: Conversion from value of type 'NSDate *' |
| 1148 | // to incompatible type 'NSString *' |
| 1149 | [birthDates addObject: [NSDate date]]; |
| 1150 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1151 | .. _osx-cocoa-RetainCount: |
| 1152 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1153 | osx.cocoa.RetainCount (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1154 | """""""""""""""""""""""""""" |
| 1155 | Check for leaks and improper reference count management |
| 1156 | |
| 1157 | .. code-block:: objc |
| 1158 | |
| 1159 | void test() { |
| 1160 | NSString *s = [[NSString alloc] init]; // warn |
| 1161 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1162 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1163 | CFStringRef test(char *bytes) { |
| 1164 | return CFStringCreateWithCStringNoCopy( |
| 1165 | 0, bytes, NSNEXTSTEPStringEncoding, 0); // warn |
| 1166 | } |
| 1167 | |
| 1168 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1169 | .. _osx-cocoa-RunLoopAutoreleaseLeak: |
| 1170 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1171 | osx.cocoa.RunLoopAutoreleaseLeak |
| 1172 | """""""""""""""""""""""""""""""" |
| 1173 | Check for leaked memory in autorelease pools that will never be drained. |
| 1174 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1175 | .. _osx-cocoa-SelfInit: |
| 1176 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1177 | osx.cocoa.SelfInit (ObjC) |
| 1178 | """"""""""""""""""""""""" |
| 1179 | Check that 'self' is properly initialized inside an initializer method. |
| 1180 | |
| 1181 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1182 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1183 | @interface MyObj : NSObject { |
| 1184 | id x; |
| 1185 | } |
| 1186 | - (id)init; |
| 1187 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1188 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1189 | @implementation MyObj |
| 1190 | - (id)init { |
| 1191 | [super init]; |
| 1192 | x = 0; // warn: instance variable used while 'self' is not |
| 1193 | // initialized |
| 1194 | return 0; |
| 1195 | } |
| 1196 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1197 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1198 | @interface MyObj : NSObject |
| 1199 | - (id)init; |
| 1200 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1201 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1202 | @implementation MyObj |
| 1203 | - (id)init { |
| 1204 | [super init]; |
| 1205 | return self; // warn: returning uninitialized 'self' |
| 1206 | } |
| 1207 | @end |
| 1208 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1209 | .. _osx-cocoa-SuperDealloc: |
| 1210 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1211 | osx.cocoa.SuperDealloc (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1212 | """"""""""""""""""""""""""""" |
| 1213 | Warn about improper use of '[super dealloc]' in Objective-C. |
| 1214 | |
| 1215 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1216 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1217 | @interface SuperDeallocThenReleaseIvarClass : NSObject { |
| 1218 | NSObject *_ivar; |
| 1219 | } |
| 1220 | @end |
| 1221 | |
| 1222 | @implementation SuperDeallocThenReleaseIvarClass |
| 1223 | - (void)dealloc { |
| 1224 | [super dealloc]; |
| 1225 | [_ivar release]; // warn |
| 1226 | } |
| 1227 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1228 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1229 | .. _osx-cocoa-UnusedIvars: |
| 1230 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1231 | osx.cocoa.UnusedIvars (ObjC) |
| 1232 | """""""""""""""""""""""""""" |
| 1233 | Warn about private ivars that are never used. |
| 1234 | |
| 1235 | .. code-block:: objc |
| 1236 | |
| 1237 | @interface MyObj : NSObject { |
| 1238 | @private |
| 1239 | id x; // warn |
| 1240 | } |
| 1241 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1242 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1243 | @implementation MyObj |
| 1244 | @end |
| 1245 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1246 | .. _osx-cocoa-VariadicMethodTypes: |
| 1247 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1248 | osx.cocoa.VariadicMethodTypes (ObjC) |
| 1249 | """""""""""""""""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1250 | Check for passing non-Objective-C types to variadic collection |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1251 | initialization methods that expect only Objective-C types. |
| 1252 | |
| 1253 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1254 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1255 | void test() { |
| 1256 | [NSSet setWithObjects:@"Foo", "Bar", nil]; |
| 1257 | // warn: argument should be an ObjC pointer type, not 'char *' |
| 1258 | } |
| 1259 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1260 | .. _osx-coreFoundation-CFError: |
| 1261 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1262 | osx.coreFoundation.CFError (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1263 | """""""""""""""""""""""""""""" |
| 1264 | Check usage of CFErrorRef* parameters |
| 1265 | |
| 1266 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1267 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1268 | void test(CFErrorRef *error) { |
| 1269 | // warn: function accepting CFErrorRef* should have a |
| 1270 | // non-void return |
| 1271 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1272 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1273 | int foo(CFErrorRef *error) { |
| 1274 | *error = 0; // warn: potential null dereference |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1278 | .. _osx-coreFoundation-CFNumber: |
| 1279 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1280 | osx.coreFoundation.CFNumber (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1281 | """"""""""""""""""""""""""""""" |
| 1282 | Check for proper uses of CFNumber APIs. |
| 1283 | |
| 1284 | .. code-block:: c |
| 1285 | |
| 1286 | CFNumberRef test(unsigned char x) { |
| 1287 | return CFNumberCreate(0, kCFNumberSInt16Type, &x); |
| 1288 | // warn: 8 bit integer is used to initialize a 16 bit integer |
| 1289 | } |
| 1290 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1291 | .. _osx-coreFoundation-CFRetainRelease: |
| 1292 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1293 | osx.coreFoundation.CFRetainRelease (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1294 | """""""""""""""""""""""""""""""""""""" |
| 1295 | Check for null arguments to CFRetain/CFRelease/CFMakeCollectable. |
| 1296 | |
| 1297 | .. code-block:: c |
| 1298 | |
| 1299 | void test(CFTypeRef p) { |
| 1300 | if (!p) |
| 1301 | CFRetain(p); // warn |
| 1302 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1303 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1304 | void test(int x, CFTypeRef p) { |
| 1305 | if (p) |
| 1306 | return; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1307 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1308 | CFRelease(p); // warn |
| 1309 | } |
| 1310 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1311 | .. _osx-coreFoundation-containers-OutOfBounds: |
| 1312 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1313 | osx.coreFoundation.containers.OutOfBounds (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1314 | """"""""""""""""""""""""""""""""""""""""""""" |
| 1315 | Checks for index out-of-bounds when using 'CFArray' API. |
| 1316 | |
| 1317 | .. code-block:: c |
| 1318 | |
| 1319 | void test() { |
| 1320 | CFArrayRef A = CFArrayCreate(0, 0, 0, &kCFTypeArrayCallBacks); |
| 1321 | CFArrayGetValueAtIndex(A, 0); // warn |
| 1322 | } |
| 1323 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1324 | .. _osx-coreFoundation-containers-PointerSizedValues: |
| 1325 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1326 | osx.coreFoundation.containers.PointerSizedValues (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1327 | """""""""""""""""""""""""""""""""""""""""""""""""""" |
| 1328 | Warns if 'CFArray', 'CFDictionary', 'CFSet' are created with non-pointer-size values. |
| 1329 | |
| 1330 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1331 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1332 | void test() { |
| 1333 | int x[] = { 1 }; |
| 1334 | CFArrayRef A = CFArrayCreate(0, (const void """""""""""""""""""""""")x, 1, |
| 1335 | &kCFTypeArrayCallBacks); // warn |
| 1336 | } |
| 1337 | |
| 1338 | |
| 1339 | .. _alpha-checkers: |
| 1340 | |
| 1341 | Experimental Checkers |
| 1342 | --------------------- |
| 1343 | |
| 1344 | *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.* |
| 1345 | |
| 1346 | alpha.clone |
| 1347 | ^^^^^^^^^^^ |
| 1348 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1349 | .. _alpha-clone-CloneChecker: |
| 1350 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1351 | alpha.clone.CloneChecker (C, C++, ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1352 | """"""""""""""""""""""""""""""""""""""" |
| 1353 | Reports similar pieces of code. |
| 1354 | |
| 1355 | .. code-block:: c |
| 1356 | |
| 1357 | void log(); |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1358 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1359 | int max(int a, int b) { // warn |
| 1360 | log(); |
| 1361 | if (a > b) |
| 1362 | return a; |
| 1363 | return b; |
| 1364 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1365 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1366 | int maxClone(int x, int y) { // similar code here |
| 1367 | log(); |
| 1368 | if (x > y) |
| 1369 | return x; |
| 1370 | return y; |
| 1371 | } |
| 1372 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1373 | .. _alpha-core-BoolAssignment: |
| 1374 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1375 | alpha.core.BoolAssignment (ObjC) |
| 1376 | """""""""""""""""""""""""""""""" |
| 1377 | Warn about assigning non-{0,1} values to boolean variables. |
| 1378 | |
| 1379 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1380 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1381 | void test() { |
| 1382 | BOOL b = -1; // warn |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1383 | } |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1384 | |
| 1385 | alpha.core |
| 1386 | ^^^^^^^^^^ |
| 1387 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1388 | .. _alpha-core-CallAndMessageUnInitRefArg: |
| 1389 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1390 | alpha.core.CallAndMessageUnInitRefArg (C,C++, ObjC) |
| 1391 | """"""""""""""""""""""""""""""""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1392 | Check for logical errors for function calls and Objective-C |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1393 | message expressions (e.g., uninitialized arguments, null function pointers, and pointer to undefined variables). |
| 1394 | |
| 1395 | .. code-block:: c |
| 1396 | |
| 1397 | void test(void) { |
| 1398 | int t; |
| 1399 | int &p = t; |
| 1400 | int &s = p; |
| 1401 | int &q = s; |
| 1402 | foo(q); // warn |
| 1403 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1404 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1405 | void test(void) { |
| 1406 | int x; |
| 1407 | foo(&x); // warn |
| 1408 | } |
| 1409 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1410 | .. _alpha-core-CastSize: |
| 1411 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1412 | alpha.core.CastSize (C) |
| 1413 | """"""""""""""""""""""" |
| 1414 | Check when casting a malloc'ed type ``T``, whether the size is a multiple of the size of ``T``. |
| 1415 | |
| 1416 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1417 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1418 | void test() { |
| 1419 | int *x = (int *) malloc(11); // warn |
| 1420 | } |
| 1421 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1422 | .. _alpha-core-CastToStruct: |
| 1423 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1424 | alpha.core.CastToStruct (C, C++) |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1425 | """""""""""""""""""""""""""""""" |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1426 | Check for cast from non-struct pointer to struct pointer. |
| 1427 | |
| 1428 | .. code-block:: cpp |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1429 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1430 | // C |
| 1431 | struct s {}; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1432 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1433 | void test(int *p) { |
| 1434 | struct s *ps = (struct s *) p; // warn |
| 1435 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1436 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1437 | // C++ |
| 1438 | class c {}; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1439 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1440 | void test(int *p) { |
| 1441 | c *pc = (c *) p; // warn |
| 1442 | } |
| 1443 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1444 | .. _alpha-core-Conversion: |
| 1445 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1446 | alpha.core.Conversion (C, C++, ObjC) |
| 1447 | """""""""""""""""""""""""""""""""""" |
| 1448 | Loss of sign/precision in implicit conversions. |
| 1449 | |
| 1450 | .. code-block:: c |
| 1451 | |
| 1452 | void test(unsigned U, signed S) { |
| 1453 | if (S > 10) { |
| 1454 | if (U < S) { |
| 1455 | } |
| 1456 | } |
| 1457 | if (S < -10) { |
| 1458 | if (U < S) { // warn (loss of sign) |
| 1459 | } |
| 1460 | } |
| 1461 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1462 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1463 | void test() { |
| 1464 | long long A = 1LL << 60; |
| 1465 | short X = A; // warn (loss of precision) |
| 1466 | } |
| 1467 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1468 | .. _alpha-core-DynamicTypeChecker: |
| 1469 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1470 | alpha.core.DynamicTypeChecker (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1471 | """""""""""""""""""""""""""""""""""" |
| 1472 | Check for cases where the dynamic and the static type of an object are unrelated. |
| 1473 | |
| 1474 | |
| 1475 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1476 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1477 | id date = [NSDate date]; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1478 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1479 | // Warning: Object has a dynamic type 'NSDate *' which is |
| 1480 | // incompatible with static type 'NSNumber *'" |
| 1481 | NSNumber *number = date; |
| 1482 | [number doubleValue]; |
| 1483 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1484 | .. _alpha-core-FixedAddr: |
| 1485 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1486 | alpha.core.FixedAddr (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1487 | """""""""""""""""""""""" |
| 1488 | Check for assignment of a fixed address to a pointer. |
| 1489 | |
| 1490 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1491 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1492 | void test() { |
| 1493 | int *p; |
| 1494 | p = (int *) 0x10000; // warn |
| 1495 | } |
| 1496 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1497 | .. _alpha-core-IdenticalExpr: |
| 1498 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1499 | alpha.core.IdenticalExpr (C, C++) |
| 1500 | """"""""""""""""""""""""""""""""" |
| 1501 | Warn about unintended use of identical expressions in operators. |
| 1502 | |
| 1503 | .. code-block:: cpp |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1504 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1505 | // C |
| 1506 | void test() { |
| 1507 | int a = 5; |
| 1508 | int b = a | 4 | a; // warn: identical expr on both sides |
| 1509 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1510 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1511 | // C++ |
| 1512 | bool f(void); |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1513 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1514 | void test(bool b) { |
| 1515 | int i = 10; |
| 1516 | if (f()) { // warn: true and false branches are identical |
| 1517 | do { |
| 1518 | i--; |
| 1519 | } while (f()); |
| 1520 | } else { |
| 1521 | do { |
| 1522 | i--; |
| 1523 | } while (f()); |
| 1524 | } |
| 1525 | } |
| 1526 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1527 | .. _alpha-core-PointerArithm: |
| 1528 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1529 | alpha.core.PointerArithm (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1530 | """""""""""""""""""""""""""" |
| 1531 | Check for pointer arithmetic on locations other than array elements. |
| 1532 | |
| 1533 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1534 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1535 | void test() { |
| 1536 | int x; |
| 1537 | int *p; |
| 1538 | p = &x + 1; // warn |
| 1539 | } |
| 1540 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1541 | .. _alpha-core-PointerSub: |
| 1542 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1543 | alpha.core.PointerSub (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1544 | """"""""""""""""""""""""" |
| 1545 | Check for pointer subtractions on two pointers pointing to different memory chunks. |
| 1546 | |
| 1547 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1548 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1549 | void test() { |
| 1550 | int x, y; |
| 1551 | int d = &y - &x; // warn |
| 1552 | } |
| 1553 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1554 | .. _alpha-core-SizeofPtr: |
| 1555 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1556 | alpha.core.SizeofPtr (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1557 | """""""""""""""""""""""" |
| 1558 | Warn about unintended use of ``sizeof()`` on pointer expressions. |
| 1559 | |
| 1560 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1561 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1562 | struct s {}; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1563 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1564 | int test(struct s *p) { |
| 1565 | return sizeof(p); |
| 1566 | // warn: sizeof(ptr) can produce an unexpected result |
| 1567 | } |
| 1568 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1569 | .. _alpha-core-StackAddressAsyncEscape: |
| 1570 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1571 | alpha.core.StackAddressAsyncEscape (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1572 | """""""""""""""""""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1573 | Check that addresses to stack memory do not escape the function that involves dispatch_after or dispatch_async. |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1574 | This checker is a part of ``core.StackAddressEscape``, but is temporarily disabled until some false positives are fixed. |
| 1575 | |
| 1576 | .. code-block:: c |
| 1577 | |
| 1578 | dispatch_block_t test_block_inside_block_async_leak() { |
| 1579 | int x = 123; |
| 1580 | void (^inner)(void) = ^void(void) { |
| 1581 | int y = x; |
| 1582 | ++y; |
| 1583 | }; |
| 1584 | void (^outer)(void) = ^void(void) { |
| 1585 | int z = x; |
| 1586 | ++z; |
| 1587 | inner(); |
| 1588 | }; |
| 1589 | return outer; // warn: address of stack-allocated block is captured by a |
| 1590 | // returned block |
| 1591 | } |
| 1592 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1593 | .. _alpha-core-TestAfterDivZero: |
| 1594 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1595 | alpha.core.TestAfterDivZero (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1596 | """"""""""""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1597 | Check for division by variable that is later compared against 0. |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1598 | Either the comparison is useless or there is division by zero. |
| 1599 | |
| 1600 | .. code-block:: c |
| 1601 | |
| 1602 | void test(int x) { |
| 1603 | var = 77 / x; |
| 1604 | if (x == 0) { } // warn |
| 1605 | } |
| 1606 | |
| 1607 | alpha.cplusplus |
| 1608 | ^^^^^^^^^^^^^^^ |
| 1609 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1610 | .. _alpha-cplusplus-DeleteWithNonVirtualDtor: |
| 1611 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1612 | alpha.cplusplus.DeleteWithNonVirtualDtor (C++) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1613 | """""""""""""""""""""""""""""""""""""""""""""" |
| 1614 | Reports destructions of polymorphic objects with a non-virtual destructor in their base class. |
| 1615 | |
| 1616 | .. code-block:: cpp |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1617 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1618 | NonVirtual *create() { |
| 1619 | NonVirtual *x = new NVDerived(); // note: conversion from derived to base |
| 1620 | // happened here |
| 1621 | return x; |
| 1622 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1623 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1624 | void sink(NonVirtual *x) { |
| 1625 | delete x; // warn: destruction of a polymorphic object with no virtual |
| 1626 | // destructor |
| 1627 | } |
| 1628 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1629 | .. _alpha-cplusplus-EnumCastOutOfRange: |
| 1630 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1631 | alpha.cplusplus.EnumCastOutOfRange (C++) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1632 | """""""""""""""""""""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1633 | Check for integer to enumeration casts that could result in undefined values. |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1634 | |
| 1635 | .. code-block:: cpp |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1636 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1637 | enum TestEnum { |
| 1638 | A = 0 |
| 1639 | }; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1640 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1641 | void foo() { |
| 1642 | TestEnum t = static_cast(-1); |
| 1643 | // warn: the value provided to the cast expression is not in |
| 1644 | the valid range of values for the enum |
| 1645 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1646 | .. _alpha-cplusplus-InvalidatedIterator: |
| 1647 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1648 | alpha.cplusplus.InvalidatedIterator (C++) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1649 | """"""""""""""""""""""""""""""""""""""""" |
| 1650 | Check for use of invalidated iterators. |
| 1651 | |
| 1652 | .. code-block:: cpp |
| 1653 | |
| 1654 | void bad_copy_assign_operator_list1(std::list &L1, |
| 1655 | const std::list &L2) { |
| 1656 | auto i0 = L1.cbegin(); |
| 1657 | L1 = L2; |
| 1658 | *i0; // warn: invalidated iterator accessed |
| 1659 | } |
| 1660 | |
| 1661 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1662 | .. _alpha-cplusplus-IteratorRange: |
| 1663 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1664 | alpha.cplusplus.IteratorRange (C++) |
| 1665 | """"""""""""""""""""""""""""""""""" |
| 1666 | Check for iterators used outside their valid ranges. |
| 1667 | |
| 1668 | .. code-block:: cpp |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1669 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1670 | void simple_bad_end(const std::vector &v) { |
| 1671 | auto i = v.end(); |
| 1672 | *i; // warn: iterator accessed outside of its range |
| 1673 | } |
| 1674 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1675 | .. _alpha-cplusplus-MismatchedIterator: |
| 1676 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1677 | alpha.cplusplus.MismatchedIterator (C++) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1678 | """""""""""""""""""""""""""""""""""""""" |
| 1679 | Check for use of iterators of different containers where iterators of the same container are expected. |
| 1680 | |
| 1681 | .. code-block:: cpp |
| 1682 | |
| 1683 | void bad_insert3(std::vector &v1, std::vector &v2) { |
| 1684 | v2.insert(v1.cbegin(), v2.cbegin(), v2.cend()); // warn: container accessed |
| 1685 | // using foreign |
| 1686 | // iterator argument |
| 1687 | v1.insert(v1.cbegin(), v1.cbegin(), v2.cend()); // warn: iterators of |
| 1688 | // different containers |
| 1689 | // used where the same |
| 1690 | // container is |
| 1691 | // expected |
| 1692 | v1.insert(v1.cbegin(), v2.cbegin(), v1.cend()); // warn: iterators of |
| 1693 | // different containers |
| 1694 | // used where the same |
| 1695 | // container is |
| 1696 | // expected |
| 1697 | } |
| 1698 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1699 | .. _alpha-cplusplus-MisusedMovedObject: |
| 1700 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1701 | alpha.cplusplus.MisusedMovedObject (C++) |
| 1702 | """""""""""""""""""""""""""""""""""""""" |
| 1703 | Method calls on a moved-from object and copying a moved-from object will be reported. |
| 1704 | |
| 1705 | |
| 1706 | .. code-block:: cpp |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1707 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1708 | struct A { |
| 1709 | void foo() {} |
| 1710 | }; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1711 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1712 | void f() { |
| 1713 | A a; |
| 1714 | A b = std::move(a); // note: 'a' became 'moved-from' here |
| 1715 | a.foo(); // warn: method call on a 'moved-from' object 'a' |
| 1716 | } |
| 1717 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1718 | alpha.deadcode |
| 1719 | ^^^^^^^^^^^^^^ |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1720 | .. _alpha-deadcode-UnreachableCode: |
| 1721 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1722 | alpha.deadcode.UnreachableCode (C, C++) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1723 | """"""""""""""""""""""""""""""""""""""" |
| 1724 | Check unreachable code. |
| 1725 | |
| 1726 | .. code-block:: cpp |
| 1727 | |
| 1728 | // C |
| 1729 | int test() { |
| 1730 | int x = 1; |
| 1731 | while(x); |
| 1732 | return x; // warn |
| 1733 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1734 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1735 | // C++ |
| 1736 | void test() { |
| 1737 | int a = 2; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1738 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1739 | while (a > 1) |
| 1740 | a--; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1741 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1742 | if (a > 1) |
| 1743 | a++; // warn |
| 1744 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1745 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1746 | // Objective-C |
| 1747 | void test(id x) { |
| 1748 | return; |
| 1749 | [x retain]; // warn |
| 1750 | } |
| 1751 | |
| 1752 | alpha.llvm |
| 1753 | ^^^^^^^^^^ |
| 1754 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1755 | .. _alpha-llvm-Conventions: |
| 1756 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1757 | alpha.llvm.Conventions |
| 1758 | """""""""""""""""""""" |
| 1759 | |
| 1760 | Check code for LLVM codebase conventions: |
| 1761 | |
| 1762 | * A StringRef should not be bound to a temporary std::string whose lifetime is shorter than the StringRef's. |
| 1763 | * Clang AST nodes should not have fields that can allocate memory. |
| 1764 | |
| 1765 | |
| 1766 | alpha.osx |
| 1767 | ^^^^^^^^^ |
| 1768 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1769 | .. _alpha-osx-cocoa-DirectIvarAssignment: |
| 1770 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1771 | alpha.osx.cocoa.DirectIvarAssignment (ObjC) |
| 1772 | """"""""""""""""""""""""""""""""""""""""""" |
| 1773 | Check for direct assignments to instance variables. |
| 1774 | |
| 1775 | |
| 1776 | .. code-block:: objc |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1777 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1778 | @interface MyClass : NSObject {} |
| 1779 | @property (readonly) id A; |
| 1780 | - (void) foo; |
| 1781 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1782 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1783 | @implementation MyClass |
| 1784 | - (void) foo { |
| 1785 | _A = 0; // warn |
| 1786 | } |
| 1787 | @end |
| 1788 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1789 | .. _alpha-osx-cocoa-DirectIvarAssignmentForAnnotatedFunctions: |
| 1790 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1791 | alpha.osx.cocoa.DirectIvarAssignmentForAnnotatedFunctions (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1792 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1793 | Check for direct assignments to instance variables in |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1794 | the methods annotated with ``objc_no_direct_instance_variable_assignment``. |
| 1795 | |
| 1796 | .. code-block:: objc |
| 1797 | |
| 1798 | @interface MyClass : NSObject {} |
| 1799 | @property (readonly) id A; |
| 1800 | - (void) fAnnotated __attribute__(( |
| 1801 | annotate("objc_no_direct_instance_variable_assignment"))); |
| 1802 | - (void) fNotAnnotated; |
| 1803 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1804 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1805 | @implementation MyClass |
| 1806 | - (void) fAnnotated { |
| 1807 | _A = 0; // warn |
| 1808 | } |
| 1809 | - (void) fNotAnnotated { |
| 1810 | _A = 0; // no warn |
| 1811 | } |
| 1812 | @end |
| 1813 | |
| 1814 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1815 | .. _alpha-osx-cocoa-InstanceVariableInvalidation: |
| 1816 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1817 | alpha.osx.cocoa.InstanceVariableInvalidation (ObjC) |
| 1818 | """"""""""""""""""""""""""""""""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1819 | Check that the invalidatable instance variables are |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1820 | invalidated in the methods annotated with objc_instance_variable_invalidator. |
| 1821 | |
| 1822 | .. code-block:: objc |
| 1823 | |
| 1824 | @protocol Invalidation <NSObject> |
| 1825 | - (void) invalidate |
| 1826 | __attribute__((annotate("objc_instance_variable_invalidator"))); |
| 1827 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1828 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1829 | @interface InvalidationImpObj : NSObject <Invalidation> |
| 1830 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1831 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1832 | @interface SubclassInvalidationImpObj : InvalidationImpObj { |
| 1833 | InvalidationImpObj *var; |
| 1834 | } |
| 1835 | - (void)invalidate; |
| 1836 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1837 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1838 | @implementation SubclassInvalidationImpObj |
| 1839 | - (void) invalidate {} |
| 1840 | @end |
| 1841 | // warn: var needs to be invalidated or set to nil |
| 1842 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1843 | .. _alpha-osx-cocoa-MissingInvalidationMethod: |
| 1844 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1845 | alpha.osx.cocoa.MissingInvalidationMethod (ObjC) |
| 1846 | """""""""""""""""""""""""""""""""""""""""""""""" |
| 1847 | Check that the invalidation methods are present in classes that contain invalidatable instance variables. |
| 1848 | |
| 1849 | .. code-block:: objc |
| 1850 | |
| 1851 | @protocol Invalidation <NSObject> |
| 1852 | - (void)invalidate |
| 1853 | __attribute__((annotate("objc_instance_variable_invalidator"))); |
| 1854 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1855 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1856 | @interface NeedInvalidation : NSObject <Invalidation> |
| 1857 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1858 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1859 | @interface MissingInvalidationMethodDecl : NSObject { |
| 1860 | NeedInvalidation *Var; // warn |
| 1861 | } |
| 1862 | @end |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1863 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1864 | @implementation MissingInvalidationMethodDecl |
| 1865 | @end |
| 1866 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1867 | .. _alpha-osx-cocoa-localizability-PluralMisuseChecker: |
| 1868 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1869 | alpha.osx.cocoa.localizability.PluralMisuseChecker (ObjC) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1870 | """"""""""""""""""""""""""""""""""""""""""""""""""""""""" |
| 1871 | Warns against using one vs. many plural pattern in code when generating localized strings. |
| 1872 | |
| 1873 | .. code-block:: objc |
| 1874 | |
| 1875 | NSString *reminderText = |
| 1876 | NSLocalizedString(@"None", @"Indicates no reminders"); |
| 1877 | if (reminderCount == 1) { |
| 1878 | // Warning: Plural cases are not supported across all languages. |
| 1879 | // Use a .stringsdict file instead |
| 1880 | reminderText = |
| 1881 | NSLocalizedString(@"1 Reminder", @"Indicates single reminder"); |
| 1882 | } else if (reminderCount >= 2) { |
| 1883 | // Warning: Plural cases are not supported across all languages. |
| 1884 | // Use a .stringsdict file instead |
| 1885 | reminderText = |
| 1886 | [NSString stringWithFormat: |
| 1887 | NSLocalizedString(@"%@ Reminders", @"Indicates multiple reminders"), |
| 1888 | reminderCount]; |
| 1889 | } |
| 1890 | |
| 1891 | alpha.security |
| 1892 | ^^^^^^^^^^^^^^ |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1893 | .. _alpha-security-ArrayBound: |
| 1894 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1895 | alpha.security.ArrayBound (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1896 | """"""""""""""""""""""""""""" |
| 1897 | Warn about buffer overflows (older checker). |
| 1898 | |
| 1899 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1900 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1901 | void test() { |
| 1902 | char *s = ""; |
| 1903 | char c = s[1]; // warn |
| 1904 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1905 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1906 | struct seven_words { |
| 1907 | int c[7]; |
| 1908 | }; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1909 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1910 | void test() { |
| 1911 | struct seven_words a, *p; |
| 1912 | p = &a; |
| 1913 | p[0] = a; |
| 1914 | p[1] = a; |
| 1915 | p[2] = a; // warn |
| 1916 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1917 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1918 | // note: requires unix.Malloc or |
| 1919 | // alpha.unix.MallocWithAnnotations checks enabled. |
| 1920 | void test() { |
| 1921 | int *p = malloc(12); |
| 1922 | p[3] = 4; // warn |
| 1923 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1924 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1925 | void test() { |
| 1926 | char a[2]; |
| 1927 | int *b = (int*)a; |
| 1928 | b[1] = 3; // warn |
| 1929 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1930 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1931 | .. _alpha-security-ArrayBoundV2: |
| 1932 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1933 | alpha.security.ArrayBoundV2 (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1934 | """"""""""""""""""""""""""""""" |
| 1935 | Warn about buffer overflows (newer checker). |
| 1936 | |
| 1937 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1938 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1939 | void test() { |
| 1940 | char *s = ""; |
| 1941 | char c = s[1]; // warn |
| 1942 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1943 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1944 | void test() { |
| 1945 | int buf[100]; |
| 1946 | int *p = buf; |
| 1947 | p = p + 99; |
| 1948 | p[1] = 1; // warn |
| 1949 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1950 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1951 | // note: compiler has internal check for this. |
| 1952 | // Use -Wno-array-bounds to suppress compiler warning. |
| 1953 | void test() { |
| 1954 | int buf[100][100]; |
| 1955 | buf[0][-1] = 1; // warn |
| 1956 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1957 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1958 | // note: requires alpha.security.taint check turned on. |
| 1959 | void test() { |
| 1960 | char s[] = "abc"; |
| 1961 | int x = getchar(); |
| 1962 | char c = s[x]; // warn: index is tainted |
| 1963 | } |
| 1964 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1965 | .. _alpha-security-MallocOverflow: |
| 1966 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1967 | alpha.security.MallocOverflow (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1968 | """"""""""""""""""""""""""""""""" |
| 1969 | Check for overflows in the arguments to malloc(). |
| 1970 | |
| 1971 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1972 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1973 | void test(int n) { |
| 1974 | void *p = malloc(n * sizeof(int)); // warn |
| 1975 | } |
| 1976 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1977 | .. _alpha-security-MmapWriteExec: |
| 1978 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1979 | alpha.security.MmapWriteExec (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1980 | """""""""""""""""""""""""""""""" |
| 1981 | Warn on mmap() calls that are both writable and executable. |
| 1982 | |
| 1983 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 1984 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1985 | void test(int n) { |
| 1986 | void *c = mmap(NULL, 32, PROT_READ | PROT_WRITE | PROT_EXEC, |
| 1987 | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 1988 | // warn: Both PROT_WRITE and PROT_EXEC flags are set. This can lead to |
| 1989 | // exploitable memory regions, which could be overwritten with malicious |
| 1990 | // code |
| 1991 | } |
| 1992 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 1993 | .. _alpha-security-ReturnPtrRange: |
| 1994 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 1995 | alpha.security.ReturnPtrRange (C) |
| 1996 | """"""""""""""""""""""""""""""""" |
| 1997 | Check for an out-of-bound pointer being returned to callers. |
| 1998 | |
| 1999 | .. code-block:: c |
| 2000 | |
| 2001 | static int A[10]; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2002 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2003 | int *test() { |
| 2004 | int *p = A + 10; |
| 2005 | return p; // warn |
| 2006 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2007 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2008 | int test(void) { |
| 2009 | int x; |
| 2010 | return x; // warn: undefined or garbage returned |
| 2011 | } |
| 2012 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2013 | .. _alpha-security-taint-TaintPropagation: |
| 2014 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2015 | alpha.security.taint.TaintPropagation (C, C++) |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2016 | """""""""""""""""""""""""""""""""""""""""""""" |
| 2017 | Generate taint information used by other checkers. |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2018 | A data is tainted when it comes from an unreliable source. |
| 2019 | |
| 2020 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2021 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2022 | void test() { |
| 2023 | char x = getchar(); // 'x' marked as tainted |
| 2024 | system(&x); // warn: untrusted data is passed to a system call |
| 2025 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2026 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2027 | // note: compiler internally checks if the second param to |
| 2028 | // sprintf is a string literal or not. |
| 2029 | // Use -Wno-format-security to suppress compiler warning. |
| 2030 | void test() { |
| 2031 | char s[10], buf[10]; |
| 2032 | fscanf(stdin, "%s", s); // 's' marked as tainted |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2033 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2034 | sprintf(buf, s); // warn: untrusted data as a format string |
| 2035 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2036 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2037 | void test() { |
| 2038 | size_t ts; |
| 2039 | scanf("%zd", &ts); // 'ts' marked as tainted |
| 2040 | int *p = (int *)malloc(ts * sizeof(int)); |
| 2041 | // warn: untrusted data as buffer size |
| 2042 | } |
| 2043 | |
| 2044 | alpha.unix |
| 2045 | ^^^^^^^^^^^ |
| 2046 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2047 | .. _alpha-unix-BlockInCriticalSection: |
| 2048 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2049 | alpha.unix.BlockInCriticalSection (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2050 | """"""""""""""""""""""""""""""""""""" |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2051 | Check for calls to blocking functions inside a critical section. |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2052 | Applies to: ``lock, unlock, sleep, getc, fgets, read, recv, pthread_mutex_lock,`` |
| 2053 | `` pthread_mutex_unlock, mtx_lock, mtx_timedlock, mtx_trylock, mtx_unlock, lock_guard, unique_lock`` |
| 2054 | |
| 2055 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2056 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2057 | void test() { |
| 2058 | std::mutex m; |
| 2059 | m.lock(); |
| 2060 | sleep(3); // warn: a blocking function sleep is called inside a critical |
| 2061 | // section |
| 2062 | m.unlock(); |
| 2063 | } |
| 2064 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2065 | .. _alpha-unix-Chroot: |
| 2066 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2067 | alpha.unix.Chroot (C) |
| 2068 | """"""""""""""""""""" |
| 2069 | Check improper use of chroot. |
| 2070 | |
| 2071 | .. code-block:: c |
| 2072 | |
| 2073 | void f(); |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2074 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2075 | void test() { |
| 2076 | chroot("/usr/local"); |
| 2077 | f(); // warn: no call of chdir("/") immediately after chroot |
| 2078 | } |
| 2079 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2080 | .. _alpha-unix-PthreadLock: |
| 2081 | |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2082 | alpha.unix.PthreadLock (C) |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2083 | """""""""""""""""""""""""" |
| 2084 | Simple lock -> unlock checker. |
| 2085 | Applies to: ``pthread_mutex_lock, pthread_rwlock_rdlock, pthread_rwlock_wrlock, lck_mtx_lock, lck_rw_lock_exclusive`` |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2086 | ``lck_rw_lock_shared, pthread_mutex_trylock, pthread_rwlock_tryrdlock, pthread_rwlock_tryrwlock, lck_mtx_try_lock, |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2087 | lck_rw_try_lock_exclusive, lck_rw_try_lock_shared, pthread_mutex_unlock, pthread_rwlock_unlock, lck_mtx_unlock, lck_rw_done``. |
| 2088 | |
| 2089 | |
| 2090 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2091 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2092 | pthread_mutex_t mtx; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2093 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2094 | void test() { |
| 2095 | pthread_mutex_lock(&mtx); |
| 2096 | pthread_mutex_lock(&mtx); |
| 2097 | // warn: this lock has already been acquired |
| 2098 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2099 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2100 | lck_mtx_t lck1, lck2; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2101 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2102 | void test() { |
| 2103 | lck_mtx_lock(&lck1); |
| 2104 | lck_mtx_lock(&lck2); |
| 2105 | lck_mtx_unlock(&lck1); |
| 2106 | // warn: this was not the most recently acquired lock |
| 2107 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2108 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2109 | lck_mtx_t lck1, lck2; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2110 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2111 | void test() { |
| 2112 | if (lck_mtx_try_lock(&lck1) == 0) |
| 2113 | return; |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2114 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2115 | lck_mtx_lock(&lck2); |
| 2116 | lck_mtx_unlock(&lck1); |
| 2117 | // warn: this was not the most recently acquired lock |
| 2118 | } |
| 2119 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2120 | .. _alpha-unix-SimpleStream: |
| 2121 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2122 | alpha.unix.SimpleStream (C) |
| 2123 | """"""""""""""""""""""""""" |
| 2124 | Check for misuses of stream APIs. Check for misuses of stream APIs: ``fopen, fclose`` |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2125 | (demo checker, the subject of the demo (`Slides <http://llvm.org/devmtg/2012-11/Zaks-Rose-Checker24Hours.pdf>`_ , |
| 2126 | `Video <https://youtu.be/kdxlsP5QVPw>`_) by Anna Zaks and Jordan Rose presented at the |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2127 | `2012 LLVM Developers' Meeting <http://llvm.org/devmtg/2012-11/>`_). |
| 2128 | |
| 2129 | .. code-block:: c |
| 2130 | |
| 2131 | void test() { |
| 2132 | FILE *F = fopen("myfile.txt", "w"); |
| 2133 | } // warn: opened file is never closed |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2134 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2135 | void test() { |
| 2136 | FILE *F = fopen("myfile.txt", "w"); |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2137 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2138 | if (F) |
| 2139 | fclose(F); |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2140 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2141 | fclose(F); // warn: closing a previously closed file stream |
| 2142 | } |
| 2143 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2144 | .. _alpha-unix-Stream: |
| 2145 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2146 | alpha.unix.Stream (C) |
| 2147 | """"""""""""""""""""" |
| 2148 | Check stream handling functions: ``fopen, tmpfile, fclose, fread, fwrite, fseek, ftell, rewind, fgetpos,`` |
| 2149 | ``fsetpos, clearerr, feof, ferror, fileno``. |
| 2150 | |
| 2151 | .. code-block:: c |
| 2152 | |
| 2153 | void test() { |
| 2154 | FILE *p = fopen("foo", "r"); |
| 2155 | } // warn: opened file is never closed |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2156 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2157 | void test() { |
| 2158 | FILE *p = fopen("foo", "r"); |
| 2159 | fseek(p, 1, SEEK_SET); // warn: stream pointer might be NULL |
| 2160 | fclose(p); |
| 2161 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2162 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2163 | void test() { |
| 2164 | FILE *p = fopen("foo", "r"); |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2165 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2166 | if (p) |
| 2167 | fseek(p, 1, 3); |
| 2168 | // warn: third arg should be SEEK_SET, SEEK_END, or SEEK_CUR |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2169 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2170 | fclose(p); |
| 2171 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2172 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2173 | void test() { |
| 2174 | FILE *p = fopen("foo", "r"); |
| 2175 | fclose(p); |
| 2176 | fclose(p); // warn: already closed |
| 2177 | } |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2178 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2179 | void test() { |
| 2180 | FILE *p = tmpfile(); |
| 2181 | ftell(p); // warn: stream pointer might be NULL |
| 2182 | fclose(p); |
| 2183 | } |
| 2184 | |
| 2185 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2186 | .. _alpha-unix-cstring-BufferOverlap: |
| 2187 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2188 | alpha.unix.cstring.BufferOverlap (C) |
| 2189 | """""""""""""""""""""""""""""""""""" |
| 2190 | Checks for overlap in two buffer arguments. Applies to: ``memcpy, mempcpy``. |
| 2191 | |
| 2192 | .. code-block:: c |
| 2193 | |
| 2194 | void test() { |
| 2195 | int a[4] = {0}; |
| 2196 | memcpy(a + 2, a + 1, 8); // warn |
| 2197 | } |
| 2198 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2199 | .. _alpha-unix-cstring-NotNullTerminated: |
| 2200 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2201 | alpha.unix.cstring.NotNullTerminated (C) |
| 2202 | """""""""""""""""""""""""""""""""""""""" |
| 2203 | Check for arguments which are not null-terminated strings; applies to: ``strlen, strnlen, strcpy, strncpy, strcat, strncat``. |
| 2204 | |
| 2205 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2206 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2207 | void test() { |
| 2208 | int y = strlen((char *)&test); // warn |
| 2209 | } |
| 2210 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2211 | .. _alpha-unix-cstring-OutOfBounds: |
| 2212 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2213 | alpha.unix.cstring.OutOfBounds (C) |
| 2214 | """""""""""""""""""""""""""""""""" |
| 2215 | Check for out-of-bounds access in string functions; applies to:`` strncopy, strncat``. |
| 2216 | |
| 2217 | |
| 2218 | .. code-block:: c |
Alexander Kornienko | 9a857d2 | 2019-02-11 15:17:13 +0000 | [diff] [blame] | 2219 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2220 | void test() { |
| 2221 | int y = strlen((char *)&test); // warn |
| 2222 | } |
| 2223 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2224 | .. _alpha-nondeterminism-PointerIteration: |
| 2225 | |
Mandeep Singh Grang | 0cdc5dd | 2019-05-24 19:24:08 +0000 | [diff] [blame] | 2226 | alpha.nondeterminism.PointerIteration (C++) |
| 2227 | """"""""""""""""""""""""""""""""""""""""""" |
| 2228 | Check for non-determinism caused by iterating unordered containers of pointers. |
| 2229 | |
| 2230 | .. code-block:: c |
| 2231 | |
| 2232 | void test() { |
| 2233 | int a = 1, b = 2; |
| 2234 | std::unordered_set<int *> UnorderedPtrSet = {&a, &b}; |
| 2235 | |
| 2236 | for (auto i : UnorderedPtrSet) // warn |
| 2237 | f(i); |
| 2238 | } |
| 2239 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2240 | .. _alpha-nondeterminism-PointerSorting: |
| 2241 | |
Mandeep Singh Grang | c0773ab | 2019-03-08 20:13:53 +0000 | [diff] [blame] | 2242 | alpha.nondeterminism.PointerSorting (C++) |
Mandeep Singh Grang | d4c4f74 | 2019-03-08 20:35:25 +0000 | [diff] [blame] | 2243 | """"""""""""""""""""""""""""""""""""""""" |
Mandeep Singh Grang | c0773ab | 2019-03-08 20:13:53 +0000 | [diff] [blame] | 2244 | Check for non-determinism caused by sorting of pointers. |
| 2245 | |
| 2246 | .. code-block:: c |
| 2247 | |
| 2248 | void test() { |
| 2249 | int a = 1, b = 2; |
| 2250 | std::vector<int *> V = {&a, &b}; |
| 2251 | std::sort(V.begin(), V.end()); // warn |
| 2252 | } |
| 2253 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2254 | |
| 2255 | Debug Checkers |
| 2256 | --------------- |
| 2257 | |
| 2258 | .. _debug-checkers: |
| 2259 | |
| 2260 | |
| 2261 | debug |
| 2262 | ^^^^^ |
| 2263 | |
| 2264 | Checkers used for debugging the analyzer. |
Kristof Umann | dccfaff | 2019-02-05 10:19:39 +0000 | [diff] [blame] | 2265 | :doc:`developer-docs/DebugChecks` page contains a detailed description. |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2266 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2267 | .. _debug-AnalysisOrder: |
| 2268 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2269 | debug.AnalysisOrder |
| 2270 | """"""""""""""""""" |
| 2271 | Print callbacks that are called during analysis in order. |
| 2272 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2273 | .. _debug-ConfigDumper: |
| 2274 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2275 | debug.ConfigDumper |
| 2276 | """""""""""""""""" |
| 2277 | Dump config table. |
| 2278 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2279 | .. _debug-DumpCFG Display: |
| 2280 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2281 | debug.DumpCFG Display |
| 2282 | """"""""""""""""""""" |
| 2283 | Control-Flow Graphs. |
| 2284 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2285 | .. _debug-DumpCallGraph: |
| 2286 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2287 | debug.DumpCallGraph |
| 2288 | """"""""""""""""""" |
| 2289 | Display Call Graph. |
| 2290 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2291 | .. _debug-DumpCalls: |
| 2292 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2293 | debug.DumpCalls |
| 2294 | """"""""""""""" |
| 2295 | Print calls as they are traversed by the engine. |
| 2296 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2297 | .. _debug-DumpDominators: |
| 2298 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2299 | debug.DumpDominators |
| 2300 | """""""""""""""""""" |
| 2301 | Print the dominance tree for a given CFG. |
| 2302 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2303 | .. _debug-DumpLiveVars: |
| 2304 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2305 | debug.DumpLiveVars |
| 2306 | """""""""""""""""" |
| 2307 | Print results of live variable analysis. |
| 2308 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2309 | .. _debug-DumpTraversal: |
| 2310 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2311 | debug.DumpTraversal |
| 2312 | """"""""""""""""""" |
| 2313 | Print branch conditions as they are traversed by the engine. |
| 2314 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2315 | .. _debug-ExprInspection: |
| 2316 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2317 | debug.ExprInspection |
| 2318 | """""""""""""""""""" |
| 2319 | Check the analyzer's understanding of expressions. |
| 2320 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2321 | .. _debug-Stats: |
| 2322 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2323 | debug.Stats |
| 2324 | """"""""""" |
| 2325 | Emit warnings with analyzer statistics. |
| 2326 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2327 | .. _debug-TaintTest: |
| 2328 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2329 | debug.TaintTest |
| 2330 | """"""""""""""" |
| 2331 | Mark tainted symbols as such. |
| 2332 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2333 | .. _debug-ViewCFG: |
| 2334 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2335 | debug.ViewCFG |
| 2336 | """"""""""""" |
| 2337 | View Control-Flow Graphs using GraphViz. |
| 2338 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2339 | .. _debug-ViewCallGraph: |
| 2340 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2341 | debug.ViewCallGraph |
| 2342 | """"""""""""""""""" |
| 2343 | View Call Graph using GraphViz. |
| 2344 | |
Nathan Huckleberry | 83c94bf | 2019-07-11 17:12:05 +0000 | [diff] [blame] | 2345 | .. _debug-ViewExplodedGraph: |
| 2346 | |
Kristof Umann | 1a17032 | 2019-02-05 00:39:33 +0000 | [diff] [blame] | 2347 | debug.ViewExplodedGraph |
| 2348 | """"""""""""""""""""""" |
| 2349 | View Exploded Graphs using GraphViz. |
| 2350 | |