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