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