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