1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <title>List of potential checkers
</title>
6 <link type=
"text/css" rel=
"stylesheet" href=
"content.css">
7 <link type=
"text/css" rel=
"stylesheet" href=
"menu.css">
8 <script type=
"text/javascript" src=
"scripts/expandcollapse.js"></script>
9 <script type=
"text/javascript" src=
"scripts/menu.js"></script>
11 <body onload=
"initExpandCollapse()">
16 <!--#include virtual="menu.html.incl"-->
19 <h1>List of potential checkers
</h1>
21 <p>This page contains a list of potential checkers to implement in the static analyzer. If you are interested in contributing to the analyzer's development, this is a good resource to help you get started. The specific names of the checkers are subject to review, and are provided here as suggestions.
</p>
23 <!-- ========================= allocation/deallocation ======================= -->
25 <table class=
"checkers">
26 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
27 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
29 <tr><td><div class=
"namedescr expandable"><span class=
"name">
30 memory.LeakEvalOrder
</span><span class=
"lang">
31 (C, C++)
</span><div class=
"descr">
32 Potential memory leaks caused by an undefined argument evaluation order.
33 <p>Source:
<a href=
"https://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm#BestPractices">
34 boost docs: shared_ptr
</a>.
</p></div></div></td>
35 <td><div class=
"exampleContainer expandable">
36 <div class=
"example"><pre>
39 int h() __attribute__((noreturn));
42 // It is possible that 'malloc(
1)' is called first,
43 // then 'h()', that is (or calls) noreturn and eventually
44 // 'g()' is never called.
45 f(g(malloc(
1)), h()); // warn: 'g()' may never be called.
48 <div class=
"example"><pre>
54 // It is possible that 'new int' is called first,
55 // then 'h()', that throws an exception and eventually
56 // 'g()' is never called.
57 f(g(new int), h()); // warn: 'g()' may never be called.
59 </pre></div></div></td>
60 <td class=
"aligned"></td></tr>
63 <tr><td><div class=
"namedescr expandable"><span class=
"name">
64 memory.DstBufferTooSmall
</span><span class=
"lang">
65 (C, C++)
</span><div class=
"descr">
66 Destination buffer passed to memory function is too small.
67 <br>Note:
<span class=
"name">security.insecureAPI.strcpy
</span> currently warns
68 on usage of
<code>strcpy
</code> and suggests to replace it.
69 <br>Note:
<span class=
"name">alpha.unix.CStringChecker
</span> contains some similar checks.
70 <p>Source:
<a href=
"https://cwe.mitre.org/data/definitions/120.html">CWE-
120</a>.
</p></div></div></td>
71 <td><div class=
"exampleContainer expandable">
72 <div class=
"example"><pre>
74 const char* s1 =
"abc";
76 strcpy(s2, s1); // warn
79 <div class=
"example"><pre>
83 memcpy(p2, p1,
3); // warn
85 </pre></div></div></td>
86 <td class=
"aligned"></td></tr>
89 <tr><td><div class=
"namedescr expandable"><span class=
"name">
90 memory.NegativeArraySize
</span><span class=
"lang">
91 (C, C++)
</span><div class=
"descr">
92 'n' is used to specify the buffer size may be negative.
93 <p>Source:
<a href=
"https://cwe.mitre.org/data/definitions/20.html">CWE-
20,
94 Example
2</a>.
</p></div></div></td>
95 <td><div class=
"exampleContainer expandable">
96 <div class=
"example"><pre>
100 p = new int[n1]; // warn
102 </pre></div></div></td>
103 <td class=
"aligned"></td></tr>
105 <tr><td><div class=
"namedescr expandable"><span class=
"name">
106 memory.ZeroAlloc
</span><span class=
"lang">
107 (C, C++)
</span><div class=
"descr">
108 Allocation of zero bytes.
109 <br>Note: an enhancement to
<span class=
"name">unix.Malloc
</span>.
110 <br>Note:
<span class=
"name">unix.API
</span> perform C-checks for zero
111 allocation. This should be moved to
<span class=
"name">unix.Malloc
</span>.
112 <p>Source: C++
03 3.7.3.1p2; C++
11 3.7.4.1p2.
</p></div></div></td>
113 <td><div class=
"exampleContainer expandable">
114 <div class=
"example"><pre>
115 #include
<stdlib.h
>
118 int *p = malloc(
0); // warn
122 <div class=
"example"><pre>
124 int *p = new int[
0]; // warn
127 </pre></div></div></td>
128 <td class=
"aligned"><a href=
"https://reviews.llvm.org/D6178">
133 <!-- ======================= constructors/destructors ====================== -->
134 <h3>constructors/destructors
</h3>
135 <table class=
"checkers">
136 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
137 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
139 <tr><td><div class=
"namedescr expandable"><span class=
"name">
140 ctordtor.ExptInsideDtor
</span><span class=
"lang">
141 (C++)
</span><div class=
"descr">
142 It is dangerous to let an exception leave a destructor.
143 Using
<code>try..catch
</code> solves the problem.
144 <p>Source: Scott Meyers
"More Effective C++", item
11: Prevent exceptions from
145 leaving destructors.
</p></div></div></td>
146 <td><div class=
"exampleContainer expandable">
147 <div class=
"example"><pre>
150 ~A() { throw
1; } // warn
153 <div class=
"example"><pre>
158 ~A() { f(); } // warn
160 </pre></div></div></td>
161 <td class=
"aligned"></td></tr>
164 <tr><td><div class=
"namedescr expandable"><span class=
"name">
165 ctordtor.PlacementSelfCopy
</span><span class=
"lang">
166 (C++
11)
</span><div class=
"descr">
167 For a placement copy or move, it is almost certainly an error if the
168 constructed object is also the object being copied from.
</div></div></td>
169 <td><div class=
"exampleContainer expandable">
170 <div class=
"example"><pre>
173 void test(A *dst, A *src) {
174 ::new (dst) A(*dst); // warn (should be 'src')
176 </pre></div></div></td>
177 <td class=
"aligned"><!--rdar://problem/13688366--></td></tr>
181 <!-- ============================== exceptions ============================= -->
183 <table class=
"checkers">
184 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
185 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
187 <tr><td><div class=
"namedescr expandable"><span class=
"name">
188 exceptions.ThrowSpecButNotThrow
</span><span class=
"lang">
189 (C++)
</span><div class=
"descr">
190 Function declaration has a
<code>throw(
<i>type
</i>)
</code> specifier but the
191 function do not throw exceptions.
</div></div></td>
192 <td><div class=
"exampleContainer expandable">
193 <div class=
"example"><pre>
194 void test() throw(int) {
196 </pre></div></div></td>
197 <td class=
"aligned"></td></tr>
200 <tr><td><div class=
"namedescr expandable"><span class=
"name">
201 exceptions.NoThrowSpecButThrows
</span><span class=
"lang">
202 (C++)
</span><div class=
"descr">
203 An exception is throw from a function having a
<code>throw()
</code>
204 specifier.
</div></div></td>
205 <td><div class=
"exampleContainer expandable">
206 <div class=
"example"><pre>
207 void test() throw() {
210 </pre></div></div></td>
211 <td class=
"aligned"></td></tr>
214 <tr><td><div class=
"namedescr expandable"><span class=
"name">
215 exceptions.ThrownTypeDiffersSpec
</span><span class=
"lang">
216 (C++)
</span><div class=
"descr">
217 The type of a thrown exception differs from those specified in
218 a
<code>throw(
<i>type
</i>)
</code> specifier.
</div></div></td>
219 <td><div class=
"exampleContainer expandable">
220 <div class=
"example"><pre>
223 void test() throw(int) {
227 </pre></div></div></td>
228 <td class=
"aligned"></td></tr>
232 <!-- ========================= smart pointers ============================== -->
233 <h3>smart pointers
</h3>
234 <table class=
"checkers">
235 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
236 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
238 <tr><td><div class=
"namedescr expandable"><span class=
"name">
239 smartptr.SmartPtrInit
</span><span class=
"lang">
240 (C++)
</span><div class=
"descr">
241 C++
03:
<code>auto_ptr
</code> should store a pointer to an object obtained via
242 new as allocated memory will be cleaned using
<code>delete
</code>.
<br>
243 C++
11: one should use
<code>unique_ptr
<<i>type
</i>[]
></code> to keep a
244 pointer to memory allocated by
<code>new[]
</code>.
<br>
245 C++
11: to keep a pointer to memory allocated by
<code>new[]
</code> in
246 a
<code>shared_ptr
</code> one should use a custom deleter that calls
<code>
248 <p>Source: C++
03 20.4.5p1; C++
11 <code>auto_ptr
</code> is deprecated (D
.10).
</p></div></div></td>
249 <td><div class=
"exampleContainer expandable">
250 <div class=
"example"><pre>
251 #include
<stdlib.h
>
252 #include
<memory
>
255 std::auto_ptr
<int
> p1(new int); // Ok
256 std::auto_ptr
<int
> p2(new int[
3]); // warn
259 <div class=
"example"><pre>
260 #include
<stdlib.h
>
261 #include
<memory
>
264 std::auto_ptr
<int
> p((int *)malloc(sizeof(int))); // warn
266 </pre></div></div></td>
267 <td class=
"aligned"></td></tr>
271 <!-- ============================== dead code ============================== -->
273 <table class=
"checkers">
274 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
275 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
277 <tr><td><div class=
"namedescr expandable"><span class=
"name">
278 deadcode.UnmodifiedVariable
</span><span class=
"lang">
279 (C, C++)
</span><div class=
"descr">
280 A variable is never modified but was not declared const and is not a
281 reference.
<br><br><i>(opt-in checker)
</i></div></div></td>
282 <td><div class=
"exampleContainer expandable">
283 <div class=
"example"><pre>
284 extern int computeDelta();
286 int test(bool cond) {
289 const int delta = computeDelta();
290 // warn: forgot to modify 'i'
294 </pre></div></div></td>
295 <td class=
"aligned"><a href=
"https://bugs.llvm.org/show_bug.cgi?id=16890">PR16890
</a></td></tr>
297 <tr><td><div class=
"namedescr expandable"><span class=
"name">
298 deadcode.IdempotentOperations
</span><span class=
"lang">
299 (C)
</span><div class=
"descr">
300 Warn about idempotent operations.
</div></div></td>
301 <td><div class=
"exampleContainer expandable">
302 <div class=
"example"><pre>
305 x = x; // warn: value is always the same
308 <div class=
"example"><pre>
311 x /= x; // warn: value is always
1
314 <div class=
"example"><pre>
317 x *= one; // warn: right op is always
1
320 <div class=
"example"><pre>
324 // warn: the right operand to '-' is always
0
326 </pre></div></div></td>
327 <td class=
"aligned">removed from alpha.deadcode.* at
328 <a href=
"https://reviews.llvm.org/rL198476">r198476
</a></td></tr>
332 <!-- ================================ POSIX ================================ -->
334 <table class=
"checkers">
335 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
336 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
338 <tr><td><div class=
"namedescr expandable"><span class=
"name">
339 posix.Errno
</span><span class=
"lang">
340 (C)
</span><div class=
"descr">
341 Record that
<code>errno
</code> is non-zero when certain functions
342 fail.
</div></div></td>
343 <td><div class=
"exampleContainer expandable">
344 <div class=
"example"><pre>
345 #include
<stdlib.h
>
347 int readWrapper(int fd, int *count) {
348 int lcount = read(fd, globalBuf, sizeof(globalBuf));
357 if (!readWrapper(fd,
&count))
358 print(
"%d", count); // should not warn
360 </pre></div></div></td>
361 <td class=
"aligned"><a href=
"https://bugs.llvm.org/show_bug.cgi?id=18701">PR18701
</a></td></tr>
365 <!-- ========================= undefined behavior ========================== -->
366 <h3>undefined behavior
</h3>
367 <table class=
"checkers">
368 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
369 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
371 <tr><td><div class=
"namedescr expandable"><span class=
"name">
372 undefbehavior.ExitInDtor
</span><span class=
"lang">
373 (C++)
</span><div class=
"descr">
374 Undefined behavior:
<code>std::exit()
</code> is called to end the program during
375 the destruction of an object with static storage duration.
376 <p>Source: C++
11 3.6.1p4.
</p></div></div></td>
377 <td><div class=
"exampleContainer expandable">
378 <div class=
"example"><pre>
379 #include
<cstdlib
>
384 std::exit(
1); // warn
387 </pre></div></div></td>
388 <td class=
"aligned"></td></tr>
391 <tr><td><div class=
"namedescr expandable"><span class=
"name">
392 undefbehavior.LocalStaticDestroyed
</span><span class=
"lang">
393 (C++)
</span><div class=
"descr">
394 Undefined behavior: function containing a definition of static local object is
395 called during the destruction of an object with static storage duration so that
396 flow of control passes through the definition of the previously destroyed
398 <p>Source: C++
11 3.6.3p2.
</p></div></div></td>
399 <td><div class=
"exampleContainer expandable">
400 <div class=
"example"><pre>
417 </pre></div></div></td>
418 <td class=
"aligned"></td></tr>
421 <tr><td><div class=
"namedescr expandable"><span class=
"name">
422 undefbehavior.ZeroAllocDereference
</span><span class=
"lang">
423 (C, C++)
</span><div class=
"descr">
424 The effect of dereferencing a pointer returned as a request for zero size is
426 Note: possibly an enhancement to
<span class=
"name">
428 <p>Source: C++
03 3.7.3.1p2; C++
11 3.7.4.1p2.
</p></div></div></td>
429 <td><div class=
"exampleContainer expandable">
430 <div class=
"example"><pre>
431 #include
<stdlib.h
>
434 int *p = (int *)malloc(
0);
439 <div class=
"example"><pre>
447 </pre></div></div></td>
448 <td class=
"aligned"><a href=
"https://reviews.llvm.org/D8273">D8273
</a></td></tr>
451 <tr><td><div class=
"namedescr expandable"><span class=
"name">
452 undefbehavior.DeadReferenced
</span><span class=
"lang">
453 (C++)
</span><div class=
"descr">
454 Undefined behavior: the following usage of the pointer to the object whose
455 lifetime has ended can result in undefined behavior:
<br>
456 The object will be or was of a class type with a non-trivial destructor and
457 <ul><li>the pointer is used as the operand of a delete-expression
</li></ul>
458 The object will be or was of a non-POD class type (C++
11: any class type) and
459 <ul><li>the pointer is used to access a non-static data member or call a
460 non-static member function of the object
</li>
461 <li>the pointer is implicitly converted to a pointer to a base class
463 <li>the pointer is used as the operand of a
<code>static_cast
</code> (except
464 when the conversion is to
<code>void*
</code>, or to
<code>void*
</code> and
465 subsequently to
<code>char*
</code>, or
<code>unsigned char*
</code>)
</li>
466 <li>the pointer is used as the operand of a
<code>dynamic_cast
</code></li></ul>
467 <p>Source: C++
03 3.8p5, p7; C++
11 3.8p5, p7.
</p></div></div></td>
468 <td><div class=
"exampleContainer expandable">
469 <div class=
"example"><pre>
477 class B : public A {};
485 <div class=
"example"><pre>
501 <div class=
"example"><pre>
509 class B : public A {};
521 <div class=
"example"><pre>
529 class B : public A {};
536 return static_cast
<A*
>(b); // warn
539 <div class=
"example"><pre>
547 class B : public A {};
554 return dynamic_cast
<A*
>(b); // warn
556 </pre></div></div></td>
557 <td class=
"aligned"></td></tr>
560 <tr><td><div class=
"namedescr expandable"><span class=
"name">
561 undefbehavior.ObjLocChanges
</span><span class=
"lang">
562 (C++)
</span><div class=
"descr">
563 Undefined behavior: the program must ensure that an object occupies the same
564 storage location when the implicit or explicit destructor call takes place.
565 <p>Source: C++
11 3.8p8.
</p></div></div></td>
566 <td><div class=
"exampleContainer expandable">
567 <div class=
"example"><pre>
582 <div class=
"example"><pre>
597 </pre></div></div></td>
598 <td class=
"aligned"></td></tr>
601 <tr><td><div class=
"namedescr expandable"><span class=
"name">
602 undefbehavior.ExprEvalOrderUndef
</span><span class=
"lang">
603 (C, C++
03)
</span><div class=
"descr">
604 Undefined behavior: a scalar object shall have its stored value modified at
605 most once by the evaluation of an expression.
<br>
606 Note: most cases are currently handled by the Clang core (search for 'multiple
607 unsequenced modifications' warning in Clang tests).
608 <p>Source: C++
03 5p4.
</p></div></div></td>
609 <td><div class=
"exampleContainer expandable">
610 <div class=
"example"><pre>
616 </pre></div></div></td>
617 <td class=
"aligned"></td></tr>
620 <tr><td><div class=
"namedescr expandable"><span class=
"name">
621 undefbehavior.StaticInitReentered
</span><span class=
"lang">
622 (C++)
</span><div class=
"descr">
623 Undefined behavior: static declaration is re-entered while the object is being
625 <p>Source: C++
11 6.7p4.
</p></div></div></td>
626 <td><div class=
"exampleContainer expandable">
627 <div class=
"example"><pre>
629 static int s = test(
2 * i); // warn
632 </pre></div></div></td>
633 <td class=
"aligned"></td></tr>
636 <tr><td><div class=
"namedescr expandable"><span class=
"name">
637 undefbehavior.ConstModified
</span><span class=
"lang">
638 (C, C++)
</span><div class=
"descr">
639 Undefined behavior: const object is being modified.
640 <p>Source: C++
03 7.1.5.1p4, C++
11 7.1.6.1p4.
</p></div></div></td>
641 <td><div class=
"exampleContainer expandable">
642 <div class=
"example"><pre>
644 const int *cp = new const int (
0);
645 int *p = const_cast
<int *
>(cp);
650 <div class=
"example"><pre>
660 C* cp = const_cast
<C *
>(&cb);
661 cp-
>i =
1; // warn
663 </pre></div></div></td>
664 <td class=
"aligned"></td></tr>
667 <tr><td><div class=
"namedescr expandable"><span class=
"name">
668 undefbehavior.DeadDestructed
</span><span class=
"lang">
669 (C++)
</span><div class=
"descr">
670 Undefined behavior: the destructor is invoked for an object whose lifetime
672 <p>Source: C++
11 12.4p14.
</p></div></div></td>
673 <td><div class=
"exampleContainer expandable">
674 <div class=
"example"><pre>
686 </pre></div></div></td>
687 <td class=
"aligned"></td></tr>
690 <tr><td><div class=
"namedescr expandable"><span class=
"name">
691 undefbehavior.MethodCallBeforeBaseInit
</span><span class=
"lang">
692 (C++)
</span><div class=
"descr">
693 Undefined behavior: calls member function but base not yet initialized.
694 <p>Source: C++
03 12.6.2p8; C++
11 12.6.2p13.
</p></div></div></td>
695 <td><div class=
"exampleContainer expandable">
696 <div class=
"example"><pre>
705 B() : A(f()) {} // warn
707 </pre></div></div></td>
708 <td class=
"aligned"></td></tr>
711 <tr><td><div class=
"namedescr expandable"><span class=
"name">
712 undefbehavior.MemberOrBaseRefBeforeCtor
</span><span class=
"lang">
713 (C++)
</span><div class=
"descr">
714 C++ Undefined behavior: non-static member or base class of non-POD class type
715 is referred before constructor begins execution.
<br>
716 C++
11 Undefined behavior: non-static member or base class of a class with a
717 non-trivial constructor is referred before constructor begins execution.
718 <p>Source: C++
03 12.7p1; C++
11 12.7p1.
</p></div></div></td>
719 <td><div class=
"exampleContainer expandable">
720 <div class=
"example"><pre>
726 extern non_POD non_pod;
728 int *p =
&non_pod.i; // warn
730 <div class=
"example"><pre>
735 struct non_POD : public POD {
739 extern non_POD non_pod;
741 int *p =
&non_pod.pod.i; // warn
743 <div class=
"example"><pre>
748 struct non_POD : public POD {};
750 extern non_POD non_pod;
752 POD *p =
&non_pod; // warn
754 <div class=
"example"><pre>
763 S() : k(
&non_pod.i) {} // warn
765 </pre></div></div></td>
766 <td class=
"aligned"></td></tr>
769 <tr><td><div class=
"namedescr expandable"><span class=
"name">
770 undefbehavior.MemberRefAfterDtor
</span><span class=
"lang">
771 (C++)
</span><div class=
"descr">
772 C++
03: Undefined behavior: non-static member of non-POD class type is referred
773 after destructor ends execution.
<br>
774 C++
11: Undefined behavior: non-static member of a class with a non-trivial
775 destructor is referred after destructor ends execution.
776 <p>Source: C++
03 12.7p1; C++
11 12.7p1.
</p></div></div></td>
777 <td><div class=
"exampleContainer expandable">
778 <div class=
"example"><pre>
790 </pre></div></div></td>
791 <td class=
"aligned"></td></tr>
794 <tr><td><div class=
"namedescr expandable"><span class=
"name">
795 undefbehavior.CtorForeignCall
</span><span class=
"lang">
796 (C++)
</span><div class=
"descr">
797 Undefined behavior: call to virtual function of an object under construction
798 whose type is neither the constructors own class or one of its bases.
799 <p>Source: C++
11 12.7p4.
</p></div></div></td>
800 <td><div class=
"exampleContainer expandable">
801 <div class=
"example"><pre>
809 B(A* a) { a-
>f(); } // warn
812 class C : public A, B {
816 </pre></div></div></td>
817 <td class=
"aligned"></td></tr>
820 <tr><td><div class=
"namedescr expandable"><span class=
"name">
821 undefbehavior.CtorForeignTypeid
</span><span class=
"lang">
822 (C++)
</span><div class=
"descr">
823 Undefined behavior: the operand of
<code>typeid
</code> is an object under
824 construction whose type is neither the constructors own class or one of its
826 <p>Source: C++
11 12.7p5.
</p></div></div></td>
827 <td><div class=
"exampleContainer expandable">
828 <div class=
"example"><pre>
829 #include
<typeinfo
>
836 (void)typeid(*a); // warn
840 class C : public A, B {
844 </pre></div></div></td>
845 <td class=
"aligned"></td></tr>
848 <tr><td><div class=
"namedescr expandable"><span class=
"name">
849 undefbehavior.CtorForeignCast
</span><span class=
"lang">
850 (C++)
</span><div class=
"descr">
851 Undefined behavior: the operand of
<code>dynamic_cast
</code> is an object under
852 construction whose type is neither the constructors own class or one of its
854 <p>Source: C++
11 12.7p6.
</p></div></div></td>
855 <td><div class=
"exampleContainer expandable">
856 <div class=
"example"><pre>
857 #include
<typeinfo
>
867 (void)dynamic_cast
<B*
>(a); //warn
871 class C : public A, B {
875 </pre></div></div></td>
876 <td class=
"aligned"></td></tr>
879 <tr><td><div class=
"namedescr expandable"><span class=
"name">
880 undefbehavior.MemberOrBaseRefInCatch
</span><span class=
"lang">
881 (C++)
</span><div class=
"descr">
882 Undefined behavior: referring to any non-static member or base class of an
883 object in the handler for a function-try-block of a constructor or destructor
884 for that object results in undefined behavior.
885 <p>Source: C++
11 15.3p10.
</p></div></div></td>
886 <td><div class=
"exampleContainer expandable">
887 <div class=
"example"><pre>
888 void f() { throw
1; }
902 <div class=
"example"><pre>
903 void f() { throw
1; }
910 class C: public Base {
919 </pre></div></div></td>
920 <td class=
"aligned"></td></tr>
923 <tr><td><div class=
"namedescr expandable"><span class=
"name">
924 undefbehavior.ReturnAtCatchEnd
</span><span class=
"lang">
925 (C++)
</span><div class=
"descr">
926 Undefined behavior: a function returns when control reaches the end of a
927 handler. This results in undefined behavior in a value-returning function.
928 <p>Source: C++
11 15.3p10.
</p></div></div></td>
929 <td><div class=
"exampleContainer expandable">
930 <div class=
"example"><pre>
931 void f() { throw
1; }
939 </pre></div></div></td>
940 <td class=
"aligned"></td></tr>
943 <tr><td><div class=
"namedescr expandable"><span class=
"name">
944 undefbehavior.AutoptrsOwnSameObj
</span><span class=
"lang">
945 (C++
03)
</span><div class=
"descr">
946 Undefined behavior: if more than one
<code>auto_ptr
</code> owns the same object
947 at the same time the behavior of the program is undefined.
948 <p>Source: C++
03 20.4.5p3; C++
11 <code>auto_ptr
</code> is deprecated
949 (D
.10).
</p></div></div></td>
950 <td><div class=
"exampleContainer expandable">
951 <div class=
"example"><pre>
952 #include
<memory
>
956 std::auto_ptr
<int
> p(data);
957 std::auto_ptr
<int
> q(data); // warn
959 </pre></div></div></td>
960 <td class=
"aligned"></td></tr>
963 <tr><td><div class=
"namedescr expandable"><span class=
"name">
964 undefbehavior.BasicStringOutOfBound
</span><span class=
"lang">
965 (C++
03)
</span><div class=
"descr">
966 Undefined behavior: out-of-bound
<code>basic_string
</code> access/modification.
967 <br>Note: possibly an enhancement to
<span class=
"name">
968 alpha.security.ArrayBoundV2
</span>.
969 <p>Source: C++
03 21.3.4p1; C++
11 behavior is defined
970 (
21.4.5p2).
</p></div></div></td>
971 <td><div class=
"exampleContainer expandable">
972 <div class=
"example"><pre>
973 #include
<string
>
976 std::basic_string
<char
> s;
977 char c = s[
10]; // warn
980 <div class=
"example"><pre>
981 #include
<string
>
984 std::basic_string
<char
> s;
987 </pre></div></div></td>
988 <td class=
"aligned"></td></tr>
991 <tr><td><div class=
"namedescr expandable"><span class=
"name">
992 undefbehavior.EosDereference
</span><span class=
"lang">
993 (C++)
</span><div class=
"descr">
994 Undefined behavior: the result of
<code>operator*()
</code> on an end of a
996 <p>Source: C++
03 24.5.3p2; C++
11 24.6.3p2.
</p></div></div></td>
997 <td><div class=
"exampleContainer expandable">
998 <div class=
"example"><pre>
999 #include
<vector
>
1002 std::vector
<int
> v;
1003 return *v.end(); // warn
1005 </pre></div></div></td>
1006 <td class=
"aligned"></td></tr>
1009 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1010 undefbehavior.QsortNonPODNonTrivial
</span><span class=
"lang">
1011 (C++)
</span><div class=
"descr">
1012 C++
03: Undefined behavior: the objects in the array passed to qsort are of
1014 C++
11: Undefined behavior: the objects in the array passed to qsort are of
1016 <p>Source: C++
03 25.4p4; C++
11 25.5p4.
</p></div></div></td>
1017 <td><div class=
"exampleContainer expandable">
1018 <div class=
"example"><pre>
1020 #include
<cstdlib
>
1027 non_POD values[] = { non_POD(), non_POD() };
1029 int compare(const void *a, const void *b);
1032 qsort(values,
2, sizeof(non_POD), compare); // warn
1035 <div class=
"example"><pre>
1037 #include
<cstdlib
>
1041 struct trivial_non_POD : public S {
1045 struct non_trivial {
1050 trivial_non_POD tnp[
2];
1053 int compare1(const void *a, const void *b);
1055 int compare2(const void *a, const void *b);
1058 qsort(tnp,
2, sizeof(trivial_non_POD), compare1); // ok
1059 qsort(nt,
2, sizeof(non_trivial), compare2); // warn
1061 </pre></div></div></td>
1062 <td class=
"aligned"></td></tr>
1065 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1066 undefbehavior.ThrowWhileCopy
</span><span class=
"lang">
1067 (C++)
</span><div class=
"descr">
1068 Undefined behavior: copy constructor/assignment operator can throw an exception.
1069 The effects are undefined if an exception is thrown.
</div></div></td>
1070 <td><div class=
"exampleContainer expandable">
1071 <div class=
"example"><pre>
1075 C (const C
&c) {
1082 <div class=
"example"><pre>
1086 C
&operator=(const C
&c) {
1092 </pre></div></div></td>
1093 <td class=
"aligned"></td></tr>
1096 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1097 undefbehavior.ValarrayArgBound
</span><span class=
"lang">
1098 (C++)
</span><div class=
"descr">
1099 Undefined behavior: the value of the
<code><i>n
</i></code> argument passed
1100 to
<code>valarray
</code> constructor is greater than the number of values
1101 pointed to by the first argument (source).
1102 <p>Source: C++
03 26.3.2.1p4; C++
11 26.6.2.2p4.
</p></div></div></td>
1103 <td><div class=
"exampleContainer expandable">
1104 <div class=
"example"><pre>
1105 #include
<valarray
>
1109 S(int ii) : i(ii) {};
1113 S s[] = { S(
1), S(
2) };
1114 std::valarray
<S
> v(s,
3); // warn
1116 </pre></div></div></td>
1117 <td class=
"aligned"></td></tr>
1120 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1121 undefbehavior.ValarrayLengthDiffer
</span><span class=
"lang">
1122 (C++)
</span><div class=
"descr">
1123 Undefined behavior:
<code>valarray
</code> operands are of different length.
1124 <p>Source: C++
03 26.3.2.2p1,
26.3.2.6p3,
26.3.3.1p3,
26.3.3.2p3;
1125 C++
11 defined (
26.6.2.3p1),
26.6.2.7p3,
26.6.3.1p3,
1126 26.6.3.2p3.
</p></div></div></td>
1127 <td><div class=
"exampleContainer expandable">
1128 <div class=
"example"><pre>
1130 #include
<valarray
>
1133 std::valarray
<int
> a(
0,
1), b(
0,
2);
1139 <div class=
"example"><pre>
1141 #include
<valarray
>
1144 std::valarray
<int
> a(
0,
1), b(
0,
2);
1148 <div class=
"example"><pre>
1150 #include
<valarray
>
1153 std::valarray
<int
> a(
0,
1), b(
0,
2);
1157 <div class=
"example"><pre>
1159 #include
<valarray
>
1162 std::valarray
<int
> a(
0,
1), b(
0,
2);
1163 std::valarray
<bool
> c(false,
1);
1166 </pre></div></div></td>
1167 <td class=
"aligned"></td></tr>
1170 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1171 undefbehavior.ValarrayZeroLength
</span><span class=
"lang">
1172 (C++)
</span><div class=
"descr">
1173 Undefined behavior: calling
<code>sum()
</code>/
<code>min()
</code>/
<code>
1174 max()
</code> methods of a zero length
<code>valarray
<code> the behavior is
1176 <p>Source: C++
03 26.3.2.7p2, p3, p4; C++
11 26.6.2.8p5, p6,
1177 p7.
</p></div></div></td>
1178 <td><div class=
"exampleContainer expandable">
1179 <div class=
"example"><pre>
1180 #include
<valarray
>
1183 std::valarray
<int
> v(
0,
0);
1186 </pre></div></div></td>
1187 <td class=
"aligned"></td></tr>
1190 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1191 undefbehavior.ValarrayBadIndirection
</span><span class=
"lang">
1192 (C++)
</span><div class=
"descr">
1193 Undefined behavior: element is specified more than once in an indirection.
1194 <p>Source: C++
03 26.3.9.2p2,
26.3.9.3p2; C++
11 26.6.9.2p2,
1195 26.6.9.3p2.
</p></div></div></td>
1196 <td><div class=
"exampleContainer expandable">
1197 <div class=
"example"><pre>
1198 #include
<valarray
>
1201 // '
1' is specified more then once
1202 size_t addr[] = {
0,
1,
1};
1203 std::valarray
<size_t
>indirect(addr,
3);
1204 std::valarray
<int
> a(
0,
5), b(
1,
3);
1205 a[indirect] = b; //warn
1208 <div class=
"example"><pre>
1209 #include
<valarray
>
1212 // '
1' is specified more then once
1213 size_t addr[] = {
0,
1,
1};
1214 std::valarray
<size_t
>indirect(addr,
3);
1215 std::valarray
<int
> a(
0,
5), b(
1,
3);
1216 a[indirect] *= b; //warn
1218 </pre></div></div></td>
1219 <td class=
"aligned"></td></tr>
1222 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1223 undefbehavior.IosBaseDestroyedBeforeInit
</span><span class=
"lang">
1224 (C++)
</span><div class=
"descr">
1225 Undefined behavior:
<code>ios_base
</code> object is destroyed before
1226 initialization have taken place.
<code>basic_ios::init
</code> should be call to
1227 initialize
<code>ios_base
</code> members.
1228 <p>Source: C++
03 27.4.2.7p1,
27.4.4.1p2; C++
11 27.5.3.7p1,
1229 27.5.5.2p2.
</p></div></div></td>
1230 <td><div class=
"exampleContainer expandable">
1231 <div class=
"example"><pre>
1232 #include
<ios
>
1234 using namespace std;
1235 template
<class T, class Traits = std::char_traits
<T
> >
1236 class my_stream1 : public std::basic_ios
<T, Traits
> {
1239 template
<class T, class Traits = std::char_traits
<T
> >
1240 class my_stream2 : public std::basic_ios
<T, Traits
> {
1242 : public std::basic_streambuf
<T, Traits
> {
1246 this-
>init(new my_streambuf);
1251 my_stream1
<char
> *p1 = new my_stream1
<char
>;
1252 my_stream2
<char
> *p2 = new my_stream2
<char
>;
1256 </pre></div></div></td>
1257 <td class=
"aligned"></td></tr>
1260 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1261 undefbehavior.IosBaseUsedBeforeInit
</span><span class=
"lang">
1262 (C++
11)
</span><div class=
"descr">
1263 Undefined behavior:
<code>ios_base
</code> object is used before initialization
1264 have taken place.
<code>basic_ios::init
</code> should be call to
1265 initialize
<code>ios_base
</code> members.
1266 <p>Source: C++
11 27.5.3.7p1,
27.5.5.2p2.
</p></div></div></td>
1267 <td><div class=
"exampleContainer expandable">
1268 <div class=
"example"><pre>
1269 #include
<ios
>
1271 using namespace std;
1272 template
<class T, class Traits = std::char_traits
<T
> >
1273 class my_stream1 : public std::basic_ios
<T, Traits
> {
1276 template
<class T, class Traits = std::char_traits
<T
> >
1277 class my_stream2 : public std::basic_ios
<T, Traits
> {
1279 : public std::basic_streambuf
<T, Traits
> {
1283 this-
>init(new my_streambuf);
1288 my_stream1
<char
> *p1 = new my_stream1
<char
>;
1289 my_stream2
<char
> *p2 = new my_stream2
<char
>;
1290 p1-
>narrow('a', 'b'); // warn
1291 p2-
>narrow('a', 'b'); // ok
1293 </pre></div></div></td>
1294 <td class=
"aligned"></td></tr>
1297 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1298 undefbehavior.MinusOnePosType
</span><span class=
"lang">
1299 (C++)
</span><div class=
"descr">
1300 Undefined behavior: passing -
1 to any
<code>streambuf
</code>/
<code>
1301 istream
</code>/
<code>ostream
</code> member that accepts a value of
1302 type
<code>traits::pos_type
</code> result in undefined behavior.
1303 <p>Source: C++
03 27.4.3.2p3; C++
11 27.5.4.2p3.
</p></div></div></td>
1304 <td><div class=
"exampleContainer expandable">
1305 <div class=
"example"><pre>
1306 #include
<fstream
>
1308 class my_streambuf : public std::streambuf {
1310 seekpos(-
1); // warn
1314 <div class=
"example"><pre>
1315 #include
<fstream
>
1319 std::istream in(
&fb);
1320 std::filebuf::off_type pos(-
1);
1321 in.seekg(pos); // warn
1323 </pre></div></div></td>
1324 <td class=
"aligned"></td></tr>
1328 <!-- ============================ different ================================ -->
1330 <table class=
"checkers">
1331 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
1332 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr>
1335 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1336 different.SuccessiveAssign
</span><span class=
"lang">
1337 (C)
</span><div class=
"descr">
1338 Successive assign to a variable.
</div></div></td>
1339 <td><div class=
"exampleContainer expandable">
1340 <div class=
"example"><pre>
1347 </pre></div></div></td>
1348 <td class=
"aligned"></td></tr>
1351 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1352 different.NullDerefStmtOrder
</span><span class=
"lang">
1353 (C)
</span><div class=
"descr">
1354 Dereferencing of the null pointer might take place. Checking the pointer for
1355 null should be performed first.
1356 <br>Note: possibly an enhancement to
<span class=
"name">
1357 core.NullDereference
</span>.
</div></div></td>
1358 <td><div class=
"exampleContainer expandable">
1359 <div class=
"example"><pre>
1368 int x1 = p1-
>x; // warn
1372 int x2 = p2-
>x; // ok
1374 </pre></div></div></td>
1375 <td class=
"aligned"></td></tr>
1378 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1379 different.NullDerefCondOrder
</span><span class=
"lang">
1380 (C)
</span><div class=
"descr">
1381 Dereferencing of the null pointer might take place. Checking the pointer for
1382 null should be performed first.
1383 <br>Note: possibly an enhancement to
<span class=
"name">
1384 core.NullDereference
</span>.
</div></div></td>
1385 <td><div class=
"exampleContainer expandable">
1386 <div class=
"example"><pre>
1393 if (p-
>i && p) {}; // warn
1395 </pre></div></div></td>
1396 <td class=
"aligned"></td></tr>
1399 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1400 different.MultipleAccessors
</span><span class=
"lang">
1401 (C++)
</span><div class=
"descr">
1402 Identical accessor bodies. Possibly a misprint.
</div></div></td>
1403 <td><div class=
"exampleContainer expandable">
1404 <div class=
"example"><pre>
1409 int getI() { return i; }
1410 int getJ() { return i; } // warn
1413 <div class=
"example"><pre>
1418 void setI(int& ii) { i = ii; }
1419 void setJ(int& jj) { i = jj; } // warn
1421 </pre></div></div></td>
1422 <td class=
"aligned"></td></tr>
1425 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1426 different.AccessorsForPublic
</span><span class=
"lang">
1427 (C++)
</span><div class=
"descr">
1428 Accessors exist for a public class field. Should this field really be
1429 public?
</div></div></td>
1430 <td><div class=
"exampleContainer expandable">
1431 <div class=
"example"><pre>
1435 int getI() { return i; }
1436 void setI(int& ii) { i = ii; }
1438 </pre></div></div></td>
1439 <td class=
"aligned"></td></tr>
1442 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1443 different.LibFuncResultUnised
</span><span class=
"lang">
1444 (C, C++)
</span><div class=
"descr">
1445 Calling a function ignoring its return value is of no use (create the list of
1446 known system/library/API functions falling into this category).
</div></div></td>
1447 <td><div class=
"exampleContainer expandable">
1448 <div class=
"example"><pre>
1449 #include
<vector
>
1452 std::vector
<int
> v;
1455 </pre></div></div></td>
1456 <td class=
"aligned"></td></tr>
1459 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1460 different.WrongVarForStmt
</span><span class=
"lang">
1461 (C, C++)
</span><div class=
"descr">
1462 Wrong variable is possibly used in the loop/cond-expression of
1463 the
<code>for
</code> statement. Did you mean
1464 'proper_variable_name'?
</div></div></td>
1465 <td><div class=
"exampleContainer expandable">
1466 <div class=
"example"><pre>
1470 for (i =
0; i <
3; j +=
1); // warn
1473 <div class=
"example"><pre>
1477 for (int j =
0; i <
3; ++j); // warn
1479 </pre></div></div></td>
1480 <td class=
"aligned"></td></tr>
1483 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1484 different.FloatingCompare
</span><span class=
"lang">
1485 (C)
</span><div class=
"descr">
1486 Comparing floating point numbers may be not precise.
</div></div></td>
1487 <td><div class=
"exampleContainer expandable">
1488 <div class=
"example"><pre>
1489 #include
<math.h
>
1492 double b = sin(M_PI /
6.0);
1493 if (b ==
0.5) // warn
1497 </pre></div></div></td>
1498 <td class=
"aligned"></td></tr>
1501 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1502 different.BitwiseOpBoolArg
</span><span class=
"lang">
1503 (C, C++)
</span><div class=
"descr">
1504 Boolean value met at the left/right part of the bitwise
<code>&</code>
1505 or
<code>|
</code> operator.
1506 Did you mean
<code>&&</code> (
<code>||
</code>) ?
</div></div></td>
1507 <td><div class=
"exampleContainer expandable">
1508 <div class=
"example"><pre>
1513 if (b
& f()) {} // warn
1515 </pre></div></div></td>
1516 <td class=
"aligned"></td></tr>
1519 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1520 different.LabelInsideSwitch
</span><span class=
"lang">
1521 (C)
</span><div class=
"descr">
1522 Possibly a misprint: label found inside a
<code>switch()
</code>
1523 statement.
</div></div></td>
1524 <td><div class=
"exampleContainer expandable">
1525 <div class=
"example"><pre>
1530 defalt: // warn (did you mean 'default'?)
1534 </pre></div></div></td>
1535 <td class=
"aligned"></td></tr>
1538 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1539 different.IdenticalCondIfIf
</span><span class=
"lang">
1540 (C)
</span><div class=
"descr">
1541 The conditions of two subsequent
<code>if
</code> statements are
1542 identical.
</div></div></td>
1543 <td><div class=
"exampleContainer expandable">
1544 <div class=
"example"><pre>
1548 if (c
> 5) // warn
1552 </pre></div></div></td>
1553 <td class=
"aligned"></td></tr>
1556 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1557 different.LogicalOpUselessArg
</span><span class=
"lang">
1558 (C)
</span><div class=
"descr">
1559 The second operand of a
<code>&&</code> operator has no impact on
1560 expression result.
</div></div></td>
1561 <td><div class=
"exampleContainer expandable">
1562 <div class=
"example"><pre>
1563 void test(unsigned a) {
1564 if (a
<7 && a
<10) {}; // warn
1566 </pre></div></div></td>
1567 <td class=
"aligned"></td></tr>
1570 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1571 different.SameResLogicalExpr
</span><span class=
"lang">
1572 (C)
</span><div class=
"descr">
1573 An expression is always evaluated to true/false.
</div></div></td>
1574 <td><div class=
"exampleContainer expandable">
1575 <div class=
"example"><pre>
1578 if (i !=
0) {}; // warn
1581 <div class=
"example"><pre>
1583 if (i ==
0 && i ==
1) {}; // warn
1586 <div class=
"example"><pre>
1588 if (i <
0 || i
>=
0) {}; // warn
1590 </pre></div></div></td>
1591 <td class=
"aligned"></td></tr>
1594 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1595 different.OpPrecedenceAssignCmp
</span><span class=
"lang">
1596 (C, C++)
</span><div class=
"descr">
1597 Comparison operation has higher precedence then assignment. Boolean value is
1598 assigned to a variable of other type. Parenthesis may bee required around an
1599 assignment.
</div></div></td>
1600 <td><div class=
"exampleContainer expandable">
1601 <div class=
"example"><pre>
1604 void test(int x, int y) {
1606 if((b = x != y)) {} // ok
1607 if((x = f() != y)) {} // warn
1609 </pre></div></div></td>
1610 <td class=
"aligned"></td></tr>
1613 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1614 different.OpPrecedenceIifShift
</span><span class=
"lang">
1615 (C, C++)
</span><div class=
"descr">
1616 <code>?:
</code> has lower precedence then
<code><<</code>.
1617 <p>Source: Stephen C. Dewhurst
"C++ Gotchas: Avoiding Common Problems in Coding
1618 and Design", advise
15.
</p></div></div></td>
1619 <td><div class=
"exampleContainer expandable">
1620 <div class=
"example"><pre>
1621 #include
<iostream
>
1624 std::cout
<< a ?
"a" :
"b"; // warn
1627 <div class=
"example"><pre>
1629 a
<< a
> 7 ?
1 :
2; // warn
1631 </pre></div></div></td>
1632 <td class=
"aligned"></td></tr>
1635 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1636 different.ObjectUnused
</span><span class=
"lang">
1637 (C++)
</span><div class=
"descr">
1638 The object was created but is not being used.
</div></div></td>
1639 <td><div class=
"exampleContainer expandable">
1640 <div class=
"example"><pre>
1643 S(int xx, int yy) : x(xx), y(yy) {}
1649 <div class=
"example"><pre>
1650 #include
<exception
>
1654 // warn (did you mean 'throw std::exception()'?)
1656 </pre></div></div></td>
1657 <td class=
"aligned"></td></tr>
1660 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1661 different.StaticArrayPtrCompare
</span><span class=
"lang">
1662 (C)
</span><div class=
"descr">
1663 Pointer to static array is being compared to NULL. May the subscripting is
1664 missing.
</div></div></td>
1665 <td><div class=
"exampleContainer expandable">
1666 <div class=
"example"><pre>
1669 if (a[
0] ==
0) {}; // warn
1671 </pre></div></div></td>
1672 <td class=
"aligned"></td></tr>
1675 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1676 different.ConversionToBool
</span><span class=
"lang">
1677 (C, C++)
</span><div class=
"descr">
1678 Odd implicit conversion to boolean.
1679 <br>Note: possibly merge with
<span class=
"name">
1680 alpha.core.BoolAssignment
</span>.
</div></div></td>
1681 <td><div class=
"exampleContainer expandable">
1682 <div class=
"example"><pre>
1687 <div class=
"example"><pre>
1691 </pre></div></div></td>
1692 <td class=
"aligned"></td></tr>
1695 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1696 different.ArrayBound
</span><span class=
"lang">
1697 (C++)
</span><div class=
"descr">
1698 Out-of-bound dynamic array access.
1699 <br>Note: possibly an enhancement to
<span class=
"name">
1700 alpha.security.ArrayBoundV2
</span>.
</div></div></td>
1701 <td><div class=
"exampleContainer expandable">
1702 <div class=
"example"><pre>
1704 int *p = new int[
1];
1706 if(p[i]) {}; // warn
1709 </pre></div></div></td>
1710 <td class=
"aligned"></td></tr>
1713 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1714 different.StrcpyInputSize
</span><span class=
"lang">
1715 (C)
</span><div class=
"descr">
1716 Buffer copy without checking the size of input.
1717 <br>Note: possibly an enhancement to
<span class=
"name">
1718 alpha.unix.cstring.OutOfBounds
</span>.
</div></div></td>
1719 <td><div class=
"exampleContainer expandable">
1720 <div class=
"example"><pre>
1721 void test(char* string) {
1723 strcpy(buf, string); // warn
1725 </pre></div></div></td>
1726 <td class=
"aligned"></td></tr>
1729 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1730 different.IntegerOverflow
</span><span class=
"lang">
1731 (C)
</span><div class=
"descr">
1733 <br>Note: partially handled by Clang core
1734 (search for 'overflow in expression' warning in Clang tests).
1735 <p>Source:
<a href=
"https://cwe.mitre.org/data/definitions/190.html">
1736 CWE-
190</a>.
</p></div></div></td>
1737 <td><div class=
"exampleContainer expandable">
1738 <div class=
"example"><pre>
1739 #include
<limits.h
>
1744 f(INT_MAX +
1); // warn
1747 <div class=
"example"><pre>
1748 #include
<limits.h
>
1751 int x = INT_MAX /
2 +
1;
1752 return x *
2; // warn
1754 </pre></div></div></td>
1755 <td class=
"aligned"></td></tr>
1758 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1759 different.SignExtension
</span><span class=
"lang">
1760 (C)
</span><div class=
"descr">
1761 Unexpected sign extension might take place.
1762 <p>Source:
<a href=
"https://cwe.mitre.org/data/definitions/194.html">
1763 CWE-
194</a>.
</p></div></div></td>
1764 <td><div class=
"exampleContainer expandable">
1765 <div class=
"example"><pre>
1766 unsigned long long test(long long sll) {
1767 unsigned long long ull = sll; // warn
1771 <div class=
"example"><pre>
1772 void f(unsigned int i);
1778 <div class=
"example"><pre>
1779 unsigned int test(int i) {
1782 </pre></div></div></td>
1783 <td class=
"aligned"></td></tr>
1786 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1787 different.NumericTruncation
</span><span class=
"lang">
1788 (C)
</span><div class=
"descr">
1789 Numeric truncation might take place.
1790 <p>Source:
<a href=
"https://cwe.mitre.org/data/definitions/197.html">
1791 CWE-
197</a>.
</p></div></div></td>
1792 <td><div class=
"exampleContainer expandable">
1793 <div class=
"example"><pre>
1794 unsigned long test(unsigned long long ull) {
1795 unsigned long ul = ull; // warn
1799 <div class=
"example"><pre>
1802 void test(long long sll) {
1806 <div class=
"example"><pre>
1809 short test(long long sll) {
1813 </pre></div></div></td>
1814 <td class=
"aligned"></td></tr>
1817 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1818 different.MissingCopyCtorAssignOp
</span><span class=
"lang">
1819 (C++)
</span><div class=
"descr">
1820 A class has dynamically allocated data members but do not define a copy
1821 constructor/assignment operator.
1822 <p>Source: Scott Meyers
"Effective C++", item
11: Prevent exceptions from
1823 leaving destructors.
</p></div></div></td>
1824 <td><div class=
"exampleContainer expandable">
1825 <div class=
"example"><pre>
1829 C() { p = new int; }
1832 </pre></div></div></td>
1833 <td class=
"aligned"></td></tr>
1837 <!-- ============================ WinAPI =================================== -->
1839 <table class=
"checkers">
1840 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
1841 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
1843 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1844 WinAPI.CreateProcess
</span><span class=
"lang">
1845 (C)
</span><div class=
"descr">
1846 <code>CreateProcess()
</code>: if the first parameter
<code><i>
1847 lpApplicationName
</i></code> is NULL then the executable name must be in the
1848 white space-delimited string pointed to by
<code><i>lpCommandLine
</code></i>.
1849 If the executable or path name has a space in it, there is a risk that a
1850 different executable could be run because of the way the function parses
1852 <p>Source:
<a href=
"https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#security-remarks">
1853 MSDN: CreateProcess function, Security Remarks
</a>.
</p></div></div></td>
1854 <td><div class=
"exampleContainer expandable">
1855 <div class=
"example"><pre>
1856 #include
<windows.h
>
1860 PROCESS_INFORMATION pi;
1861 CreateProcess(NULL, TEXT(
"C:\\Program Files\\App -L -S"),
1862 NULL, NULL, TRUE,
0, NULL, NULL, &si, &pi);
1865 </pre></div></div></td>
1866 <td class=
"aligned"></td></tr>
1869 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1870 WinAPI.LoadLibrary
</span><span class=
"lang">
1871 (C)
</span><div class=
"descr">
1872 The
<code>SearchPath()
</code> function is used to retrieve a path to a DLL for
1873 a subsequent
<code>LoadLibrary()
</code> call.
1874 <p>Source:
<a href=
"https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya#security-remarks">
1875 MSDN: LoadLibrary function, Security Remarks
</a>.
</p></div></div></td>
1876 <td><div class=
"exampleContainer expandable">
1877 <div class=
"example"><pre>
1878 #include
<windows.h
>
1882 SearchPath(NULL,
"file.dll", NULL,
100, filePath, NULL);
1883 return LoadLibrary(filePath); // warn
1885 </pre></div></div></td>
1886 <td class=
"aligned"></td></tr>
1889 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1890 WinAPI.WideCharToMultiByte
</span><span class=
"lang">
1891 (C)
</span><div class=
"descr">
1892 Buffer overrun while calling
<code>WideCharToMultiByte()
</code>. The size of
1893 the input buffer equals the number of characters in the Unicode string, while
1894 the size of the output buffer equals the number of bytes.
1895 <p>Source:
<a href=
"https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte">
1896 MSDN: WideCharToMultiByte function
</a>.
</p></div></div></td>
1897 <td><div class=
"exampleContainer expandable">
1898 <div class=
"example"><pre>
1899 #include
<windows.h
>
1902 wchar_t ws[] = L
"abc";
1904 WideCharToMultiByte(CP_UTF8,
0, ws, -
1, s,
1905 3, NULL, NULL); // warn
1907 </pre></div></div></td>
1908 <td class=
"aligned"></td></tr>
1913 <!-- =========================== optimization ============================== -->
1914 <h3>optimization
</h3>
1915 <table class=
"checkers">
1916 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
1917 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
1919 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1920 optimization.PassConstObjByValue
</span><span class=
"lang">
1921 (C, C++)
</span><div class=
"descr">
1922 Optimization: It is more effective to pass constant parameter by reference to
1923 avoid unnecessary object copying.
</div></div></td>
1924 <td><div class=
"exampleContainer expandable">
1925 <div class=
"example"><pre>
1928 void f(const struct A a); // warn
1929 </pre></div></div></td>
1930 <td class=
"aligned"></td></tr>
1933 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1934 optimization.PostfixIncIter
</span><span class=
"lang">
1935 (C++)
</span><div class=
"descr">
1936 Optimization: It is more effective to use prefix increment operator with
1938 <p>Source: Scott Meyers
"More Effective C++", item
6:
1939 Distinguish between prefix and postfix forms of increment and decrement
1940 operators.
</p></div></div></td>
1941 <td><div class=
"exampleContainer expandable">
1942 <div class=
"example"><pre>
1943 #include
<vector
>
1946 std::vector
<int
> v;
1947 std::vector
<int
>::const_iterator it;
1949 it != v.end(); it++) {}; // warn
1951 </pre></div></div></td>
1952 <td class=
"aligned"></td></tr>
1955 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1956 optimization.MultipleCallsStrlen
</span><span class=
"lang">
1957 (C)
</span><div class=
"descr">
1958 Optimization: multiple calls to
<code>strlen()
</code> for a string in an
1959 expression. It is more effective to hold a value returned
1960 from
<code>strlen()
</code> in a temporary variable.
</div></div></td>
1961 <td><div class=
"exampleContainer expandable">
1962 <div class=
"example"><pre>
1963 #include
<string.h
>
1965 void test(const char* s) {
1966 if (strlen(s)
> 0 &&
1967 strlen(s)
< 7) {}; // warn
1969 </pre></div></div></td>
1970 <td class=
"aligned"></td></tr>
1973 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1974 optimization.StrLengthCalculation
</span><span class=
"lang">
1975 (C++)
</span><div class=
"descr">
1976 Optimization: it is more efficient to use
<code>string::length()
</code> to
1977 calculate the length of an
<code>std::string
</code>.
</div></div></td>
1978 <td><div class=
"exampleContainer expandable">
1979 <div class=
"example"><pre>
1980 #include
<string
>
1981 #include
<string.h
>
1985 if (strlen(s.c_str()) !=
0) {}; // warn
1987 </pre></div></div></td>
1988 <td class=
"aligned"></td></tr>
1991 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1992 optimization.EmptyContainerDetect
</span><span class=
"lang">
1993 (C++)
</span><div class=
"descr">
1994 Optimization: It is more efficient to use containers
<code>empty()
</code>
1995 method to identify an empty container.
</div></div></td>
1996 <td><div class=
"exampleContainer expandable">
1997 <div class=
"example"><pre>
1998 #include
<list
>
2001 std::list
<int
> l;
2002 if (l.size() !=
0) {}; // warn
2004 </pre></div></div></td>
2005 <td class=
"aligned"></td></tr>
2011 </div> <!-- page -->
2012 </div> <!-- content -->