]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - www/analyzer/alpha_checks.html
Vendor import of clang trunk r300422:
[FreeBSD/FreeBSD.git] / www / analyzer / alpha_checks.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2           "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5   <title>Alpha Checks</title>
6   <link type="text/css" rel="stylesheet" href="menu.css">
7   <link type="text/css" rel="stylesheet" href="content.css">
8   <script type="text/javascript" src="scripts/menu.js"></script>
9   <script type="text/javascript" src="scripts/expandcollapse.js"></script>
10   <style type="text/css">
11   tr:first-child { width:20%; }
12   </style>
13 </head>
14 <body onload="initExpandCollapse()">
15
16 <div id="page">
17 <!--#include virtual="menu.html.incl"-->
18
19 <div id="content">
20 <h1>Alpha Checkers</h1>
21 Experimental checkers in addition to the <a href = "available_checks.html">
22 Default Checkers</a>. These are checkers with known issues or limitations that
23 keep them from being on by default. They are likely to have false positives.
24 Bug reports are welcome but will likely not be investigated for some time.
25 Patches welcome!
26 <ul>
27 <li><a href="#core_alpha_checkers">Core Alpha Checkers</a></li>
28 <li><a href="#cplusplus_alpha_checkers">C++ Alpha Checkers</a></li>
29 <li><a href="#valist_alpha_checkers">Variable Argument Alpha Checkers</a></li>
30 <li><a href="#deadcode_alpha_checkers">Dead Code Alpha Checkers</a></li>
31 <li><a href="#osx_alpha_checkers">OS X Alpha Checkers</a></li>
32 <li><a href="#security_alpha_checkers">Security Alpha Checkers</a></li>
33 <li><a href="#unix_alpha_checkers">Unix Alpha Checkers</a></li>
34 </ul>
35
36 <!-- ============================= core alpha ============================= -->
37 <h3 id="core_alpha_checkers">Core Alpha Checkers</h3>
38 <table class="checkers">
39 <colgroup><col class="namedescr"><col class="example"></colgroup>
40 <thead><tr><td>Name, Description</td><td>Example</td></tr></thead>
41
42 <tbody>
43 <tr><td><div class="namedescr expandable"><span class="name">
44 alpha.core.BoolAssignment</span><span class="lang">
45 (ObjC)</span><div class="descr">
46 Warn about assigning non-{0,1} values to boolean variables.</div></div></td>
47 <td><div class="exampleContainer expandable">
48 <div class="example"><pre>
49 void test() {
50   BOOL b = -1; // warn
51 }
52 </pre></div></div></td></tr>
53
54
55 <tr><td><div class="namedescr expandable"><span class="name">
56 alpha.core.CastSize</span><span class="lang">
57 (C)</span><div class="descr">
58 Check when casting a malloc'ed type T, whether the size is a multiple of the 
59 size of T (Works only with <span class="name">unix.Malloc</span>
60 or <span class="name">alpha.unix.MallocWithAnnotations</span> 
61 checks enabled).</div></div></td>
62 <td><div class="exampleContainer expandable">
63 <div class="example"><pre>
64 void test() {
65   int *x = (int *)malloc(11); // warn
66 }
67 </pre></div></div></td></tr>
68
69
70 <tr><td><div class="namedescr expandable"><span class="name">
71 alpha.core.CastToStruct</span><span class="lang">
72 (C, C++)</span><div class="descr">
73 Check for cast from non-struct pointer to struct pointer.</div></div></td>
74 <td><div class="exampleContainer expandable">
75 <div class="example"><pre>
76 // C
77 struct s {};
78
79 void test(int *p) {
80   struct s *ps = (struct s *) p; // warn
81 }
82 </pre></div><div class="separator"></div>
83 <div class="example"><pre>
84 // C++
85 class c {};
86
87 void test(int *p) {
88   c *pc = (c *) p; // warn
89 }
90 </pre></div></div></td></tr>
91
92
93 <tr><td><div class="namedescr expandable"><span class="name">
94 alpha.core.FixedAddr</span><span class="lang">
95 (C)</span><div class="descr">
96 Check for assignment of a fixed address to a pointer.</div></div></td>
97 <td><div class="exampleContainer expandable">
98 <div class="example"><pre>
99 void test() {
100   int *p;
101   p = (int *) 0x10000; // warn
102 }
103 </pre></div></div></td></tr>
104
105
106 <tr><td><div class="namedescr expandable"><span class="name">
107 alpha.core.IdenticalExpr</span><span class="lang">
108 (C, C++)</span><div class="descr">
109 Warn about suspicious uses of identical expressions.</div></div></td>
110 <td><div class="exampleContainer expandable">
111 <div class="example"><pre>
112 // C
113 void test() {
114   int a = 5;
115   int b = a | 4 | a; // warn: identical expr on both sides
116 }
117 </pre></div><div class="separator"></div>
118 <div class="example"><pre>
119 // C++
120 bool f(void);
121
122 void test(bool b) {
123   int i = 10;
124   if (f()) { // warn: true and false branches are identical
125     do {
126       i--;
127     } while (f());
128   } else {
129     do {
130       i--;
131     } while (f());
132   }
133 }
134 </pre></div></div></td></tr>
135
136
137 <tr><td><div class="namedescr expandable"><span class="name">
138 alpha.core.PointerArithm</span><span class="lang">
139 (C)</span><div class="descr">
140 Check for pointer arithmetic on locations other than array 
141 elements.</div></div></td>
142 <td><div class="exampleContainer expandable">
143 <div class="example"><pre>
144 void test() {
145   int x;
146   int *p;
147   p = &amp;x + 1; // warn
148 }
149 </pre></div></div></td></tr>
150
151
152 <tr><td><div class="namedescr expandable"><span class="name">
153 alpha.core.PointerSub</span><span class="lang">
154 (C)</span><div class="descr">
155 Check for pointer subtractions on two pointers pointing to different memory 
156 chunks.</div></div></td>
157 <td><div class="exampleContainer expandable">
158 <div class="example"><pre>
159 void test() {
160   int x, y;
161   int d = &amp;y - &amp;x; // warn
162 }
163 </pre></div></div></td></tr>
164
165
166 <tr><td><div class="namedescr expandable"><span class="name">
167 alpha.core.SizeofPtr</span><span class="lang">
168 (C)</span><div class="descr">
169 Warn about unintended use of <code>sizeof()</code> on pointer 
170 expressions.</div></div></td>
171 <td><div class="exampleContainer expandable">
172 <div class="example"><pre>
173 struct s {};
174
175 int test(struct s *p) {
176   return sizeof(p); 
177     // warn: sizeof(ptr) can produce an unexpected result
178 }
179 </pre></div></div></td></tr>
180
181 </tbody></table>
182
183 <!-- =========================== cplusplus alpha =========================== -->
184 <h3 id="cplusplus_alpha_checkers">C++ Alpha Checkers</h3>
185 <table class="checkers">
186 <colgroup><col class="namedescr"><col class="example"></colgroup>
187 <thead><tr><td>Name, Description</td><td>Example</td></tr></thead>
188
189 <tbody>
190 <tr><td><div class="namedescr expandable"><span class="name">
191 alpha.cplusplus.NewDeleteLeaks</span><span class="lang">
192 (C++)</span><div class="descr">
193 Check for memory leaks. Traces memory managed by <code>new</code>/<code>
194 delete</code>.</div></div></td>
195 <td><div class="exampleContainer expandable">
196 <div class="example"><pre>
197 void test() {
198   int *p = new int;
199 } // warn
200 </pre></div></div></td></tr>
201
202
203 <tr><td><div class="namedescr expandable"><span class="name">
204 alpha.cplusplus.VirtualCall</span><span class="lang">
205 (C++)</span><div class="descr">
206 Check virtual member function calls during construction or 
207 destruction.</div></div></td>
208 <td><div class="exampleContainer expandable">
209 <div class="example"><pre>
210 class A {
211 public:
212   A() { 
213     f(); // warn
214   }
215   virtual void f();
216 };
217 </pre></div><div class="separator"></div>
218 <div class="example"><pre>
219 class A {
220 public:
221   ~A() {
222     this-&gt;f(); // warn
223   }
224   virtual void f();
225 };
226 </pre></div></div></td></tr>
227
228 </tbody></table>
229
230
231
232 <!-- =============================== va_list =============================== -->
233 <h3 id="valist_alpha_checkers">Variable Argument Alpha Checkers</h3>
234 <table class="checkers">
235 <colgroup><col class="namedescr"><col class="example"></colgroup>
236 <thead><tr><td>Name, Description</td><td>Example</td></tr></thead>
237
238 <tbody>
239 <tr><td><div class="namedescr expandable"><span class="name">
240 alpha.valist.CopyToSelf</span><span class="lang">
241 (C)</span><div class="descr">
242 Calls to the <code>va_copy</code> macro should not copy onto itself.</div></div></td>
243 <td><div class="exampleContainer expandable">
244 <div class="example"><pre>
245 #include &lt;stdarg.h&gt;
246
247 void test(int x, ...) {
248   va_list args;
249   va_start(args, x);
250   va_copy(args, args); // warn
251   va_end(args);
252 }
253 </pre></div></div></td></tr>
254
255 <tr><td><div class="namedescr expandable"><span class="name">
256 alpha.valist.Uninitialized</span><span class="lang">
257 (C)</span><div class="descr">
258 Calls to the <code>va_arg</code>, <code>va_copy</code>, or
259 <code>va_end</code> macro must happen after calling <code>va_start</code> and
260 before calling <code>va_end</code>.</div></div></td>
261 <td><div class="exampleContainer expandable">
262 <div class="example"><pre>
263 #include &lt;stdarg.h&gt;
264
265 void test(int x, ...) {
266   va_list args;
267   int y = va_arg(args, int); // warn
268 }
269 </pre></div>
270 <div class="example"><pre>
271 #include &lt;stdarg.h&gt;
272
273 void test(int x, ...) {
274   va_list args;
275   va_start(args, x);
276   va_end(args);
277   int z = va_arg(args, int); // warn
278 }
279 </pre></div></div></td></tr>
280
281 <tr><td><div class="namedescr expandable"><span class="name">
282 alpha.valist.Unterminated</span><span class="lang">
283 (C)</span><div class="descr">
284 Every <code>va_start</code> must be matched by a <code>va_end</code>. A va_list
285 can only be ended once.</div></div></td>
286 <td><div class="exampleContainer expandable">
287 <div class="example"><pre>
288 #include &lt;stdarg.h&gt;
289
290 void test(int x, ...) {
291   va_list args;
292   va_start(args, x);
293   int y = x + va_arg(args, int);
294 } // warn: missing va_end
295 </pre></div></div></td></tr>
296
297 </tbody></table>
298
299 <!-- =========================== dead code alpha =========================== -->
300 <h3 id="deadcode_alpha_checkers">Dead Code Alpha Checkers</h3>
301 <table class="checkers">
302 <colgroup><col class="namedescr"><col class="example"></colgroup>
303 <thead><tr><td>Name, Description</td><td>Example</td></tr></thead>
304
305 <tbody>
306 <tr><td><div class="namedescr expandable"><span class="name">
307 alpha.deadcode.UnreachableCode</span><span class="lang">
308 (C, C++, ObjC)</span><div class="descr">
309 Check unreachable code.</div></div></td>
310 <td><div class="exampleContainer expandable">
311 <div class="example"><pre>
312 // C
313 int test() {
314   int x = 1;
315   while(x);
316   return x; // warn
317 }
318 </pre></div><div class="separator"></div>
319 <div class="example"><pre>
320 // C++
321 void test() {
322   int a = 2;
323
324   while (a > 1)
325     a--;
326
327   if (a > 1)
328     a++; // warn
329 }
330 </pre></div><div class="separator"></div>
331 <div class="example"><pre>
332 // Objective-C
333 void test(id x) {
334   return;
335   [x retain]; // warn
336 }
337 </pre></div></div></td></tr>
338 </tbody></table>
339
340 <!-- ============================== OS X alpha ============================== -->
341 <h3 id="osx_alpha_checkers">OS X Alpha Checkers</h3>
342 <table class="checkers">
343 <colgroup><col class="namedescr"><col class="example"></colgroup>
344 <thead><tr><td>Name, Description</td><td>Example</td></tr></thead>
345
346 <tbody>
347 <tr><td><div class="namedescr expandable"><span class="name">
348 alpha.osx.cocoa.Dealloc</span><span class="lang">
349 (ObjC)</span><div class="descr">
350 Warn about Objective-C classes that lack a correct implementation 
351 of <code>-dealloc</code>.
352 </div></div></td>
353 <td><div class="exampleContainer expandable">
354 <div class="example"><pre>
355 @interface MyObject : NSObject {
356   id _myproperty;  
357 }
358 @end
359
360 @implementation MyObject // warn: lacks 'dealloc'
361 @end
362 </pre></div><div class="separator"></div>
363 <div class="example"><pre>
364 @interface MyObject : NSObject {}
365 @property(assign) id myproperty;
366 @end
367
368 @implementation MyObject // warn: does not send 'dealloc' to super
369 - (void)dealloc {
370   self.myproperty = 0;
371 }
372 @end
373 </pre></div><div class="separator"></div>
374 <div class="example"><pre>
375 @interface MyObject : NSObject {
376   id _myproperty;
377 }
378 @property(retain) id myproperty;
379 @end
380
381 @implementation MyObject
382 @synthesize myproperty = _myproperty;
383   // warn: var was retained but wasn't released
384 - (void)dealloc {
385   [super dealloc];
386 }
387 @end
388 </pre></div><div class="separator"></div>
389 <div class="example"><pre>
390 @interface MyObject : NSObject {
391   id _myproperty;
392 }
393 @property(assign) id myproperty;
394 @end
395
396 @implementation MyObject
397 @synthesize myproperty = _myproperty;
398   // warn: var wasn't retained but was released
399 - (void)dealloc {
400   [_myproperty release];
401   [super dealloc];
402 }
403 @end
404 </pre></div></div></td></tr>
405
406
407 <tr><td><div class="namedescr expandable"><span class="name">
408 alpha.osx.cocoa.DirectIvarAssignment</span><span class="lang">
409 (ObjC)</span><div class="descr">
410 Check that Objective C properties follow the following rule: the property 
411 should be set with the setter, not though a direct assignment.</div></div></td>
412 <td><div class="exampleContainer expandable">
413 <div class="example"><pre>
414 @interface MyClass : NSObject {}
415 @property (readonly) id A;
416 - (void) foo;
417 @end
418
419 @implementation MyClass
420 - (void) foo {
421   _A = 0; // warn
422 }
423 @end
424 </pre></div></div></td></tr>
425
426
427 <tr><td><div class="namedescr expandable"><span class="name">
428 alpha.osx.cocoa.DirectIvarAssignmentForAnnotatedFunctions</span><span class="lang">
429 (ObjC)</span><div class="descr">
430 Check for direct assignments to instance variables in the methods annotated 
431 with <code>objc_no_direct_instance_variable_assignment</code>.</div></div></td>
432 <td><div class="exampleContainer expandable">
433 <div class="example"><pre>
434 @interface MyClass : NSObject {}
435 @property (readonly) id A;
436 - (void) fAnnotated __attribute__((
437     annotate("objc_no_direct_instance_variable_assignment")));
438 - (void) fNotAnnotated;
439 @end
440
441 @implementation MyClass
442 - (void) fAnnotated {
443   _A = 0; // warn
444 }
445 - (void) fNotAnnotated {
446   _A = 0; // no warn
447 }
448 @end
449 </pre></div></div></td></tr>
450
451
452 <tr><td><div class="namedescr expandable"><span class="name">
453 alpha.osx.cocoa.InstanceVariableInvalidation</span><span class="lang">
454 (ObjC)</span><div class="descr">
455 Check that the invalidatable instance variables are invalidated in the methods
456 annotated with <code>objc_instance_variable_invalidator</code>.</div></div></td>
457 <td><div class="exampleContainer expandable">
458 <div class="example"><pre>
459 @protocol Invalidation &lt;NSObject&gt;
460 - (void) invalidate 
461   __attribute__((annotate("objc_instance_variable_invalidator")));
462 @end 
463
464 @interface InvalidationImpObj : NSObject &lt;Invalidation&gt;
465 @end
466
467 @interface SubclassInvalidationImpObj : InvalidationImpObj {
468   InvalidationImpObj *var;
469 }
470 - (void)invalidate;
471 @end
472
473 @implementation SubclassInvalidationImpObj
474 - (void) invalidate {}
475 @end
476 // warn: var needs to be invalidated or set to nil
477 </pre></div></div></td></tr>
478
479
480 <tr><td><div class="namedescr expandable"><span class="name">
481 alpha.osx.cocoa.MissingInvalidationMethod</span><span class="lang">
482 (ObjC)</span><div class="descr">
483 Check that the invalidation methods are present in classes that contain 
484 invalidatable instance variables.</div></div></td>
485 <td><div class="exampleContainer expandable">
486 <div class="example"><pre>
487 @protocol Invalidation &lt;NSObject&gt;
488 - (void)invalidate 
489   __attribute__((annotate("objc_instance_variable_invalidator")));
490 @end
491
492 @interface NeedInvalidation : NSObject &lt;Invalidation&gt;
493 @end
494
495 @interface MissingInvalidationMethodDecl : NSObject {
496   NeedInvalidation *Var; // warn
497 }
498 @end
499
500 @implementation MissingInvalidationMethodDecl
501 @end
502 </pre></div></div></td></tr>
503
504 </tbody></table>
505
506 <!-- =========================== security alpha =========================== -->
507 <h3 id="security_alpha_checkers">Security Alpha Checkers</h3>
508 <table class="checkers">
509 <colgroup><col class="namedescr"><col class="example"></colgroup>
510 <thead><tr><td>Name, Description</td><td>Example</td></tr></thead>
511
512 <tbody>
513 <tr><td><div class="namedescr expandable"><span class="name">
514 alpha.security.ArrayBound</span><span class="lang">
515 (C)</span><div class="descr">
516 Warn about buffer overflows (older checker).</div></div></td>
517 <td><div class="exampleContainer expandable">
518 <div class="example"><pre>
519 void test() {
520   char *s = "";
521   char c = s[1]; // warn
522 }
523 </pre></div><div class="separator"></div>
524 <div class="example"><pre>
525 struct seven_words {
526   int c[7];
527 };
528
529 void test() {
530   struct seven_words a, *p;
531   p = &a;
532   p[0] = a;
533   p[1] = a;
534   p[2] = a; // warn
535 }
536 </pre></div><div class="separator"></div>
537 <div class="example"><pre>
538 // note: requires unix.Malloc or 
539 // alpha.unix.MallocWithAnnotations checks enabled.
540 void test() {
541   int *p = malloc(12);
542   p[3] = 4; // warn
543 }
544 </pre></div><div class="separator"></div>
545 <div class="example"><pre>
546 void test() {
547   char a[2];
548   int *b = (int*)a;
549   b[1] = 3; // warn
550 }
551 </pre></div></div></td></tr>
552
553
554 <tr><td><div class="namedescr expandable"><span class="name">
555 alpha.security.ArrayBoundV2</span><span class="lang">
556 (C)</span><div class="descr">
557 Warn about buffer overflows (newer checker).</div></div></td>
558 <td><div class="exampleContainer expandable">
559 <div class="example"><pre>
560 void test() {
561   char *s = "";
562   char c = s[1]; // warn
563 }
564 </pre></div><div class="separator"></div>
565 <div class="example"><pre>
566 void test() {
567   int buf[100];
568   int *p = buf;
569   p = p + 99;
570   p[1] = 1; // warn
571 }
572 </pre></div><div class="separator"></div>
573 <div class="example"><pre>
574 // note: compiler has internal check for this.
575 // Use -Wno-array-bounds to suppress compiler warning.
576 void test() {
577   int buf[100][100];
578   buf[0][-1] = 1; // warn
579 }
580 </pre></div><div class="separator"></div>
581 <div class="example"><pre>
582 // note: requires alpha.security.taint check turned on.
583 void test() {
584   char s[] = "abc";
585   int x = getchar();
586   char c = s[x]; // warn: index is tainted
587 }
588 </pre></div></div></td></tr>
589
590
591 <tr><td><div class="namedescr expandable"><span class="name">
592 alpha.security.MallocOverflow</span><span class="lang">
593 (C)</span><div class="descr">
594 Check for overflows in the arguments to <code>malloc()</code>.</div></div></td>
595 <td><div class="exampleContainer expandable">
596 <div class="example"><pre>
597 void test(int n) {
598   void *p = malloc(n * sizeof(int)); // warn
599 }
600 </pre></div></div></td></tr>
601
602
603 <tr><td><div class="namedescr expandable"><span class="name">
604 alpha.security.ReturnPtrRange</span><span class="lang">
605 (C)</span><div class="descr">
606 Check for an out-of-bound pointer being returned to callers.</div></div></td>
607 <td><div class="exampleContainer expandable">
608 <div class="example"><pre>
609 static int A[10];
610
611 int *test() {
612   int *p = A + 10;
613   return p; // warn
614 }
615 </pre></div><div class="separator"></div>
616 <div class="example"><pre>
617 int test(void) {
618   int x;
619   return x; // warn: undefined or garbage returned
620 }
621 </pre></div></div></td></tr>
622
623
624 <tr><td><div class="namedescr expandable"><span class="name">
625 alpha.security.taint.TaintPropagation</span><span class="lang">
626 (C)</span><div class="descr">
627 Generate taint information used by other checkers.</div></div></td>
628 <td><div class="exampleContainer expandable">
629 <div class="example"><pre>
630 void test() {
631   char x = getchar(); // 'x' marked as tainted
632   system(&x); // warn: untrusted data is passed to a system call
633 }
634 </pre></div><div class="separator"></div>
635 <div class="example"><pre>
636 // note: compiler internally checks if the second param to
637 // sprintf is a string literal or not. 
638 // Use -Wno-format-security to suppress compiler warning.
639 void test() {
640   char s[10], buf[10];
641   fscanf(stdin, "%s", s); // 's' marked as tainted
642
643   sprintf(buf, s); // warn: untrusted data as a format string
644 }
645 </pre></div><div class="separator"></div>
646 <div class="example"><pre>
647 void test() {
648   size_t ts;
649   scanf("%zd", &ts); // 'ts' marked as tainted
650   int *p = (int *)malloc(ts * sizeof(int)); 
651     // warn: untrusted data as bufer size
652 }
653 </pre></div></div></td></tr>
654
655 </tbody></table>
656
657 <!-- ============================= unix alpha ============================= -->
658 <h3 id="unix_alpha_checkers">Unix Alpha Checkers</h3>
659 <table class="checkers">
660 <colgroup><col class="namedescr"><col class="example"></colgroup>
661 <thead><tr><td>Name, Description</td><td>Example</td></tr></thead>
662
663 <tbody>
664 <tr><td><div class="namedescr expandable"><span class="name">
665 alpha.unix.Chroot</span><span class="lang">
666 (C)</span><div class="descr">
667 Check improper use of <code>chroot</code>.</div></div></td>
668 <td><div class="exampleContainer expandable">
669 <div class="example"><pre>
670 void f();
671
672 void test() {
673   chroot("/usr/local");
674   f(); // warn: no call of chdir("/") immediately after chroot
675 }
676 </pre></div></div></td></tr>
677
678
679 <tr><td><div class="namedescr expandable"><span class="name">
680 alpha.unix.MallocWithAnnotations</span><span class="lang">
681 (C)</span><div class="descr">
682 Check for memory leaks, double free, and use-after-free problems. Assumes that
683 all user-defined functions which might free a pointer are
684 annotated.</div></div></td>
685 <td><div class="exampleContainer expandable">
686 <div class="example"><pre>
687 void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
688
689 void test() {
690   int *p = my_malloc(1);
691 } // warn: potential leak
692 </pre></div><div class="separator"></div>
693 <div class="example"><pre>
694 void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
695 void __attribute((ownership_takes(malloc, 1))) my_free(void *);
696
697 void test() {
698   int *p = my_malloc(1);
699   my_free(p);
700   my_free(p); // warn: attempt to free released
701 }
702 </pre></div><div class="separator"></div>
703 <div class="example"><pre>
704 void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
705 void __attribute((ownership_holds(malloc, 1))) my_hold(void *);
706
707 void test() {
708   int *p = my_malloc(1);
709   my_hold(p);
710   free(p); // warn: attempt to free non-owned memory
711 }
712 </pre></div><div class="separator"></div>
713 <div class="example"><pre>
714 void __attribute((ownership_takes(malloc, 1))) my_free(void *);
715
716 void test() {
717   int *p = malloc(1);
718   my_free(p);
719   *p = 1; // warn: use after free
720 }
721 </pre></div></div></td></tr>
722
723
724 <tr><td><div class="namedescr expandable"><span class="name">
725 alpha.unix.PthreadLock</span><span class="lang">
726 (C)</span><div class="descr">
727 Simple lock -> unlock checker; applies to:<div class=functions>
728 pthread_mutex_lock<br>
729 pthread_rwlock_rdlock<br>
730 pthread_rwlock_wrlock<br>
731 lck_mtx_lock<br>
732 lck_rw_lock_exclusive<br>
733 lck_rw_lock_shared<br>
734 pthread_mutex_trylock<br>
735 pthread_rwlock_tryrdlock<br>
736 pthread_rwlock_tryrwlock<br>
737 lck_mtx_try_lock<br>
738 lck_rw_try_lock_exclusive<br>
739 lck_rw_try_lock_shared<br>
740 pthread_mutex_unlock<br>
741 pthread_rwlock_unlock<br>
742 lck_mtx_unlock<br>
743 lck_rw_done</div></div></div></td>
744 <td><div class="exampleContainer expandable">
745 <div class="example"><pre>
746 pthread_mutex_t mtx;
747
748 void test() {
749   pthread_mutex_lock(&mtx);
750   pthread_mutex_lock(&mtx); 
751     // warn: this lock has already been acquired
752 }
753 </pre></div><div class="separator"></div>
754 <div class="example"><pre>
755 lck_mtx_t lck1, lck2;
756
757 void test() {
758   lck_mtx_lock(&lck1);
759   lck_mtx_lock(&lck2);
760   lck_mtx_unlock(&lck1); 
761     // warn: this was not the most recently acquired lock
762 }
763 </pre></div><div class="separator"></div>
764 <div class="example"><pre>
765 lck_mtx_t lck1, lck2;
766
767 void test() {
768   if (lck_mtx_try_lock(&lck1) == 0)
769     return;
770
771   lck_mtx_lock(&lck2);
772   lck_mtx_unlock(&lck1);
773     // warn: this was not the most recently acquired lock
774 }
775 </pre></div></div></td></tr>
776
777
778 <tr><td><div class="namedescr expandable"><span class="name">
779 alpha.unix.SimpleStream</span><span class="lang">
780 (C)</span><div class="descr">
781 Check for misuses of stream APIs:<div class=functions>
782 fopen<br>
783 fclose</div>(demo checker, the subject of the demo
784 (<a href="http://llvm.org/devmtg/2012-11/Zaks-Rose-Checker24Hours.pdf">Slides</a>
785 ,<a href="http://llvm.org/devmtg/2012-11/videos/Zaks-Rose-Checker24Hours.mp4">Video</a>) 
786 by Anna Zaks and Jordan Rose presented at the <a href="http://llvm.org/devmtg/2012-11/">
787 2012 LLVM Developers' Meeting).</a></div></div></td>
788 <td><div class="exampleContainer expandable">
789 <div class="example"><pre>
790 void test() {
791   FILE *F = fopen("myfile.txt", "w");
792 } // warn: opened file is never closed
793 </pre></div><div class="separator"></div>
794 <div class="example"><pre>
795 void test() {
796   FILE *F = fopen("myfile.txt", "w");
797
798   if (F)
799     fclose(F);
800
801   fclose(F); // warn: closing a previously closed file stream
802 }
803 </pre></div></div></td></tr>
804
805
806 <tr><td><div class="namedescr expandable"><span class="name">
807 alpha.unix.Stream</span><span class="lang">
808 (C)</span><div class="descr">
809 Check stream handling functions:<div class=functions>fopen<br>
810 tmpfile<br>
811 fclose<br>
812 fread<br>
813 fwrite<br>
814 fseek<br>
815 ftell<br>
816 rewind<br>
817 fgetpos<br>
818 fsetpos<br>
819 clearerr<br>
820 feof<br>
821 ferror<br>
822 fileno</div></div></div></td>
823 <td><div class="exampleContainer expandable">
824 <div class="example"><pre>
825 void test() {
826   FILE *p = fopen("foo", "r");
827 } // warn: opened file is never closed
828 </pre></div><div class="separator"></div>
829 <div class="example"><pre>
830 void test() {
831   FILE *p = fopen("foo", "r");
832   fseek(p, 1, SEEK_SET); // warn: stream pointer might be NULL
833   fclose(p); 
834 }
835 </pre></div><div class="separator"></div>
836 <div class="example"><pre>
837 void test() {
838   FILE *p = fopen("foo", "r");
839
840   if (p)
841     fseek(p, 1, 3);
842      // warn: third arg should be SEEK_SET, SEEK_END, or SEEK_CUR
843
844   fclose(p); 
845 }
846 </pre></div><div class="separator"></div>
847 <div class="example"><pre>
848 void test() {
849   FILE *p = fopen("foo", "r");
850   fclose(p); 
851   fclose(p); // warn: already closed
852 }
853 </pre></div><div class="separator"></div>
854 <div class="example"><pre>
855 void test() {
856   FILE *p = tmpfile();
857   ftell(p); // warn: stream pointer might be NULL
858   fclose(p);
859 }
860 </pre></div></div></td></tr>
861
862
863 <tr><td><div class="namedescr expandable"><span class="name">
864 alpha.unix.cstring.BufferOverlap</span><span class="lang">
865 (C)</span><div class="descr">
866 Checks for overlap in two buffer arguments; applies to:<div class=functions>
867 memcpy<br>
868 mempcpy</div></div></div></td>
869 <td><div class="exampleContainer expandable">
870 <div class="example"><pre>
871 void test() {
872   int a[4] = {0};
873   memcpy(a + 2, a + 1, 8); // warn
874 }
875 </pre></div></div></td></tr>
876
877
878 <tr><td><div class="namedescr expandable"><span class="name">
879 alpha.unix.cstring.NotNullTerminated</span><span class="lang">
880 (C)</span><div class="descr">
881 Check for arguments which are not null-terminated strings; applies
882 to:<div class=functions>
883 strlen<br>
884 strnlen<br>
885 strcpy<br>
886 strncpy<br>
887 strcat<br>
888 strncat</div></div></div></td>
889 <td><div class="exampleContainer expandable">
890 <div class="example"><pre>
891 void test() {
892   int y = strlen((char *)&test); // warn
893 }
894 </pre></div></div></td></tr>
895
896
897 <tr><td><div class="namedescr expandable"><span class="name">
898 alpha.unix.cstring.OutOfBounds</span><span class="lang">
899 (C)</span><div class="descr">
900 Check for out-of-bounds access in string functions; applies
901 to:<div class=functions>
902 strncopy<br>
903 strncat</div></div></div></td>
904 <td><div class="exampleContainer expandable">
905 <div class="example"><pre>
906 void test(char *y) {
907   char x[4];
908   if (strlen(y) == 4)
909     strncpy(x, y, 5); // warn
910 }
911 </pre></div></div></td></tr>
912
913
914 <tr><td><div class="namedescr expandable"><span class="name">
915 alpha.unix.cstring.BlockInCriticalSection</span><span class="lang">
916 (C)</span><div class="descr">
917 Check for calls to blocking functions inside a critical section; applies
918 to:<div class=functions>
919 lock, unlock<br>
920 sleep<br>
921 getc<br>
922 fgets<br>
923 read<br>
924 recv<br>
925 pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock<br>
926 mtx_lock, mtx_timedlock, mtx_trylock, mtx_unlock<br>
927 </div></div></div></td>
928 <td><div class="exampleContainer expandable">
929 <div class="example"><pre>
930 void testBlockInCriticalSection() {
931   std::mutex m;
932   m.lock();
933   sleep(3); // warn
934   m.unlock();
935 }
936 </pre></div></div></td></tr>
937 </tbody></table>
938
939 </div> <!-- page -->
940 </div> <!-- content -->
941 </body>
942 </html>