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 <br>Note: possibly an enhancement to
<span class=
"name">
94 alpha.security.MallocOverflow
</span>.
95 <p>Source:
<a href=
"https://cwe.mitre.org/data/definitions/20.html">CWE-
20,
96 Example
2</a>.
</p></div></div></td>
97 <td><div class=
"exampleContainer expandable">
98 <div class=
"example"><pre>
102 p = new int[n1]; // warn
104 </pre></div></div></td>
105 <td class=
"aligned"></td></tr>
107 <tr><td><div class=
"namedescr expandable"><span class=
"name">
108 memory.ZeroAlloc
</span><span class=
"lang">
109 (C, C++)
</span><div class=
"descr">
110 Allocation of zero bytes.
111 <br>Note: an enhancement to
<span class=
"name">unix.Malloc
</span>.
112 <br>Note:
<span class=
"name">unix.API
</span> perform C-checks for zero
113 allocation. This should be moved to
<span class=
"name">unix.Malloc
</span>.
114 <p>Source: C++
03 3.7.3.1p2; C++
11 3.7.4.1p2.
</p></div></div></td>
115 <td><div class=
"exampleContainer expandable">
116 <div class=
"example"><pre>
117 #include
<stdlib.h
>
120 int *p = malloc(
0); // warn
124 <div class=
"example"><pre>
126 int *p = new int[
0]; // warn
129 </pre></div></div></td>
130 <td class=
"aligned"><a href=
"https://reviews.llvm.org/D6178">
135 <!-- ======================= constructors/destructors ====================== -->
136 <h3>constructors/destructors
</h3>
137 <table class=
"checkers">
138 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
139 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
141 <tr><td><div class=
"namedescr expandable"><span class=
"name">
142 ctordtor.ExptInsideDtor
</span><span class=
"lang">
143 (C++)
</span><div class=
"descr">
144 It is dangerous to let an exception leave a destructor.
145 Using
<code>try..catch
</code> solves the problem.
146 <p>Source: Scott Meyers
"More Effective C++", item
11: Prevent exceptions from
147 leaving destructors.
</p></div></div></td>
148 <td><div class=
"exampleContainer expandable">
149 <div class=
"example"><pre>
152 ~A() { throw
1; } // warn
155 <div class=
"example"><pre>
160 ~A() { f(); } // warn
162 </pre></div></div></td>
163 <td class=
"aligned"></td></tr>
166 <tr><td><div class=
"namedescr expandable"><span class=
"name">
167 ctordtor.PlacementSelfCopy
</span><span class=
"lang">
168 (C++
11)
</span><div class=
"descr">
169 For a placement copy or move, it is almost certainly an error if the
170 constructed object is also the object being copied from.
</div></div></td>
171 <td><div class=
"exampleContainer expandable">
172 <div class=
"example"><pre>
175 void test(A *dst, A *src) {
176 ::new (dst) A(*dst); // warn (should be 'src')
178 </pre></div></div></td>
179 <td class=
"aligned"><!--rdar://problem/13688366--></td></tr>
183 <!-- ============================== exceptions ============================= -->
185 <table class=
"checkers">
186 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
187 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
189 <tr><td><div class=
"namedescr expandable"><span class=
"name">
190 exceptions.ThrowSpecButNotThrow
</span><span class=
"lang">
191 (C++)
</span><div class=
"descr">
192 Function declaration has a
<code>throw(
<i>type
</i>)
</code> specifier but the
193 function do not throw exceptions.
</div></div></td>
194 <td><div class=
"exampleContainer expandable">
195 <div class=
"example"><pre>
196 void test() throw(int) {
198 </pre></div></div></td>
199 <td class=
"aligned"></td></tr>
202 <tr><td><div class=
"namedescr expandable"><span class=
"name">
203 exceptions.NoThrowSpecButThrows
</span><span class=
"lang">
204 (C++)
</span><div class=
"descr">
205 An exception is throw from a function having a
<code>throw()
</code>
206 specifier.
</div></div></td>
207 <td><div class=
"exampleContainer expandable">
208 <div class=
"example"><pre>
209 void test() throw() {
212 </pre></div></div></td>
213 <td class=
"aligned"></td></tr>
216 <tr><td><div class=
"namedescr expandable"><span class=
"name">
217 exceptions.ThrownTypeDiffersSpec
</span><span class=
"lang">
218 (C++)
</span><div class=
"descr">
219 The type of a thrown exception differs from those specified in
220 a
<code>throw(
<i>type
</i>)
</code> specifier.
</div></div></td>
221 <td><div class=
"exampleContainer expandable">
222 <div class=
"example"><pre>
225 void test() throw(int) {
229 </pre></div></div></td>
230 <td class=
"aligned"></td></tr>
234 <!-- ========================= smart pointers ============================== -->
235 <h3>smart pointers
</h3>
236 <table class=
"checkers">
237 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
238 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
240 <tr><td><div class=
"namedescr expandable"><span class=
"name">
241 smartptr.SmartPtrInit
</span><span class=
"lang">
242 (C++)
</span><div class=
"descr">
243 C++
03:
<code>auto_ptr
</code> should store a pointer to an object obtained via
244 new as allocated memory will be cleaned using
<code>delete
</code>.
<br>
245 C++
11: one should use
<code>unique_ptr
<<i>type
</i>[]
></code> to keep a
246 pointer to memory allocated by
<code>new[]
</code>.
<br>
247 C++
11: to keep a pointer to memory allocated by
<code>new[]
</code> in
248 a
<code>shared_ptr
</code> one should use a custom deleter that calls
<code>
250 <p>Source: C++
03 20.4.5p1; C++
11 <code>auto_ptr
</code> is deprecated (D
.10).
</p></div></div></td>
251 <td><div class=
"exampleContainer expandable">
252 <div class=
"example"><pre>
253 #include
<stdlib.h
>
254 #include
<memory
>
257 std::auto_ptr
<int
> p1(new int); // Ok
258 std::auto_ptr
<int
> p2(new int[
3]); // warn
261 <div class=
"example"><pre>
262 #include
<stdlib.h
>
263 #include
<memory
>
266 std::auto_ptr
<int
> p((int *)malloc(sizeof(int))); // warn
268 </pre></div></div></td>
269 <td class=
"aligned"></td></tr>
273 <!-- ============================== dead code ============================== -->
275 <table class=
"checkers">
276 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
277 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
279 <tr><td><div class=
"namedescr expandable"><span class=
"name">
280 deadcode.UnmodifiedVariable
</span><span class=
"lang">
281 (C, C++)
</span><div class=
"descr">
282 A variable is never modified but was not declared const and is not a
283 reference.
<br><br><i>(opt-in checker)
</i></div></div></td>
284 <td><div class=
"exampleContainer expandable">
285 <div class=
"example"><pre>
286 extern int computeDelta();
288 int test(bool cond) {
291 const int delta = computeDelta();
292 // warn: forgot to modify 'i'
296 </pre></div></div></td>
297 <td class=
"aligned"><a href=
"https://bugs.llvm.org/show_bug.cgi?id=16890">PR16890
</a></td></tr>
299 <tr><td><div class=
"namedescr expandable"><span class=
"name">
300 deadcode.IdempotentOperations
</span><span class=
"lang">
301 (C)
</span><div class=
"descr">
302 Warn about idempotent operations.
</div></div></td>
303 <td><div class=
"exampleContainer expandable">
304 <div class=
"example"><pre>
307 x = x; // warn: value is always the same
310 <div class=
"example"><pre>
313 x /= x; // warn: value is always
1
316 <div class=
"example"><pre>
319 x *= one; // warn: right op is always
1
322 <div class=
"example"><pre>
326 // warn: the right operand to '-' is always
0
328 </pre></div></div></td>
329 <td class=
"aligned">removed from alpha.deadcode.* at
330 <a href=
"https://reviews.llvm.org/rL198476">r198476
</a></td></tr>
334 <!-- ================================ POSIX ================================ -->
336 <table class=
"checkers">
337 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
338 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
340 <tr><td><div class=
"namedescr expandable"><span class=
"name">
341 posix.Errno
</span><span class=
"lang">
342 (C)
</span><div class=
"descr">
343 Record that
<code>errno
</code> is non-zero when certain functions
344 fail.
</div></div></td>
345 <td><div class=
"exampleContainer expandable">
346 <div class=
"example"><pre>
347 #include
<stdlib.h
>
349 int readWrapper(int fd, int *count) {
350 int lcount = read(fd, globalBuf, sizeof(globalBuf));
359 if (!readWrapper(fd,
&count))
360 print(
"%d", count); // should not warn
362 </pre></div></div></td>
363 <td class=
"aligned"><a href=
"https://bugs.llvm.org/show_bug.cgi?id=18701">PR18701
</a></td></tr>
367 <!-- ========================= undefined behavior ========================== -->
368 <h3>undefined behavior
</h3>
369 <table class=
"checkers">
370 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
371 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
373 <tr><td><div class=
"namedescr expandable"><span class=
"name">
374 undefbehavior.ExitInDtor
</span><span class=
"lang">
375 (C++)
</span><div class=
"descr">
376 Undefined behavior:
<code>std::exit()
</code> is called to end the program during
377 the destruction of an object with static storage duration.
378 <p>Source: C++
11 3.6.1p4.
</p></div></div></td>
379 <td><div class=
"exampleContainer expandable">
380 <div class=
"example"><pre>
381 #include
<cstdlib
>
386 std::exit(
1); // warn
389 </pre></div></div></td>
390 <td class=
"aligned"></td></tr>
393 <tr><td><div class=
"namedescr expandable"><span class=
"name">
394 undefbehavior.LocalStaticDestroyed
</span><span class=
"lang">
395 (C++)
</span><div class=
"descr">
396 Undefined behavior: function containing a definition of static local object is
397 called during the destruction of an object with static storage duration so that
398 flow of control passes through the definition of the previously destroyed
400 <p>Source: C++
11 3.6.3p2.
</p></div></div></td>
401 <td><div class=
"exampleContainer expandable">
402 <div class=
"example"><pre>
419 </pre></div></div></td>
420 <td class=
"aligned"></td></tr>
423 <tr><td><div class=
"namedescr expandable"><span class=
"name">
424 undefbehavior.ZeroAllocDereference
</span><span class=
"lang">
425 (C, C++)
</span><div class=
"descr">
426 The effect of dereferencing a pointer returned as a request for zero size is
428 Note: possibly an enhancement to
<span class=
"name">
430 <p>Source: C++
03 3.7.3.1p2; C++
11 3.7.4.1p2.
</p></div></div></td>
431 <td><div class=
"exampleContainer expandable">
432 <div class=
"example"><pre>
433 #include
<stdlib.h
>
436 int *p = (int *)malloc(
0);
441 <div class=
"example"><pre>
449 </pre></div></div></td>
450 <td class=
"aligned"><a href=
"https://reviews.llvm.org/D8273">D8273
</a></td></tr>
453 <tr><td><div class=
"namedescr expandable"><span class=
"name">
454 undefbehavior.DeadReferenced
</span><span class=
"lang">
455 (C++)
</span><div class=
"descr">
456 Undefined behavior: the following usage of the pointer to the object whose
457 lifetime has ended can result in undefined behavior:
<br>
458 The object will be or was of a class type with a non-trivial destructor and
459 <ul><li>the pointer is used as the operand of a delete-expression
</li></ul>
460 The object will be or was of a non-POD class type (C++
11: any class type) and
461 <ul><li>the pointer is used to access a non-static data member or call a
462 non-static member function of the object
</li>
463 <li>the pointer is implicitly converted to a pointer to a base class
465 <li>the pointer is used as the operand of a
<code>static_cast
</code> (except
466 when the conversion is to
<code>void*
</code>, or to
<code>void*
</code> and
467 subsequently to
<code>char*
</code>, or
<code>unsigned char*
</code>)
</li>
468 <li>the pointer is used as the operand of a
<code>dynamic_cast
</code></li></ul>
469 <p>Source: C++
03 3.8p5, p7; C++
11 3.8p5, p7.
</p></div></div></td>
470 <td><div class=
"exampleContainer expandable">
471 <div class=
"example"><pre>
479 class B : public A {};
487 <div class=
"example"><pre>
503 <div class=
"example"><pre>
511 class B : public A {};
523 <div class=
"example"><pre>
531 class B : public A {};
538 return static_cast
<A*
>(b); // warn
541 <div class=
"example"><pre>
549 class B : public A {};
556 return dynamic_cast
<A*
>(b); // warn
558 </pre></div></div></td>
559 <td class=
"aligned"></td></tr>
562 <tr><td><div class=
"namedescr expandable"><span class=
"name">
563 undefbehavior.ObjLocChanges
</span><span class=
"lang">
564 (C++)
</span><div class=
"descr">
565 Undefined behavior: the program must ensure that an object occupies the same
566 storage location when the implicit or explicit destructor call takes place.
567 <p>Source: C++
11 3.8p8.
</p></div></div></td>
568 <td><div class=
"exampleContainer expandable">
569 <div class=
"example"><pre>
584 <div class=
"example"><pre>
599 </pre></div></div></td>
600 <td class=
"aligned"></td></tr>
603 <tr><td><div class=
"namedescr expandable"><span class=
"name">
604 undefbehavior.ExprEvalOrderUndef
</span><span class=
"lang">
605 (C, C++
03)
</span><div class=
"descr">
606 Undefined behavior: a scalar object shall have its stored value modified at
607 most once by the evaluation of an expression.
<br>
608 Note: most cases are currently handled by the Clang core (search for 'multiple
609 unsequenced modifications' warning in Clang tests).
610 <p>Source: C++
03 5p4.
</p></div></div></td>
611 <td><div class=
"exampleContainer expandable">
612 <div class=
"example"><pre>
618 </pre></div></div></td>
619 <td class=
"aligned"></td></tr>
622 <tr><td><div class=
"namedescr expandable"><span class=
"name">
623 undefbehavior.StaticInitReentered
</span><span class=
"lang">
624 (C++)
</span><div class=
"descr">
625 Undefined behavior: static declaration is re-entered while the object is being
627 <p>Source: C++
11 6.7p4.
</p></div></div></td>
628 <td><div class=
"exampleContainer expandable">
629 <div class=
"example"><pre>
631 static int s = test(
2 * i); // warn
634 </pre></div></div></td>
635 <td class=
"aligned"></td></tr>
638 <tr><td><div class=
"namedescr expandable"><span class=
"name">
639 undefbehavior.ConstModified
</span><span class=
"lang">
640 (C, C++)
</span><div class=
"descr">
641 Undefined behavior: const object is being modified.
642 <p>Source: C++
03 7.1.5.1p4, C++
11 7.1.6.1p4.
</p></div></div></td>
643 <td><div class=
"exampleContainer expandable">
644 <div class=
"example"><pre>
646 const int *cp = new const int (
0);
647 int *p = const_cast
<int *
>(cp);
652 <div class=
"example"><pre>
662 C* cp = const_cast
<C *
>(&cb);
663 cp-
>i =
1; // warn
665 </pre></div></div></td>
666 <td class=
"aligned"></td></tr>
669 <tr><td><div class=
"namedescr expandable"><span class=
"name">
670 undefbehavior.DeadDestructed
</span><span class=
"lang">
671 (C++)
</span><div class=
"descr">
672 Undefined behavior: the destructor is invoked for an object whose lifetime
674 <p>Source: C++
11 12.4p14.
</p></div></div></td>
675 <td><div class=
"exampleContainer expandable">
676 <div class=
"example"><pre>
688 </pre></div></div></td>
689 <td class=
"aligned"></td></tr>
692 <tr><td><div class=
"namedescr expandable"><span class=
"name">
693 undefbehavior.MethodCallBeforeBaseInit
</span><span class=
"lang">
694 (C++)
</span><div class=
"descr">
695 Undefined behavior: calls member function but base not yet initialized.
696 <p>Source: C++
03 12.6.2p8; C++
11 12.6.2p13.
</p></div></div></td>
697 <td><div class=
"exampleContainer expandable">
698 <div class=
"example"><pre>
707 B() : A(f()) {} // warn
709 </pre></div></div></td>
710 <td class=
"aligned"></td></tr>
713 <tr><td><div class=
"namedescr expandable"><span class=
"name">
714 undefbehavior.MemberOrBaseRefBeforeCtor
</span><span class=
"lang">
715 (C++)
</span><div class=
"descr">
716 C++ Undefined behavior: non-static member or base class of non-POD class type
717 is referred before constructor begins execution.
<br>
718 C++
11 Undefined behavior: non-static member or base class of a class with a
719 non-trivial constructor is referred before constructor begins execution.
720 <p>Source: C++
03 12.7p1; C++
11 12.7p1.
</p></div></div></td>
721 <td><div class=
"exampleContainer expandable">
722 <div class=
"example"><pre>
728 extern non_POD non_pod;
730 int *p =
&non_pod.i; // warn
732 <div class=
"example"><pre>
737 struct non_POD : public POD {
741 extern non_POD non_pod;
743 int *p =
&non_pod.pod.i; // warn
745 <div class=
"example"><pre>
750 struct non_POD : public POD {};
752 extern non_POD non_pod;
754 POD *p =
&non_pod; // warn
756 <div class=
"example"><pre>
765 S() : k(
&non_pod.i) {} // warn
767 </pre></div></div></td>
768 <td class=
"aligned"></td></tr>
771 <tr><td><div class=
"namedescr expandable"><span class=
"name">
772 undefbehavior.MemberRefAfterDtor
</span><span class=
"lang">
773 (C++)
</span><div class=
"descr">
774 C++
03: Undefined behavior: non-static member of non-POD class type is referred
775 after destructor ends execution.
<br>
776 C++
11: Undefined behavior: non-static member of a class with a non-trivial
777 destructor is referred after destructor ends execution.
778 <p>Source: C++
03 12.7p1; C++
11 12.7p1.
</p></div></div></td>
779 <td><div class=
"exampleContainer expandable">
780 <div class=
"example"><pre>
792 </pre></div></div></td>
793 <td class=
"aligned"></td></tr>
796 <tr><td><div class=
"namedescr expandable"><span class=
"name">
797 undefbehavior.CtorForeignCall
</span><span class=
"lang">
798 (C++)
</span><div class=
"descr">
799 Undefined behavior: call to virtual function of an object under construction
800 whose type is neither the constructors own class or one of its bases.
801 <p>Source: C++
11 12.7p4.
</p></div></div></td>
802 <td><div class=
"exampleContainer expandable">
803 <div class=
"example"><pre>
811 B(A* a) { a-
>f(); } // warn
814 class C : public A, B {
818 </pre></div></div></td>
819 <td class=
"aligned"></td></tr>
822 <tr><td><div class=
"namedescr expandable"><span class=
"name">
823 undefbehavior.CtorForeignTypeid
</span><span class=
"lang">
824 (C++)
</span><div class=
"descr">
825 Undefined behavior: the operand of
<code>typeid
</code> is an object under
826 construction whose type is neither the constructors own class or one of its
828 <p>Source: C++
11 12.7p5.
</p></div></div></td>
829 <td><div class=
"exampleContainer expandable">
830 <div class=
"example"><pre>
831 #include
<typeinfo
>
838 (void)typeid(*a); // warn
842 class C : public A, B {
846 </pre></div></div></td>
847 <td class=
"aligned"></td></tr>
850 <tr><td><div class=
"namedescr expandable"><span class=
"name">
851 undefbehavior.CtorForeignCast
</span><span class=
"lang">
852 (C++)
</span><div class=
"descr">
853 Undefined behavior: the operand of
<code>dynamic_cast
</code> is an object under
854 construction whose type is neither the constructors own class or one of its
856 <p>Source: C++
11 12.7p6.
</p></div></div></td>
857 <td><div class=
"exampleContainer expandable">
858 <div class=
"example"><pre>
859 #include
<typeinfo
>
869 (void)dynamic_cast
<B*
>(a); //warn
873 class C : public A, B {
877 </pre></div></div></td>
878 <td class=
"aligned"></td></tr>
881 <tr><td><div class=
"namedescr expandable"><span class=
"name">
882 undefbehavior.MemberOrBaseRefInCatch
</span><span class=
"lang">
883 (C++)
</span><div class=
"descr">
884 Undefined behavior: referring to any non-static member or base class of an
885 object in the handler for a function-try-block of a constructor or destructor
886 for that object results in undefined behavior.
887 <p>Source: C++
11 15.3p10.
</p></div></div></td>
888 <td><div class=
"exampleContainer expandable">
889 <div class=
"example"><pre>
890 void f() { throw
1; }
904 <div class=
"example"><pre>
905 void f() { throw
1; }
912 class C: public Base {
921 </pre></div></div></td>
922 <td class=
"aligned"></td></tr>
925 <tr><td><div class=
"namedescr expandable"><span class=
"name">
926 undefbehavior.ReturnAtCatchEnd
</span><span class=
"lang">
927 (C++)
</span><div class=
"descr">
928 Undefined behavior: a function returns when control reaches the end of a
929 handler. This results in undefined behavior in a value-returning function.
930 <p>Source: C++
11 15.3p10.
</p></div></div></td>
931 <td><div class=
"exampleContainer expandable">
932 <div class=
"example"><pre>
933 void f() { throw
1; }
941 </pre></div></div></td>
942 <td class=
"aligned"></td></tr>
945 <tr><td><div class=
"namedescr expandable"><span class=
"name">
946 undefbehavior.AutoptrsOwnSameObj
</span><span class=
"lang">
947 (C++
03)
</span><div class=
"descr">
948 Undefined behavior: if more than one
<code>auto_ptr
</code> owns the same object
949 at the same time the behavior of the program is undefined.
950 <p>Source: C++
03 20.4.5p3; C++
11 <code>auto_ptr
</code> is deprecated
951 (D
.10).
</p></div></div></td>
952 <td><div class=
"exampleContainer expandable">
953 <div class=
"example"><pre>
954 #include
<memory
>
958 std::auto_ptr
<int
> p(data);
959 std::auto_ptr
<int
> q(data); // warn
961 </pre></div></div></td>
962 <td class=
"aligned"></td></tr>
965 <tr><td><div class=
"namedescr expandable"><span class=
"name">
966 undefbehavior.BasicStringOutOfBound
</span><span class=
"lang">
967 (C++
03)
</span><div class=
"descr">
968 Undefined behavior: out-of-bound
<code>basic_string
</code> access/modification.
969 <br>Note: possibly an enhancement to
<span class=
"name">
970 alpha.security.ArrayBoundV2
</span>.
971 <p>Source: C++
03 21.3.4p1; C++
11 behavior is defined
972 (
21.4.5p2).
</p></div></div></td>
973 <td><div class=
"exampleContainer expandable">
974 <div class=
"example"><pre>
975 #include
<string
>
978 std::basic_string
<char
> s;
979 char c = s[
10]; // warn
982 <div class=
"example"><pre>
983 #include
<string
>
986 std::basic_string
<char
> s;
989 </pre></div></div></td>
990 <td class=
"aligned"></td></tr>
993 <tr><td><div class=
"namedescr expandable"><span class=
"name">
994 undefbehavior.EosDereference
</span><span class=
"lang">
995 (C++)
</span><div class=
"descr">
996 Undefined behavior: the result of
<code>operator*()
</code> on an end of a
998 <p>Source: C++
03 24.5.3p2; C++
11 24.6.3p2.
</p></div></div></td>
999 <td><div class=
"exampleContainer expandable">
1000 <div class=
"example"><pre>
1001 #include
<vector
>
1004 std::vector
<int
> v;
1005 return *v.end(); // warn
1007 </pre></div></div></td>
1008 <td class=
"aligned"></td></tr>
1011 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1012 undefbehavior.QsortNonPODNonTrivial
</span><span class=
"lang">
1013 (C++)
</span><div class=
"descr">
1014 C++
03: Undefined behavior: the objects in the array passed to qsort are of
1016 C++
11: Undefined behavior: the objects in the array passed to qsort are of
1018 <p>Source: C++
03 25.4p4; C++
11 25.5p4.
</p></div></div></td>
1019 <td><div class=
"exampleContainer expandable">
1020 <div class=
"example"><pre>
1022 #include
<cstdlib
>
1029 non_POD values[] = { non_POD(), non_POD() };
1031 int compare(const void *a, const void *b);
1034 qsort(values,
2, sizeof(non_POD), compare); // warn
1037 <div class=
"example"><pre>
1039 #include
<cstdlib
>
1043 struct trivial_non_POD : public S {
1047 struct non_trivial {
1052 trivial_non_POD tnp[
2];
1055 int compare1(const void *a, const void *b);
1057 int compare2(const void *a, const void *b);
1060 qsort(tnp,
2, sizeof(trivial_non_POD), compare1); // ok
1061 qsort(nt,
2, sizeof(non_trivial), compare2); // warn
1063 </pre></div></div></td>
1064 <td class=
"aligned"></td></tr>
1067 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1068 undefbehavior.ThrowWhileCopy
</span><span class=
"lang">
1069 (C++)
</span><div class=
"descr">
1070 Undefined behavior: copy constructor/assignment operator can throw an exception.
1071 The effects are undefined if an exception is thrown.
</div></div></td>
1072 <td><div class=
"exampleContainer expandable">
1073 <div class=
"example"><pre>
1077 C (const C
&c) {
1084 <div class=
"example"><pre>
1088 C
&operator=(const C
&c) {
1094 </pre></div></div></td>
1095 <td class=
"aligned"></td></tr>
1098 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1099 undefbehavior.ValarrayArgBound
</span><span class=
"lang">
1100 (C++)
</span><div class=
"descr">
1101 Undefined behavior: the value of the
<code><i>n
</i></code> argument passed
1102 to
<code>valarray
</code> constructor is greater than the number of values
1103 pointed to by the first argument (source).
1104 <p>Source: C++
03 26.3.2.1p4; C++
11 26.6.2.2p4.
</p></div></div></td>
1105 <td><div class=
"exampleContainer expandable">
1106 <div class=
"example"><pre>
1107 #include
<valarray
>
1111 S(int ii) : i(ii) {};
1115 S s[] = { S(
1), S(
2) };
1116 std::valarray
<S
> v(s,
3); // warn
1118 </pre></div></div></td>
1119 <td class=
"aligned"></td></tr>
1122 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1123 undefbehavior.ValarrayLengthDiffer
</span><span class=
"lang">
1124 (C++)
</span><div class=
"descr">
1125 Undefined behavior:
<code>valarray
</code> operands are of different length.
1126 <p>Source: C++
03 26.3.2.2p1,
26.3.2.6p3,
26.3.3.1p3,
26.3.3.2p3;
1127 C++
11 defined (
26.6.2.3p1),
26.6.2.7p3,
26.6.3.1p3,
1128 26.6.3.2p3.
</p></div></div></td>
1129 <td><div class=
"exampleContainer expandable">
1130 <div class=
"example"><pre>
1132 #include
<valarray
>
1135 std::valarray
<int
> a(
0,
1), b(
0,
2);
1141 <div class=
"example"><pre>
1143 #include
<valarray
>
1146 std::valarray
<int
> a(
0,
1), b(
0,
2);
1150 <div class=
"example"><pre>
1152 #include
<valarray
>
1155 std::valarray
<int
> a(
0,
1), b(
0,
2);
1159 <div class=
"example"><pre>
1161 #include
<valarray
>
1164 std::valarray
<int
> a(
0,
1), b(
0,
2);
1165 std::valarray
<bool
> c(false,
1);
1168 </pre></div></div></td>
1169 <td class=
"aligned"></td></tr>
1172 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1173 undefbehavior.ValarrayZeroLength
</span><span class=
"lang">
1174 (C++)
</span><div class=
"descr">
1175 Undefined behavior: calling
<code>sum()
</code>/
<code>min()
</code>/
<code>
1176 max()
</code> methods of a zero length
<code>valarray
<code> the behavior is
1178 <p>Source: C++
03 26.3.2.7p2, p3, p4; C++
11 26.6.2.8p5, p6,
1179 p7.
</p></div></div></td>
1180 <td><div class=
"exampleContainer expandable">
1181 <div class=
"example"><pre>
1182 #include
<valarray
>
1185 std::valarray
<int
> v(
0,
0);
1188 </pre></div></div></td>
1189 <td class=
"aligned"></td></tr>
1192 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1193 undefbehavior.ValarrayBadIndirection
</span><span class=
"lang">
1194 (C++)
</span><div class=
"descr">
1195 Undefined behavior: element is specified more than once in an indirection.
1196 <p>Source: C++
03 26.3.9.2p2,
26.3.9.3p2; C++
11 26.6.9.2p2,
1197 26.6.9.3p2.
</p></div></div></td>
1198 <td><div class=
"exampleContainer expandable">
1199 <div class=
"example"><pre>
1200 #include
<valarray
>
1203 // '
1' is specified more then once
1204 size_t addr[] = {
0,
1,
1};
1205 std::valarray
<size_t
>indirect(addr,
3);
1206 std::valarray
<int
> a(
0,
5), b(
1,
3);
1207 a[indirect] = b; //warn
1210 <div class=
"example"><pre>
1211 #include
<valarray
>
1214 // '
1' is specified more then once
1215 size_t addr[] = {
0,
1,
1};
1216 std::valarray
<size_t
>indirect(addr,
3);
1217 std::valarray
<int
> a(
0,
5), b(
1,
3);
1218 a[indirect] *= b; //warn
1220 </pre></div></div></td>
1221 <td class=
"aligned"></td></tr>
1224 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1225 undefbehavior.IosBaseDestroyedBeforeInit
</span><span class=
"lang">
1226 (C++)
</span><div class=
"descr">
1227 Undefined behavior:
<code>ios_base
</code> object is destroyed before
1228 initialization have taken place.
<code>basic_ios::init
</code> should be call to
1229 initialize
<code>ios_base
</code> members.
1230 <p>Source: C++
03 27.4.2.7p1,
27.4.4.1p2; C++
11 27.5.3.7p1,
1231 27.5.5.2p2.
</p></div></div></td>
1232 <td><div class=
"exampleContainer expandable">
1233 <div class=
"example"><pre>
1234 #include
<ios
>
1236 using namespace std;
1237 template
<class T, class Traits = std::char_traits
<T
> >
1238 class my_stream1 : public std::basic_ios
<T, Traits
> {
1241 template
<class T, class Traits = std::char_traits
<T
> >
1242 class my_stream2 : public std::basic_ios
<T, Traits
> {
1244 : public std::basic_streambuf
<T, Traits
> {
1248 this-
>init(new my_streambuf);
1253 my_stream1
<char
> *p1 = new my_stream1
<char
>;
1254 my_stream2
<char
> *p2 = new my_stream2
<char
>;
1258 </pre></div></div></td>
1259 <td class=
"aligned"></td></tr>
1262 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1263 undefbehavior.IosBaseUsedBeforeInit
</span><span class=
"lang">
1264 (C++
11)
</span><div class=
"descr">
1265 Undefined behavior:
<code>ios_base
</code> object is used before initialization
1266 have taken place.
<code>basic_ios::init
</code> should be call to
1267 initialize
<code>ios_base
</code> members.
1268 <p>Source: C++
11 27.5.3.7p1,
27.5.5.2p2.
</p></div></div></td>
1269 <td><div class=
"exampleContainer expandable">
1270 <div class=
"example"><pre>
1271 #include
<ios
>
1273 using namespace std;
1274 template
<class T, class Traits = std::char_traits
<T
> >
1275 class my_stream1 : public std::basic_ios
<T, Traits
> {
1278 template
<class T, class Traits = std::char_traits
<T
> >
1279 class my_stream2 : public std::basic_ios
<T, Traits
> {
1281 : public std::basic_streambuf
<T, Traits
> {
1285 this-
>init(new my_streambuf);
1290 my_stream1
<char
> *p1 = new my_stream1
<char
>;
1291 my_stream2
<char
> *p2 = new my_stream2
<char
>;
1292 p1-
>narrow('a', 'b'); // warn
1293 p2-
>narrow('a', 'b'); // ok
1295 </pre></div></div></td>
1296 <td class=
"aligned"></td></tr>
1299 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1300 undefbehavior.MinusOnePosType
</span><span class=
"lang">
1301 (C++)
</span><div class=
"descr">
1302 Undefined behavior: passing -
1 to any
<code>streambuf
</code>/
<code>
1303 istream
</code>/
<code>ostream
</code> member that accepts a value of
1304 type
<code>traits::pos_type
</code> result in undefined behavior.
1305 <p>Source: C++
03 27.4.3.2p3; C++
11 27.5.4.2p3.
</p></div></div></td>
1306 <td><div class=
"exampleContainer expandable">
1307 <div class=
"example"><pre>
1308 #include
<fstream
>
1310 class my_streambuf : public std::streambuf {
1312 seekpos(-
1); // warn
1316 <div class=
"example"><pre>
1317 #include
<fstream
>
1321 std::istream in(
&fb);
1322 std::filebuf::off_type pos(-
1);
1323 in.seekg(pos); // warn
1325 </pre></div></div></td>
1326 <td class=
"aligned"></td></tr>
1330 <!-- ============================ different ================================ -->
1332 <table class=
"checkers">
1333 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
1334 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr>
1337 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1338 different.SuccessiveAssign
</span><span class=
"lang">
1339 (C)
</span><div class=
"descr">
1340 Successive assign to a variable.
</div></div></td>
1341 <td><div class=
"exampleContainer expandable">
1342 <div class=
"example"><pre>
1349 </pre></div></div></td>
1350 <td class=
"aligned"></td></tr>
1353 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1354 different.NullDerefStmtOrder
</span><span class=
"lang">
1355 (C)
</span><div class=
"descr">
1356 Dereferencing of the null pointer might take place. Checking the pointer for
1357 null should be performed first.
1358 <br>Note: possibly an enhancement to
<span class=
"name">
1359 core.NullDereference
</span>.
</div></div></td>
1360 <td><div class=
"exampleContainer expandable">
1361 <div class=
"example"><pre>
1370 int x1 = p1-
>x; // warn
1374 int x2 = p2-
>x; // ok
1376 </pre></div></div></td>
1377 <td class=
"aligned"></td></tr>
1380 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1381 different.NullDerefCondOrder
</span><span class=
"lang">
1382 (C)
</span><div class=
"descr">
1383 Dereferencing of the null pointer might take place. Checking the pointer for
1384 null should be performed first.
1385 <br>Note: possibly an enhancement to
<span class=
"name">
1386 core.NullDereference
</span>.
</div></div></td>
1387 <td><div class=
"exampleContainer expandable">
1388 <div class=
"example"><pre>
1395 if (p-
>i && p) {}; // warn
1397 </pre></div></div></td>
1398 <td class=
"aligned"></td></tr>
1401 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1402 different.MultipleAccessors
</span><span class=
"lang">
1403 (C++)
</span><div class=
"descr">
1404 Identical accessor bodies. Possibly a misprint.
</div></div></td>
1405 <td><div class=
"exampleContainer expandable">
1406 <div class=
"example"><pre>
1411 int getI() { return i; }
1412 int getJ() { return i; } // warn
1415 <div class=
"example"><pre>
1420 void setI(int& ii) { i = ii; }
1421 void setJ(int& jj) { i = jj; } // warn
1423 </pre></div></div></td>
1424 <td class=
"aligned"></td></tr>
1427 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1428 different.AccessorsForPublic
</span><span class=
"lang">
1429 (C++)
</span><div class=
"descr">
1430 Accessors exist for a public class field. Should this field really be
1431 public?
</div></div></td>
1432 <td><div class=
"exampleContainer expandable">
1433 <div class=
"example"><pre>
1437 int getI() { return i; }
1438 void setI(int& ii) { i = ii; }
1440 </pre></div></div></td>
1441 <td class=
"aligned"></td></tr>
1444 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1445 different.LibFuncResultUnised
</span><span class=
"lang">
1446 (C, C++)
</span><div class=
"descr">
1447 Calling a function ignoring its return value is of no use (create the list of
1448 known system/library/API functions falling into this category).
</div></div></td>
1449 <td><div class=
"exampleContainer expandable">
1450 <div class=
"example"><pre>
1451 #include
<vector
>
1454 std::vector
<int
> v;
1457 </pre></div></div></td>
1458 <td class=
"aligned"></td></tr>
1461 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1462 different.WrongVarForStmt
</span><span class=
"lang">
1463 (C, C++)
</span><div class=
"descr">
1464 Wrong variable is possibly used in the loop/cond-expression of
1465 the
<code>for
</code> statement. Did you mean
1466 'proper_variable_name'?
</div></div></td>
1467 <td><div class=
"exampleContainer expandable">
1468 <div class=
"example"><pre>
1472 for (i =
0; i <
3; j +=
1); // warn
1475 <div class=
"example"><pre>
1479 for (int j =
0; i <
3; ++j); // warn
1481 </pre></div></div></td>
1482 <td class=
"aligned"></td></tr>
1485 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1486 different.FloatingCompare
</span><span class=
"lang">
1487 (C)
</span><div class=
"descr">
1488 Comparing floating point numbers may be not precise.
</div></div></td>
1489 <td><div class=
"exampleContainer expandable">
1490 <div class=
"example"><pre>
1491 #include
<math.h
>
1494 double b = sin(M_PI /
6.0);
1495 if (b ==
0.5) // warn
1499 </pre></div></div></td>
1500 <td class=
"aligned"></td></tr>
1503 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1504 different.BitwiseOpBoolArg
</span><span class=
"lang">
1505 (C, C++)
</span><div class=
"descr">
1506 Boolean value met at the left/right part of the bitwise
<code>&</code>
1507 or
<code>|
</code> operator.
1508 Did you mean
<code>&&</code> (
<code>||
</code>) ?
</div></div></td>
1509 <td><div class=
"exampleContainer expandable">
1510 <div class=
"example"><pre>
1515 if (b
& f()) {} // warn
1517 </pre></div></div></td>
1518 <td class=
"aligned"></td></tr>
1521 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1522 different.LabelInsideSwitch
</span><span class=
"lang">
1523 (C)
</span><div class=
"descr">
1524 Possibly a misprint: label found inside a
<code>switch()
</code>
1525 statement.
</div></div></td>
1526 <td><div class=
"exampleContainer expandable">
1527 <div class=
"example"><pre>
1532 defalt: // warn (did you mean 'default'?)
1536 </pre></div></div></td>
1537 <td class=
"aligned"></td></tr>
1540 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1541 different.IdenticalCondIfIf
</span><span class=
"lang">
1542 (C)
</span><div class=
"descr">
1543 The conditions of two subsequent
<code>if
</code> statements are
1544 identical.
</div></div></td>
1545 <td><div class=
"exampleContainer expandable">
1546 <div class=
"example"><pre>
1550 if (c
> 5) // warn
1554 </pre></div></div></td>
1555 <td class=
"aligned"></td></tr>
1558 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1559 different.LogicalOpUselessArg
</span><span class=
"lang">
1560 (C)
</span><div class=
"descr">
1561 The second operand of a
<code>&&</code> operator has no impact on
1562 expression result.
</div></div></td>
1563 <td><div class=
"exampleContainer expandable">
1564 <div class=
"example"><pre>
1565 void test(unsigned a) {
1566 if (a
<7 && a
<10) {}; // warn
1568 </pre></div></div></td>
1569 <td class=
"aligned"></td></tr>
1572 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1573 different.SameResLogicalExpr
</span><span class=
"lang">
1574 (C)
</span><div class=
"descr">
1575 An expression is always evaluated to true/false.
</div></div></td>
1576 <td><div class=
"exampleContainer expandable">
1577 <div class=
"example"><pre>
1580 if (i !=
0) {}; // warn
1583 <div class=
"example"><pre>
1585 if (i ==
0 && i ==
1) {}; // warn
1588 <div class=
"example"><pre>
1590 if (i <
0 || i
>=
0) {}; // warn
1592 </pre></div></div></td>
1593 <td class=
"aligned"></td></tr>
1596 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1597 different.OpPrecedenceAssignCmp
</span><span class=
"lang">
1598 (C, C++)
</span><div class=
"descr">
1599 Comparison operation has higher precedence then assignment. Boolean value is
1600 assigned to a variable of other type. Parenthesis may bee required around an
1601 assignment.
</div></div></td>
1602 <td><div class=
"exampleContainer expandable">
1603 <div class=
"example"><pre>
1606 void test(int x, int y) {
1608 if((b = x != y)) {} // ok
1609 if((x = f() != y)) {} // warn
1611 </pre></div></div></td>
1612 <td class=
"aligned"></td></tr>
1615 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1616 different.OpPrecedenceIifShift
</span><span class=
"lang">
1617 (C, C++)
</span><div class=
"descr">
1618 <code>?:
</code> has lower precedence then
<code><<</code>.
1619 <p>Source: Stephen C. Dewhurst
"C++ Gotchas: Avoiding Common Problems in Coding
1620 and Design", advise
15.
</p></div></div></td>
1621 <td><div class=
"exampleContainer expandable">
1622 <div class=
"example"><pre>
1623 #include
<iostream
>
1626 std::cout
<< a ?
"a" :
"b"; // warn
1629 <div class=
"example"><pre>
1631 a
<< a
> 7 ?
1 :
2; // warn
1633 </pre></div></div></td>
1634 <td class=
"aligned"></td></tr>
1637 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1638 different.ObjectUnused
</span><span class=
"lang">
1639 (C++)
</span><div class=
"descr">
1640 The object was created but is not being used.
</div></div></td>
1641 <td><div class=
"exampleContainer expandable">
1642 <div class=
"example"><pre>
1645 S(int xx, int yy) : x(xx), y(yy) {}
1651 <div class=
"example"><pre>
1652 #include
<exception
>
1656 // warn (did you mean 'throw std::exception()'?)
1658 </pre></div></div></td>
1659 <td class=
"aligned"></td></tr>
1662 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1663 different.StaticArrayPtrCompare
</span><span class=
"lang">
1664 (C)
</span><div class=
"descr">
1665 Pointer to static array is being compared to NULL. May the subscripting is
1666 missing.
</div></div></td>
1667 <td><div class=
"exampleContainer expandable">
1668 <div class=
"example"><pre>
1671 if (a[
0] ==
0) {}; // warn
1673 </pre></div></div></td>
1674 <td class=
"aligned"></td></tr>
1677 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1678 different.ConversionToBool
</span><span class=
"lang">
1679 (C, C++)
</span><div class=
"descr">
1680 Odd implicit conversion to boolean.
1681 <br>Note: possibly merge with
<span class=
"name">
1682 alpha.core.BoolAssignment
</span>.
</div></div></td>
1683 <td><div class=
"exampleContainer expandable">
1684 <div class=
"example"><pre>
1689 <div class=
"example"><pre>
1693 </pre></div></div></td>
1694 <td class=
"aligned"></td></tr>
1697 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1698 different.ArrayBound
</span><span class=
"lang">
1699 (C++)
</span><div class=
"descr">
1700 Out-of-bound dynamic array access.
1701 <br>Note: possibly an enhancement to
<span class=
"name">
1702 alpha.security.ArrayBoundV2
</span>.
</div></div></td>
1703 <td><div class=
"exampleContainer expandable">
1704 <div class=
"example"><pre>
1706 int *p = new int[
1];
1708 if(p[i]) {}; // warn
1711 </pre></div></div></td>
1712 <td class=
"aligned"></td></tr>
1715 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1716 different.StrcpyInputSize
</span><span class=
"lang">
1717 (C)
</span><div class=
"descr">
1718 Buffer copy without checking the size of input.
1719 <br>Note: possibly an enhancement to
<span class=
"name">
1720 alpha.unix.cstring.OutOfBounds
</span>.
</div></div></td>
1721 <td><div class=
"exampleContainer expandable">
1722 <div class=
"example"><pre>
1723 void test(char* string) {
1725 strcpy(buf, string); // warn
1727 </pre></div></div></td>
1728 <td class=
"aligned"></td></tr>
1731 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1732 different.IntegerOverflow
</span><span class=
"lang">
1733 (C)
</span><div class=
"descr">
1735 <br>Note: partially handled by Clang core
1736 (search for 'overflow in expression' warning in Clang tests).
1737 <p>Source:
<a href=
"https://cwe.mitre.org/data/definitions/190.html">
1738 CWE-
190</a>.
</p></div></div></td>
1739 <td><div class=
"exampleContainer expandable">
1740 <div class=
"example"><pre>
1741 #include
<limits.h
>
1746 f(INT_MAX +
1); // warn
1749 <div class=
"example"><pre>
1750 #include
<limits.h
>
1753 int x = INT_MAX /
2 +
1;
1754 return x *
2; // warn
1756 </pre></div></div></td>
1757 <td class=
"aligned"></td></tr>
1760 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1761 different.SignExtension
</span><span class=
"lang">
1762 (C)
</span><div class=
"descr">
1763 Unexpected sign extension might take place.
1764 <p>Source:
<a href=
"https://cwe.mitre.org/data/definitions/194.html">
1765 CWE-
194</a>.
</p></div></div></td>
1766 <td><div class=
"exampleContainer expandable">
1767 <div class=
"example"><pre>
1768 unsigned long long test(long long sll) {
1769 unsigned long long ull = sll; // warn
1773 <div class=
"example"><pre>
1774 void f(unsigned int i);
1780 <div class=
"example"><pre>
1781 unsigned int test(int i) {
1784 </pre></div></div></td>
1785 <td class=
"aligned"></td></tr>
1788 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1789 different.NumericTruncation
</span><span class=
"lang">
1790 (C)
</span><div class=
"descr">
1791 Numeric truncation might take place.
1792 <p>Source:
<a href=
"https://cwe.mitre.org/data/definitions/197.html">
1793 CWE-
197</a>.
</p></div></div></td>
1794 <td><div class=
"exampleContainer expandable">
1795 <div class=
"example"><pre>
1796 unsigned long test(unsigned long long ull) {
1797 unsigned long ul = ull; // warn
1801 <div class=
"example"><pre>
1804 void test(long long sll) {
1808 <div class=
"example"><pre>
1811 short test(long long sll) {
1815 </pre></div></div></td>
1816 <td class=
"aligned"></td></tr>
1819 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1820 different.MissingCopyCtorAssignOp
</span><span class=
"lang">
1821 (C++)
</span><div class=
"descr">
1822 A class has dynamically allocated data members but do not define a copy
1823 constructor/assignment operator.
1824 <p>Source: Scott Meyers
"Effective C++", item
11: Prevent exceptions from
1825 leaving destructors.
</p></div></div></td>
1826 <td><div class=
"exampleContainer expandable">
1827 <div class=
"example"><pre>
1831 C() { p = new int; }
1834 </pre></div></div></td>
1835 <td class=
"aligned"></td></tr>
1839 <!-- ============================ WinAPI =================================== -->
1841 <table class=
"checkers">
1842 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
1843 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
1845 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1846 WinAPI.CreateProcess
</span><span class=
"lang">
1847 (C)
</span><div class=
"descr">
1848 <code>CreateProcess()
</code>: if the first parameter
<code><i>
1849 lpApplicationName
</i></code> is NULL then the executable name must be in the
1850 white space-delimited string pointed to by
<code><i>lpCommandLine
</code></i>.
1851 If the executable or path name has a space in it, there is a risk that a
1852 different executable could be run because of the way the function parses
1854 <p>Source:
<a href=
"https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#security-remarks">
1855 MSDN: CreateProcess function, Security Remarks
</a>.
</p></div></div></td>
1856 <td><div class=
"exampleContainer expandable">
1857 <div class=
"example"><pre>
1858 #include
<windows.h
>
1862 PROCESS_INFORMATION pi;
1863 CreateProcess(NULL, TEXT(
"C:\\Program Files\\App -L -S"),
1864 NULL, NULL, TRUE,
0, NULL, NULL, &si, &pi);
1867 </pre></div></div></td>
1868 <td class=
"aligned"></td></tr>
1871 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1872 WinAPI.LoadLibrary
</span><span class=
"lang">
1873 (C)
</span><div class=
"descr">
1874 The
<code>SearchPath()
</code> function is used to retrieve a path to a DLL for
1875 a subsequent
<code>LoadLibrary()
</code> call.
1876 <p>Source:
<a href=
"https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya#security-remarks">
1877 MSDN: LoadLibrary function, Security Remarks
</a>.
</p></div></div></td>
1878 <td><div class=
"exampleContainer expandable">
1879 <div class=
"example"><pre>
1880 #include
<windows.h
>
1884 SearchPath(NULL,
"file.dll", NULL,
100, filePath, NULL);
1885 return LoadLibrary(filePath); // warn
1887 </pre></div></div></td>
1888 <td class=
"aligned"></td></tr>
1891 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1892 WinAPI.WideCharToMultiByte
</span><span class=
"lang">
1893 (C)
</span><div class=
"descr">
1894 Buffer overrun while calling
<code>WideCharToMultiByte()
</code>. The size of
1895 the input buffer equals the number of characters in the Unicode string, while
1896 the size of the output buffer equals the number of bytes.
1897 <p>Source:
<a href=
"https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte">
1898 MSDN: WideCharToMultiByte function
</a>.
</p></div></div></td>
1899 <td><div class=
"exampleContainer expandable">
1900 <div class=
"example"><pre>
1901 #include
<windows.h
>
1904 wchar_t ws[] = L
"abc";
1906 WideCharToMultiByte(CP_UTF8,
0, ws, -
1, s,
1907 3, NULL, NULL); // warn
1909 </pre></div></div></td>
1910 <td class=
"aligned"></td></tr>
1915 <!-- =========================== optimization ============================== -->
1916 <h3>optimization
</h3>
1917 <table class=
"checkers">
1918 <col class=
"namedescr"><col class=
"example"><col class=
"progress">
1919 <thead><tr><td>Name, Description
</td><td>Example
</td><td>Progress
</td></tr></thead>
1921 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1922 optimization.PassConstObjByValue
</span><span class=
"lang">
1923 (C, C++)
</span><div class=
"descr">
1924 Optimization: It is more effective to pass constant parameter by reference to
1925 avoid unnecessary object copying.
</div></div></td>
1926 <td><div class=
"exampleContainer expandable">
1927 <div class=
"example"><pre>
1930 void f(const struct A a); // warn
1931 </pre></div></div></td>
1932 <td class=
"aligned"></td></tr>
1935 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1936 optimization.PostfixIncIter
</span><span class=
"lang">
1937 (C++)
</span><div class=
"descr">
1938 Optimization: It is more effective to use prefix increment operator with
1940 <p>Source: Scott Meyers
"More Effective C++", item
6:
1941 Distinguish between prefix and postfix forms of increment and decrement
1942 operators.
</p></div></div></td>
1943 <td><div class=
"exampleContainer expandable">
1944 <div class=
"example"><pre>
1945 #include
<vector
>
1948 std::vector
<int
> v;
1949 std::vector
<int
>::const_iterator it;
1951 it != v.end(); it++) {}; // warn
1953 </pre></div></div></td>
1954 <td class=
"aligned"></td></tr>
1957 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1958 optimization.MultipleCallsStrlen
</span><span class=
"lang">
1959 (C)
</span><div class=
"descr">
1960 Optimization: multiple calls to
<code>strlen()
</code> for a string in an
1961 expression. It is more effective to hold a value returned
1962 from
<code>strlen()
</code> in a temporary variable.
</div></div></td>
1963 <td><div class=
"exampleContainer expandable">
1964 <div class=
"example"><pre>
1965 #include
<string.h
>
1967 void test(const char* s) {
1968 if (strlen(s)
> 0 &&
1969 strlen(s)
< 7) {}; // warn
1971 </pre></div></div></td>
1972 <td class=
"aligned"></td></tr>
1975 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1976 optimization.StrLengthCalculation
</span><span class=
"lang">
1977 (C++)
</span><div class=
"descr">
1978 Optimization: it is more efficient to use
<code>string::length()
</code> to
1979 calculate the length of an
<code>std::string
</code>.
</div></div></td>
1980 <td><div class=
"exampleContainer expandable">
1981 <div class=
"example"><pre>
1982 #include
<string
>
1983 #include
<string.h
>
1987 if (strlen(s.c_str()) !=
0) {}; // warn
1989 </pre></div></div></td>
1990 <td class=
"aligned"></td></tr>
1993 <tr><td><div class=
"namedescr expandable"><span class=
"name">
1994 optimization.EmptyContainerDetect
</span><span class=
"lang">
1995 (C++)
</span><div class=
"descr">
1996 Optimization: It is more efficient to use containers
<code>empty()
</code>
1997 method to identify an empty container.
</div></div></td>
1998 <td><div class=
"exampleContainer expandable">
1999 <div class=
"example"><pre>
2000 #include
<list
>
2003 std::list
<int
> l;
2004 if (l.size() !=
0) {}; // warn
2006 </pre></div></div></td>
2007 <td class=
"aligned"></td></tr>
2013 </div> <!-- page -->
2014 </div> <!-- content -->