xref: /openbsd-src/gnu/llvm/clang/www/analyzer/potential_checkers.html (revision e5dd70708596ae51455a0ffa086a00c5b29f8583)
1*e5dd7070Spatrick<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2*e5dd7070Spatrick          "http://www.w3.org/TR/html4/strict.dtd">
3*e5dd7070Spatrick<html>
4*e5dd7070Spatrick<head>
5*e5dd7070Spatrick  <title>List of potential checkers</title>
6*e5dd7070Spatrick  <link type="text/css" rel="stylesheet" href="content.css">
7*e5dd7070Spatrick  <link type="text/css" rel="stylesheet" href="menu.css">
8*e5dd7070Spatrick  <script type="text/javascript" src="scripts/expandcollapse.js"></script>
9*e5dd7070Spatrick  <script type="text/javascript" src="scripts/menu.js"></script>
10*e5dd7070Spatrick</head>
11*e5dd7070Spatrick<body onload="initExpandCollapse()">
12*e5dd7070Spatrick
13*e5dd7070Spatrick<div id="page">
14*e5dd7070Spatrick
15*e5dd7070Spatrick<!-- menu -->
16*e5dd7070Spatrick<!--#include virtual="menu.html.incl"-->
17*e5dd7070Spatrick<!-- page content -->
18*e5dd7070Spatrick<div id="content">
19*e5dd7070Spatrick<h1>List of potential checkers</h1>
20*e5dd7070Spatrick
21*e5dd7070Spatrick<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>
22*e5dd7070Spatrick
23*e5dd7070Spatrick<!-- ========================= allocation/deallocation ======================= -->
24*e5dd7070Spatrick<h3>memory</h3>
25*e5dd7070Spatrick<table class="checkers">
26*e5dd7070Spatrick<col class="namedescr"><col class="example"><col class="progress">
27*e5dd7070Spatrick<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>
28*e5dd7070Spatrick
29*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
30*e5dd7070Spatrickmemory.LeakEvalOrder</span><span class="lang">
31*e5dd7070Spatrick(C, C++)</span><div class="descr">
32*e5dd7070SpatrickPotential memory leaks caused by an undefined argument evaluation order.
33*e5dd7070Spatrick<p>Source: <a href="https://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm#BestPractices">
34*e5dd7070Spatrickboost docs: shared_ptr</a>.</p></div></div></td>
35*e5dd7070Spatrick<td><div class="exampleContainer expandable">
36*e5dd7070Spatrick<div class="example"><pre>
37*e5dd7070Spatrickvoid f(int, int);
38*e5dd7070Spatrickint g(void *);
39*e5dd7070Spatrickint h() __attribute__((noreturn));
40*e5dd7070Spatrick
41*e5dd7070Spatrickvoid test() {
42*e5dd7070Spatrick  // It is possible that 'malloc(1)' is called first,
43*e5dd7070Spatrick  // then 'h()', that is (or calls) noreturn and eventually
44*e5dd7070Spatrick  // 'g()' is never called.
45*e5dd7070Spatrick  f(g(malloc(1)), h()); // warn: 'g()' may never be called.
46*e5dd7070Spatrick}
47*e5dd7070Spatrick</pre></div>
48*e5dd7070Spatrick<div class="example"><pre>
49*e5dd7070Spatrickvoid f(int, int);
50*e5dd7070Spatrickint g(int *);
51*e5dd7070Spatrickint h() { throw 1; };
52*e5dd7070Spatrick
53*e5dd7070Spatrickvoid test() {
54*e5dd7070Spatrick  // It is possible that 'new int' is called first,
55*e5dd7070Spatrick  // then 'h()', that throws an exception and eventually
56*e5dd7070Spatrick  // 'g()' is never called.
57*e5dd7070Spatrick  f(g(new int), h()); // warn: 'g()' may never be called.
58*e5dd7070Spatrick}
59*e5dd7070Spatrick</pre></div></div></td>
60*e5dd7070Spatrick<td class="aligned"></td></tr>
61*e5dd7070Spatrick
62*e5dd7070Spatrick
63*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
64*e5dd7070Spatrickmemory.DstBufferTooSmall</span><span class="lang">
65*e5dd7070Spatrick(C, C++)</span><div class="descr">
66*e5dd7070SpatrickDestination buffer passed to memory function is too small.
67*e5dd7070Spatrick<br>Note: <span class="name">security.insecureAPI.strcpy</span> currently warns
68*e5dd7070Spatrickon usage of <code>strcpy</code> and suggests to replace it.
69*e5dd7070Spatrick<br>Note: <span class="name">alpha.unix.CStringChecker</span> contains some similar checks.
70*e5dd7070Spatrick<p>Source: <a href="https://cwe.mitre.org/data/definitions/120.html">CWE-120</a>.</p></div></div></td>
71*e5dd7070Spatrick<td><div class="exampleContainer expandable">
72*e5dd7070Spatrick<div class="example"><pre>
73*e5dd7070Spatrickvoid test() {
74*e5dd7070Spatrick  const char* s1 = "abc";
75*e5dd7070Spatrick  char *s2 = new char;
76*e5dd7070Spatrick  strcpy(s2, s1); // warn
77*e5dd7070Spatrick}
78*e5dd7070Spatrick</pre></div>
79*e5dd7070Spatrick<div class="example"><pre>
80*e5dd7070Spatrickvoid test() {
81*e5dd7070Spatrick  int* p1 = new int[3];
82*e5dd7070Spatrick  int* p2 = new int;
83*e5dd7070Spatrick  memcpy(p2, p1, 3); // warn
84*e5dd7070Spatrick}
85*e5dd7070Spatrick</pre></div></div></td>
86*e5dd7070Spatrick<td class="aligned"></td></tr>
87*e5dd7070Spatrick
88*e5dd7070Spatrick
89*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
90*e5dd7070Spatrickmemory.NegativeArraySize</span><span class="lang">
91*e5dd7070Spatrick(C, C++)</span><div class="descr">
92*e5dd7070Spatrick'n' is used to specify the buffer size may be negative.
93*e5dd7070Spatrick<br>Note: possibly an enhancement to <span class="name">
94*e5dd7070Spatrickalpha.security.MallocOverflow</span>.
95*e5dd7070Spatrick<p>Source: <a href="https://cwe.mitre.org/data/definitions/20.html">CWE-20,
96*e5dd7070SpatrickExample 2</a>.</p></div></div></td>
97*e5dd7070Spatrick<td><div class="exampleContainer expandable">
98*e5dd7070Spatrick<div class="example"><pre>
99*e5dd7070Spatrickvoid test() {
100*e5dd7070Spatrick  int *p;
101*e5dd7070Spatrick  int n1 = -1;
102*e5dd7070Spatrick  p = new int[n1]; // warn
103*e5dd7070Spatrick}
104*e5dd7070Spatrick</pre></div></div></td>
105*e5dd7070Spatrick<td class="aligned"></td></tr>
106*e5dd7070Spatrick
107*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
108*e5dd7070Spatrickmemory.ZeroAlloc</span><span class="lang">
109*e5dd7070Spatrick(C, C++)</span><div class="descr">
110*e5dd7070SpatrickAllocation of zero bytes.
111*e5dd7070Spatrick<br>Note: an enhancement to <span class="name">unix.Malloc</span>.
112*e5dd7070Spatrick<br>Note: <span class="name">unix.API</span> perform C-checks for zero
113*e5dd7070Spatrickallocation. This should be moved to <span class="name">unix.Malloc</span>.
114*e5dd7070Spatrick<p>Source: C++03 3.7.3.1p2; C++11 3.7.4.1p2.</p></div></div></td>
115*e5dd7070Spatrick<td><div class="exampleContainer expandable">
116*e5dd7070Spatrick<div class="example"><pre>
117*e5dd7070Spatrick#include &lt;stdlib.h&gt;
118*e5dd7070Spatrick
119*e5dd7070Spatrickvoid test() {
120*e5dd7070Spatrick  int *p = malloc(0); // warn
121*e5dd7070Spatrick  free(p);
122*e5dd7070Spatrick}
123*e5dd7070Spatrick</pre></div>
124*e5dd7070Spatrick<div class="example"><pre>
125*e5dd7070Spatrickvoid test() {
126*e5dd7070Spatrick  int *p = new int[0]; // warn
127*e5dd7070Spatrick  delete[] p;
128*e5dd7070Spatrick}
129*e5dd7070Spatrick</pre></div></div></td>
130*e5dd7070Spatrick<td class="aligned"><a href="https://reviews.llvm.org/D6178">
131*e5dd7070SpatrickD6178</a></td></tr>
132*e5dd7070Spatrick
133*e5dd7070Spatrick</table>
134*e5dd7070Spatrick
135*e5dd7070Spatrick<!-- ======================= constructors/destructors ====================== -->
136*e5dd7070Spatrick<h3>constructors/destructors</h3>
137*e5dd7070Spatrick<table class="checkers">
138*e5dd7070Spatrick<col class="namedescr"><col class="example"><col class="progress">
139*e5dd7070Spatrick<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>
140*e5dd7070Spatrick
141*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
142*e5dd7070Spatrickctordtor.ExptInsideDtor</span><span class="lang">
143*e5dd7070Spatrick(C++)</span><div class="descr">
144*e5dd7070SpatrickIt is dangerous to let an exception leave a destructor.
145*e5dd7070SpatrickUsing <code>try..catch</code> solves the problem.
146*e5dd7070Spatrick<p>Source: Scott Meyers "More Effective C++", item 11: Prevent exceptions from
147*e5dd7070Spatrickleaving destructors.</p></div></div></td>
148*e5dd7070Spatrick<td><div class="exampleContainer expandable">
149*e5dd7070Spatrick<div class="example"><pre>
150*e5dd7070Spatrickclass A {
151*e5dd7070Spatrick  A() {}
152*e5dd7070Spatrick  ~A() { throw 1; } // warn
153*e5dd7070Spatrick};
154*e5dd7070Spatrick</pre></div>
155*e5dd7070Spatrick<div class="example"><pre>
156*e5dd7070Spatrickvoid f() throw(int);
157*e5dd7070Spatrick
158*e5dd7070Spatrickclass A {
159*e5dd7070Spatrick  A() {}
160*e5dd7070Spatrick  ~A() { f(); } // warn
161*e5dd7070Spatrick};
162*e5dd7070Spatrick</pre></div></div></td>
163*e5dd7070Spatrick<td class="aligned"></td></tr>
164*e5dd7070Spatrick
165*e5dd7070Spatrick
166*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
167*e5dd7070Spatrickctordtor.PlacementSelfCopy</span><span class="lang">
168*e5dd7070Spatrick(C++11)</span><div class="descr">
169*e5dd7070SpatrickFor a placement copy or move, it is almost certainly an error if the
170*e5dd7070Spatrickconstructed object is also the object being copied from.</div></div></td>
171*e5dd7070Spatrick<td><div class="exampleContainer expandable">
172*e5dd7070Spatrick<div class="example"><pre>
173*e5dd7070Spatrickclass A {};
174*e5dd7070Spatrick
175*e5dd7070Spatrickvoid test(A *dst, A *src) {
176*e5dd7070Spatrick  ::new (dst) A(*dst); // warn (should be 'src')
177*e5dd7070Spatrick}
178*e5dd7070Spatrick</pre></div></div></td>
179*e5dd7070Spatrick<td class="aligned"><!--rdar://problem/13688366--></td></tr>
180*e5dd7070Spatrick
181*e5dd7070Spatrick</table>
182*e5dd7070Spatrick
183*e5dd7070Spatrick<!-- ============================== exceptions ============================= -->
184*e5dd7070Spatrick<h3>exceptions</h3>
185*e5dd7070Spatrick<table class="checkers">
186*e5dd7070Spatrick<col class="namedescr"><col class="example"><col class="progress">
187*e5dd7070Spatrick<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>
188*e5dd7070Spatrick
189*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
190*e5dd7070Spatrickexceptions.ThrowSpecButNotThrow</span><span class="lang">
191*e5dd7070Spatrick(C++)</span><div class="descr">
192*e5dd7070SpatrickFunction declaration has a <code>throw(<i>type</i>)</code> specifier but the
193*e5dd7070Spatrickfunction do not throw exceptions.</div></div></td>
194*e5dd7070Spatrick<td><div class="exampleContainer expandable">
195*e5dd7070Spatrick<div class="example"><pre>
196*e5dd7070Spatrickvoid test() throw(int) {
197*e5dd7070Spatrick} // warn
198*e5dd7070Spatrick</pre></div></div></td>
199*e5dd7070Spatrick<td class="aligned"></td></tr>
200*e5dd7070Spatrick
201*e5dd7070Spatrick
202*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
203*e5dd7070Spatrickexceptions.NoThrowSpecButThrows</span><span class="lang">
204*e5dd7070Spatrick(C++)</span><div class="descr">
205*e5dd7070SpatrickAn exception is throw from a function having a <code>throw()</code>
206*e5dd7070Spatrickspecifier.</div></div></td>
207*e5dd7070Spatrick<td><div class="exampleContainer expandable">
208*e5dd7070Spatrick<div class="example"><pre>
209*e5dd7070Spatrickvoid test() throw() {
210*e5dd7070Spatrick  throw(1); // warn
211*e5dd7070Spatrick}
212*e5dd7070Spatrick</pre></div></div></td>
213*e5dd7070Spatrick<td class="aligned"></td></tr>
214*e5dd7070Spatrick
215*e5dd7070Spatrick
216*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
217*e5dd7070Spatrickexceptions.ThrownTypeDiffersSpec</span><span class="lang">
218*e5dd7070Spatrick(C++)</span><div class="descr">
219*e5dd7070SpatrickThe type of a thrown exception differs from those specified in
220*e5dd7070Spatricka <code>throw(<i>type</i>)</code> specifier.</div></div></td>
221*e5dd7070Spatrick<td><div class="exampleContainer expandable">
222*e5dd7070Spatrick<div class="example"><pre>
223*e5dd7070Spatrickstruct S{};
224*e5dd7070Spatrick
225*e5dd7070Spatrickvoid test() throw(int) {
226*e5dd7070Spatrick  S s;
227*e5dd7070Spatrick  throw (s); // warn
228*e5dd7070Spatrick}
229*e5dd7070Spatrick</pre></div></div></td>
230*e5dd7070Spatrick<td class="aligned"></td></tr>
231*e5dd7070Spatrick
232*e5dd7070Spatrick</table>
233*e5dd7070Spatrick
234*e5dd7070Spatrick<!-- ========================= smart pointers ============================== -->
235*e5dd7070Spatrick<h3>smart pointers</h3>
236*e5dd7070Spatrick<table class="checkers">
237*e5dd7070Spatrick<col class="namedescr"><col class="example"><col class="progress">
238*e5dd7070Spatrick<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>
239*e5dd7070Spatrick
240*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
241*e5dd7070Spatricksmartptr.SmartPtrInit</span><span class="lang">
242*e5dd7070Spatrick(C++)</span><div class="descr">
243*e5dd7070SpatrickC++03: <code>auto_ptr</code> should store a pointer to an object obtained via
244*e5dd7070Spatricknew as allocated memory will be cleaned using <code>delete</code>.<br>
245*e5dd7070SpatrickC++11: one should use <code>unique_ptr&lt;<i>type</i>[]&gt;</code> to keep a
246*e5dd7070Spatrickpointer to memory allocated by <code>new[]</code>.<br>
247*e5dd7070SpatrickC++11: to keep a pointer to memory allocated by <code>new[]</code> in
248*e5dd7070Spatricka <code>shared_ptr</code> one should use a custom deleter that calls <code>
249*e5dd7070Spatrickdelete[].</code>.
250*e5dd7070Spatrick<p>Source: C++03 20.4.5p1; C++11 <code>auto_ptr</code> is deprecated (D.10).</p></div></div></td>
251*e5dd7070Spatrick<td><div class="exampleContainer expandable">
252*e5dd7070Spatrick<div class="example"><pre>
253*e5dd7070Spatrick#include &lt;stdlib.h&gt;
254*e5dd7070Spatrick#include &lt;memory&gt;
255*e5dd7070Spatrick
256*e5dd7070Spatrickvoid test() {
257*e5dd7070Spatrick  std::auto_ptr&lt;int&gt; p1(new int); // Ok
258*e5dd7070Spatrick  std::auto_ptr&lt;int&gt; p2(new int[3]); // warn
259*e5dd7070Spatrick}
260*e5dd7070Spatrick</pre></div>
261*e5dd7070Spatrick<div class="example"><pre>
262*e5dd7070Spatrick#include &lt;stdlib.h&gt;
263*e5dd7070Spatrick#include &lt;memory&gt;
264*e5dd7070Spatrick
265*e5dd7070Spatrickvoid test() {
266*e5dd7070Spatrick  std::auto_ptr&lt;int&gt; p((int *)malloc(sizeof(int))); // warn
267*e5dd7070Spatrick}
268*e5dd7070Spatrick</pre></div></div></td>
269*e5dd7070Spatrick<td class="aligned"></td></tr>
270*e5dd7070Spatrick
271*e5dd7070Spatrick</table>
272*e5dd7070Spatrick
273*e5dd7070Spatrick<!-- ============================== dead code ============================== -->
274*e5dd7070Spatrick<h3>dead code</h3>
275*e5dd7070Spatrick<table class="checkers">
276*e5dd7070Spatrick<col class="namedescr"><col class="example"><col class="progress">
277*e5dd7070Spatrick<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>
278*e5dd7070Spatrick
279*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
280*e5dd7070Spatrickdeadcode.UnmodifiedVariable</span><span class="lang">
281*e5dd7070Spatrick(C, C++)</span><div class="descr">
282*e5dd7070SpatrickA variable is never modified but was not declared const and is not a
283*e5dd7070Spatrickreference.<br><br><i>(opt-in checker)</i></div></div></td>
284*e5dd7070Spatrick<td><div class="exampleContainer expandable">
285*e5dd7070Spatrick<div class="example"><pre>
286*e5dd7070Spatrickextern int computeDelta();
287*e5dd7070Spatrick
288*e5dd7070Spatrickint test(bool cond) {
289*e5dd7070Spatrick  int i = 0;
290*e5dd7070Spatrick  if (cond) {
291*e5dd7070Spatrick    const int delta = computeDelta();
292*e5dd7070Spatrick    // warn: forgot to modify 'i'
293*e5dd7070Spatrick  }
294*e5dd7070Spatrick  return i;
295*e5dd7070Spatrick}
296*e5dd7070Spatrick</pre></div></div></td>
297*e5dd7070Spatrick<td class="aligned"><a href="https://bugs.llvm.org/show_bug.cgi?id=16890">PR16890</a></td></tr>
298*e5dd7070Spatrick
299*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
300*e5dd7070Spatrickdeadcode.IdempotentOperations</span><span class="lang">
301*e5dd7070Spatrick(C)</span><div class="descr">
302*e5dd7070SpatrickWarn about idempotent operations.</div></div></td>
303*e5dd7070Spatrick<td><div class="exampleContainer expandable">
304*e5dd7070Spatrick<div class="example"><pre>
305*e5dd7070Spatrickvoid test() {
306*e5dd7070Spatrick  int x = 7;
307*e5dd7070Spatrick  x = x; // warn: value is always the same
308*e5dd7070Spatrick}
309*e5dd7070Spatrick</pre></div>
310*e5dd7070Spatrick<div class="example"><pre>
311*e5dd7070Spatrickvoid test() {
312*e5dd7070Spatrick  int x = 7;
313*e5dd7070Spatrick  x /= x; // warn: value is always 1
314*e5dd7070Spatrick}
315*e5dd7070Spatrick</pre></div>
316*e5dd7070Spatrick<div class="example"><pre>
317*e5dd7070Spatrickvoid test() {
318*e5dd7070Spatrick  int x = 7, one = 1;
319*e5dd7070Spatrick  x *= one; // warn: right op is always 1
320*e5dd7070Spatrick}
321*e5dd7070Spatrick</pre></div>
322*e5dd7070Spatrick<div class="example"><pre>
323*e5dd7070Spatrickvoid test() {
324*e5dd7070Spatrick  int x = 7, zero = 0;
325*e5dd7070Spatrick  x = x - zero;
326*e5dd7070Spatrick   // warn: the right operand to '-' is always 0
327*e5dd7070Spatrick}
328*e5dd7070Spatrick</pre></div></div></td>
329*e5dd7070Spatrick<td class="aligned">removed from alpha.deadcode.* at
330*e5dd7070Spatrick<a href="https://reviews.llvm.org/rL198476">r198476</a></td></tr>
331*e5dd7070Spatrick
332*e5dd7070Spatrick</table>
333*e5dd7070Spatrick
334*e5dd7070Spatrick<!-- ================================ POSIX ================================ -->
335*e5dd7070Spatrick<h3>POSIX</h3>
336*e5dd7070Spatrick<table class="checkers">
337*e5dd7070Spatrick<col class="namedescr"><col class="example"><col class="progress">
338*e5dd7070Spatrick<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>
339*e5dd7070Spatrick
340*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
341*e5dd7070Spatrickposix.Errno</span><span class="lang">
342*e5dd7070Spatrick(C)</span><div class="descr">
343*e5dd7070SpatrickRecord that <code>errno</code> is non-zero when certain functions
344*e5dd7070Spatrickfail.</div></div></td>
345*e5dd7070Spatrick<td><div class="exampleContainer expandable">
346*e5dd7070Spatrick<div class="example"><pre>
347*e5dd7070Spatrick#include &lt;stdlib.h&gt;
348*e5dd7070Spatrick
349*e5dd7070Spatrickint readWrapper(int fd, int *count) {
350*e5dd7070Spatrick  int lcount = read(fd, globalBuf, sizeof(globalBuf));
351*e5dd7070Spatrick  if (lcount < 0)
352*e5dd7070Spatrick    return errno;
353*e5dd7070Spatrick  *count = lcount;
354*e5dd7070Spatrick  return 0;
355*e5dd7070Spatrick}
356*e5dd7070Spatrick
357*e5dd7070Spatrickvoid use(int fd) {
358*e5dd7070Spatrick  int count;
359*e5dd7070Spatrick  if (!readWrapper(fd, &amp;count))
360*e5dd7070Spatrick    print("%d", count); // should not warn
361*e5dd7070Spatrick}
362*e5dd7070Spatrick</pre></div></div></td>
363*e5dd7070Spatrick<td class="aligned"><a href="https://bugs.llvm.org/show_bug.cgi?id=18701">PR18701</a></td></tr>
364*e5dd7070Spatrick
365*e5dd7070Spatrick</table>
366*e5dd7070Spatrick
367*e5dd7070Spatrick<!-- ========================= undefined behavior ========================== -->
368*e5dd7070Spatrick<h3>undefined behavior</h3>
369*e5dd7070Spatrick<table class="checkers">
370*e5dd7070Spatrick<col class="namedescr"><col class="example"><col class="progress">
371*e5dd7070Spatrick<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>
372*e5dd7070Spatrick
373*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
374*e5dd7070Spatrickundefbehavior.ExitInDtor</span><span class="lang">
375*e5dd7070Spatrick(C++)</span><div class="descr">
376*e5dd7070SpatrickUndefined behavior: <code>std::exit()</code> is called to end the program during
377*e5dd7070Spatrickthe destruction of an object with static storage duration.
378*e5dd7070Spatrick<p>Source: C++11 3.6.1p4.</p></div></div></td>
379*e5dd7070Spatrick<td><div class="exampleContainer expandable">
380*e5dd7070Spatrick<div class="example"><pre>
381*e5dd7070Spatrick#include &lt;cstdlib&gt;
382*e5dd7070Spatrick
383*e5dd7070Spatrickclass A {
384*e5dd7070Spatrickpublic:
385*e5dd7070Spatrick  ~A() {
386*e5dd7070Spatrick    std::exit(1); // warn
387*e5dd7070Spatrick  }
388*e5dd7070Spatrick};
389*e5dd7070Spatrick</pre></div></div></td>
390*e5dd7070Spatrick<td class="aligned"></td></tr>
391*e5dd7070Spatrick
392*e5dd7070Spatrick
393*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
394*e5dd7070Spatrickundefbehavior.LocalStaticDestroyed</span><span class="lang">
395*e5dd7070Spatrick(C++)</span><div class="descr">
396*e5dd7070SpatrickUndefined behavior: function containing a definition of static local object is
397*e5dd7070Spatrickcalled during the destruction of an object with static storage duration so that
398*e5dd7070Spatrickflow of control passes through the definition of the previously destroyed
399*e5dd7070Spatrickstatic local object.
400*e5dd7070Spatrick<p>Source: C++11 3.6.3p2.</p></div></div></td>
401*e5dd7070Spatrick<td><div class="exampleContainer expandable">
402*e5dd7070Spatrick<div class="example"><pre>
403*e5dd7070Spatrickvoid f();
404*e5dd7070Spatrick
405*e5dd7070Spatrickclass A {
406*e5dd7070Spatrickpublic:
407*e5dd7070Spatrick  ~A() {
408*e5dd7070Spatrick    f(); // warn
409*e5dd7070Spatrick  }
410*e5dd7070Spatrick};
411*e5dd7070Spatrick
412*e5dd7070Spatrickclass B {};
413*e5dd7070Spatrick
414*e5dd7070SpatrickA a;
415*e5dd7070Spatrick
416*e5dd7070Spatrickvoid f() {
417*e5dd7070Spatrick  static B b;
418*e5dd7070Spatrick}
419*e5dd7070Spatrick</pre></div></div></td>
420*e5dd7070Spatrick<td class="aligned"></td></tr>
421*e5dd7070Spatrick
422*e5dd7070Spatrick
423*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
424*e5dd7070Spatrickundefbehavior.ZeroAllocDereference</span><span class="lang">
425*e5dd7070Spatrick(C, C++)</span><div class="descr">
426*e5dd7070SpatrickThe effect of dereferencing a pointer returned as a request for zero size is
427*e5dd7070Spatrickundefined.<br>
428*e5dd7070SpatrickNote: possibly an enhancement to <span class="name">
429*e5dd7070Spatrickunix.Malloc</span>.
430*e5dd7070Spatrick<p>Source: C++03 3.7.3.1p2; C++11 3.7.4.1p2.</p></div></div></td>
431*e5dd7070Spatrick<td><div class="exampleContainer expandable">
432*e5dd7070Spatrick<div class="example"><pre>
433*e5dd7070Spatrick#include &lt;stdlib.h&gt;
434*e5dd7070Spatrick
435*e5dd7070Spatrickvoid test() {
436*e5dd7070Spatrick  int *p = (int *)malloc(0);
437*e5dd7070Spatrick  *p = 1; // warn
438*e5dd7070Spatrick  free(p);
439*e5dd7070Spatrick}
440*e5dd7070Spatrick</pre></div>
441*e5dd7070Spatrick<div class="example"><pre>
442*e5dd7070Spatrickvoid f(int);
443*e5dd7070Spatrick
444*e5dd7070Spatrickvoid test() {
445*e5dd7070Spatrick  int *p = new int[0];
446*e5dd7070Spatrick  f(*p); // warn
447*e5dd7070Spatrick  delete[] p;
448*e5dd7070Spatrick}
449*e5dd7070Spatrick</pre></div></div></td>
450*e5dd7070Spatrick<td class="aligned"><a href="https://reviews.llvm.org/D8273">D8273</a></td></tr>
451*e5dd7070Spatrick
452*e5dd7070Spatrick
453*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
454*e5dd7070Spatrickundefbehavior.DeadReferenced</span><span class="lang">
455*e5dd7070Spatrick(C++)</span><div class="descr">
456*e5dd7070SpatrickUndefined behavior: the following usage of the pointer to the object whose
457*e5dd7070Spatricklifetime has ended can result in undefined behavior:<br>
458*e5dd7070SpatrickThe object will be or was of a class type with a non-trivial destructor and
459*e5dd7070Spatrick<ul><li>the pointer is used as the operand of a delete-expression</li></ul>
460*e5dd7070SpatrickThe object will be or was of a non-POD class type (C++11: any class type) and
461*e5dd7070Spatrick<ul><li>the pointer is used to access a non-static data member or call a
462*e5dd7070Spatricknon-static member function of the object</li>
463*e5dd7070Spatrick<li>the pointer is implicitly converted to a pointer to a base class
464*e5dd7070Spatricktype</li>
465*e5dd7070Spatrick<li>the pointer is used as the operand of a <code>static_cast</code> (except
466*e5dd7070Spatrickwhen the conversion is to <code>void*</code>, or to <code>void*</code> and
467*e5dd7070Spatricksubsequently to <code>char*</code>, or <code>unsigned char*</code>)</li>
468*e5dd7070Spatrick<li>the pointer is used as the operand of a <code>dynamic_cast</code></li></ul>
469*e5dd7070Spatrick<p>Source: C++03 3.8p5, p7; C++11 3.8p5, p7.</p></div></div></td>
470*e5dd7070Spatrick<td><div class="exampleContainer expandable">
471*e5dd7070Spatrick<div class="example"><pre>
472*e5dd7070Spatrick#include &lt;new&gt;
473*e5dd7070Spatrick
474*e5dd7070Spatrickclass A {
475*e5dd7070Spatrickpublic:
476*e5dd7070Spatrick  ~A();
477*e5dd7070Spatrick};
478*e5dd7070Spatrick
479*e5dd7070Spatrickclass B : public A {};
480*e5dd7070Spatrick
481*e5dd7070Spatrickvoid test() {
482*e5dd7070Spatrick  A *a = new A;
483*e5dd7070Spatrick  new(a) B;
484*e5dd7070Spatrick  delete a; // warn
485*e5dd7070Spatrick}
486*e5dd7070Spatrick</pre></div>
487*e5dd7070Spatrick<div class="example"><pre>
488*e5dd7070Spatrick#include &lt;new&gt;
489*e5dd7070Spatrick
490*e5dd7070Spatrickclass A {
491*e5dd7070Spatrickpublic:
492*e5dd7070Spatrick  ~A();
493*e5dd7070Spatrick};
494*e5dd7070Spatrick
495*e5dd7070Spatrickclass B {};
496*e5dd7070Spatrick
497*e5dd7070Spatrickvoid test() {
498*e5dd7070Spatrick  A *a = new A;
499*e5dd7070Spatrick  new(a) B;
500*e5dd7070Spatrick  a->~A();
501*e5dd7070Spatrick}
502*e5dd7070Spatrick</pre></div>
503*e5dd7070Spatrick<div class="example"><pre>
504*e5dd7070Spatrick#include &lt;new&gt;
505*e5dd7070Spatrick
506*e5dd7070Spatrickclass A {
507*e5dd7070Spatrickpublic:
508*e5dd7070Spatrick  ~A();
509*e5dd7070Spatrick};
510*e5dd7070Spatrick
511*e5dd7070Spatrickclass B : public A {};
512*e5dd7070Spatrick
513*e5dd7070Spatrickclass C {};
514*e5dd7070Spatrick
515*e5dd7070Spatrickvoid f(A*);
516*e5dd7070Spatrick
517*e5dd7070Spatrickvoid test() {
518*e5dd7070Spatrick  B *b = new B;
519*e5dd7070Spatrick  new(b) C;
520*e5dd7070Spatrick  f(b); // warn
521*e5dd7070Spatrick}
522*e5dd7070Spatrick</pre></div>
523*e5dd7070Spatrick<div class="example"><pre>
524*e5dd7070Spatrick#include &lt;new&gt;
525*e5dd7070Spatrick
526*e5dd7070Spatrickclass A {
527*e5dd7070Spatrickpublic:
528*e5dd7070Spatrick  ~A();
529*e5dd7070Spatrick};
530*e5dd7070Spatrick
531*e5dd7070Spatrickclass B : public A {};
532*e5dd7070Spatrick
533*e5dd7070Spatrickclass C {};
534*e5dd7070Spatrick
535*e5dd7070SpatrickA* test() {
536*e5dd7070Spatrick  B *b = new B;
537*e5dd7070Spatrick  new(b) C;
538*e5dd7070Spatrick  return static_cast&lt;A*&gt;(b); // warn
539*e5dd7070Spatrick}
540*e5dd7070Spatrick</pre></div>
541*e5dd7070Spatrick<div class="example"><pre>
542*e5dd7070Spatrick#include &lt;new&gt;
543*e5dd7070Spatrick
544*e5dd7070Spatrickclass A {
545*e5dd7070Spatrickpublic:
546*e5dd7070Spatrick  ~A();
547*e5dd7070Spatrick};
548*e5dd7070Spatrick
549*e5dd7070Spatrickclass B : public A {};
550*e5dd7070Spatrick
551*e5dd7070Spatrickclass C {};
552*e5dd7070Spatrick
553*e5dd7070SpatrickA* test() {
554*e5dd7070Spatrick  B *b = new B;
555*e5dd7070Spatrick  new(b) C;
556*e5dd7070Spatrick  return dynamic_cast&lt;A*&gt;(b); // warn
557*e5dd7070Spatrick}
558*e5dd7070Spatrick</pre></div></div></td>
559*e5dd7070Spatrick<td class="aligned"></td></tr>
560*e5dd7070Spatrick
561*e5dd7070Spatrick
562*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
563*e5dd7070Spatrickundefbehavior.ObjLocChanges</span><span class="lang">
564*e5dd7070Spatrick(C++)</span><div class="descr">
565*e5dd7070SpatrickUndefined behavior: the program must ensure that an object occupies the same
566*e5dd7070Spatrickstorage location when the implicit or explicit destructor call takes place.
567*e5dd7070Spatrick<p>Source: C++11 3.8p8.</p></div></div></td>
568*e5dd7070Spatrick<td><div class="exampleContainer expandable">
569*e5dd7070Spatrick<div class="example"><pre>
570*e5dd7070Spatrick#include &lt;new&gt;
571*e5dd7070Spatrick
572*e5dd7070Spatrickclass A {};
573*e5dd7070Spatrick
574*e5dd7070Spatrickclass B {
575*e5dd7070Spatrickpublic:
576*e5dd7070Spatrick  ~B();
577*e5dd7070Spatrick};
578*e5dd7070Spatrick
579*e5dd7070Spatrickvoid test() {
580*e5dd7070Spatrick  B b;
581*e5dd7070Spatrick  new (&b) A;
582*e5dd7070Spatrick} // warn
583*e5dd7070Spatrick</pre></div>
584*e5dd7070Spatrick<div class="example"><pre>
585*e5dd7070Spatrick#include &lt;new&gt;
586*e5dd7070Spatrick
587*e5dd7070Spatrickclass A {};
588*e5dd7070Spatrick
589*e5dd7070Spatrickclass B {
590*e5dd7070Spatrickpublic:
591*e5dd7070Spatrick  ~B();
592*e5dd7070Spatrick};
593*e5dd7070Spatrick
594*e5dd7070Spatrickvoid test() {
595*e5dd7070Spatrick  B *b = new B;
596*e5dd7070Spatrick  new (b) A;
597*e5dd7070Spatrick  delete b; // warn
598*e5dd7070Spatrick}
599*e5dd7070Spatrick</pre></div></div></td>
600*e5dd7070Spatrick<td class="aligned"></td></tr>
601*e5dd7070Spatrick
602*e5dd7070Spatrick
603*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
604*e5dd7070Spatrickundefbehavior.ExprEvalOrderUndef</span><span class="lang">
605*e5dd7070Spatrick(C, C++03)</span><div class="descr">
606*e5dd7070SpatrickUndefined behavior: a scalar object shall have its stored value modified at
607*e5dd7070Spatrickmost once by the evaluation of an expression.<br>
608*e5dd7070SpatrickNote: most cases are currently handled by the Clang core (search for 'multiple
609*e5dd7070Spatrickunsequenced modifications' warning in Clang tests).
610*e5dd7070Spatrick<p>Source: C++03 5p4.</p></div></div></td>
611*e5dd7070Spatrick<td><div class="exampleContainer expandable">
612*e5dd7070Spatrick<div class="example"><pre>
613*e5dd7070Spatrickint test () {
614*e5dd7070Spatrick  int i = 0;
615*e5dd7070Spatrick  i = ++i + 1; // warn
616*e5dd7070Spatrick  return i;
617*e5dd7070Spatrick}
618*e5dd7070Spatrick</pre></div></div></td>
619*e5dd7070Spatrick<td class="aligned"></td></tr>
620*e5dd7070Spatrick
621*e5dd7070Spatrick
622*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
623*e5dd7070Spatrickundefbehavior.StaticInitReentered</span><span class="lang">
624*e5dd7070Spatrick(C++)</span><div class="descr">
625*e5dd7070SpatrickUndefined behavior: static declaration is re-entered while the object is being
626*e5dd7070Spatrickinitialized.
627*e5dd7070Spatrick<p>Source: C++11 6.7p4.</p></div></div></td>
628*e5dd7070Spatrick<td><div class="exampleContainer expandable">
629*e5dd7070Spatrick<div class="example"><pre>
630*e5dd7070Spatrickint test(int i) {
631*e5dd7070Spatrick  static int s = test(2 * i); // warn
632*e5dd7070Spatrick  return i + 1;
633*e5dd7070Spatrick}
634*e5dd7070Spatrick</pre></div></div></td>
635*e5dd7070Spatrick<td class="aligned"></td></tr>
636*e5dd7070Spatrick
637*e5dd7070Spatrick
638*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
639*e5dd7070Spatrickundefbehavior.ConstModified</span><span class="lang">
640*e5dd7070Spatrick(C, C++)</span><div class="descr">
641*e5dd7070SpatrickUndefined behavior: const object is being modified.
642*e5dd7070Spatrick<p>Source: C++03 7.1.5.1p4, C++11 7.1.6.1p4.</p></div></div></td>
643*e5dd7070Spatrick<td><div class="exampleContainer expandable">
644*e5dd7070Spatrick<div class="example"><pre>
645*e5dd7070Spatrickvoid test() {
646*e5dd7070Spatrick  const int *cp = new const int (0);
647*e5dd7070Spatrick  int *p = const_cast&lt;int *&gt;(cp);
648*e5dd7070Spatrick  *p = 1; // warn
649*e5dd7070Spatrick  delete p;
650*e5dd7070Spatrick}
651*e5dd7070Spatrick</pre></div>
652*e5dd7070Spatrick<div class="example"><pre>
653*e5dd7070Spatrickclass C {
654*e5dd7070Spatrickpublic :
655*e5dd7070Spatrick  int i;
656*e5dd7070Spatrick  C();
657*e5dd7070Spatrick};
658*e5dd7070Spatrick
659*e5dd7070Spatrickvoid test() {
660*e5dd7070Spatrick  const C cb;
661*e5dd7070Spatrick
662*e5dd7070Spatrick  C* cp = const_cast&lt;C *&gt;(&cb);
663*e5dd7070Spatrick  cp-&gt;i = 1; // warn
664*e5dd7070Spatrick}
665*e5dd7070Spatrick</pre></div></div></td>
666*e5dd7070Spatrick<td class="aligned"></td></tr>
667*e5dd7070Spatrick
668*e5dd7070Spatrick
669*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
670*e5dd7070Spatrickundefbehavior.DeadDestructed</span><span class="lang">
671*e5dd7070Spatrick(C++)</span><div class="descr">
672*e5dd7070SpatrickUndefined behavior: the destructor is invoked for an object whose lifetime
673*e5dd7070Spatrickhas ended.
674*e5dd7070Spatrick<p>Source: C++11 12.4p14.</p></div></div></td>
675*e5dd7070Spatrick<td><div class="exampleContainer expandable">
676*e5dd7070Spatrick<div class="example"><pre>
677*e5dd7070Spatrickclass A {
678*e5dd7070Spatrickpublic:
679*e5dd7070Spatrick  void f();
680*e5dd7070Spatrick  A();
681*e5dd7070Spatrick  ~A();
682*e5dd7070Spatrick};
683*e5dd7070Spatrick
684*e5dd7070Spatrickvoid test() {
685*e5dd7070Spatrick  A a;
686*e5dd7070Spatrick  a.~A();
687*e5dd7070Spatrick} // warn
688*e5dd7070Spatrick</pre></div></div></td>
689*e5dd7070Spatrick<td class="aligned"></td></tr>
690*e5dd7070Spatrick
691*e5dd7070Spatrick
692*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
693*e5dd7070Spatrickundefbehavior.MethodCallBeforeBaseInit</span><span class="lang">
694*e5dd7070Spatrick(C++)</span><div class="descr">
695*e5dd7070SpatrickUndefined behavior: calls member function but base not yet initialized.
696*e5dd7070Spatrick<p>Source: C++03 12.6.2p8; C++11 12.6.2p13.</p></div></div></td>
697*e5dd7070Spatrick<td><div class="exampleContainer expandable">
698*e5dd7070Spatrick<div class="example"><pre>
699*e5dd7070Spatrickclass A {
700*e5dd7070Spatrickpublic :
701*e5dd7070Spatrick  A(int);
702*e5dd7070Spatrick};
703*e5dd7070Spatrick
704*e5dd7070Spatrickclass B : public A {
705*e5dd7070Spatrickpublic :
706*e5dd7070Spatrick  int f();
707*e5dd7070Spatrick  B() : A(f()) {} // warn
708*e5dd7070Spatrick};
709*e5dd7070Spatrick</pre></div></div></td>
710*e5dd7070Spatrick<td class="aligned"></td></tr>
711*e5dd7070Spatrick
712*e5dd7070Spatrick
713*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
714*e5dd7070Spatrickundefbehavior.MemberOrBaseRefBeforeCtor</span><span class="lang">
715*e5dd7070Spatrick(C++)</span><div class="descr">
716*e5dd7070SpatrickC++ Undefined behavior: non-static member or base class of non-POD class type
717*e5dd7070Spatrickis referred before constructor begins execution.<br>
718*e5dd7070SpatrickC++11 Undefined behavior: non-static member or base class of a class with a
719*e5dd7070Spatricknon-trivial constructor is referred before constructor begins execution.
720*e5dd7070Spatrick<p>Source: C++03 12.7p1; C++11 12.7p1.</p></div></div></td>
721*e5dd7070Spatrick<td><div class="exampleContainer expandable">
722*e5dd7070Spatrick<div class="example"><pre>
723*e5dd7070Spatrickstruct non_POD {
724*e5dd7070Spatrick  int i;
725*e5dd7070Spatrick  non_POD();
726*e5dd7070Spatrick};
727*e5dd7070Spatrick
728*e5dd7070Spatrickextern non_POD non_pod;
729*e5dd7070Spatrick
730*e5dd7070Spatrickint *p = &amp;non_pod.i; // warn
731*e5dd7070Spatrick</pre></div>
732*e5dd7070Spatrick<div class="example"><pre>
733*e5dd7070Spatrickstruct POD {
734*e5dd7070Spatrick  int i;
735*e5dd7070Spatrick};
736*e5dd7070Spatrick
737*e5dd7070Spatrickstruct non_POD : public POD {
738*e5dd7070Spatrick  POD pod;
739*e5dd7070Spatrick};
740*e5dd7070Spatrick
741*e5dd7070Spatrickextern non_POD non_pod;
742*e5dd7070Spatrick
743*e5dd7070Spatrickint *p = &amp;non_pod.pod.i; // warn
744*e5dd7070Spatrick</pre></div>
745*e5dd7070Spatrick<div class="example"><pre>
746*e5dd7070Spatrickstruct POD {
747*e5dd7070Spatrick  int i;
748*e5dd7070Spatrick};
749*e5dd7070Spatrick
750*e5dd7070Spatrickstruct non_POD : public POD {};
751*e5dd7070Spatrick
752*e5dd7070Spatrickextern non_POD non_pod;
753*e5dd7070Spatrick
754*e5dd7070SpatrickPOD *p = &amp;non_pod; // warn
755*e5dd7070Spatrick</pre></div>
756*e5dd7070Spatrick<div class="example"><pre>
757*e5dd7070Spatrickstruct non_POD {
758*e5dd7070Spatrick  int i;
759*e5dd7070Spatrick  non_POD();
760*e5dd7070Spatrick};
761*e5dd7070Spatrick
762*e5dd7070Spatrickstruct S {
763*e5dd7070Spatrick  int *k;
764*e5dd7070Spatrick  non_POD non_pod;
765*e5dd7070Spatrick  S() : k(&amp;non_pod.i) {} // warn
766*e5dd7070Spatrick};
767*e5dd7070Spatrick</pre></div></div></td>
768*e5dd7070Spatrick<td class="aligned"></td></tr>
769*e5dd7070Spatrick
770*e5dd7070Spatrick
771*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
772*e5dd7070Spatrickundefbehavior.MemberRefAfterDtor</span><span class="lang">
773*e5dd7070Spatrick(C++)</span><div class="descr">
774*e5dd7070SpatrickC++03: Undefined behavior: non-static member of non-POD class type is referred
775*e5dd7070Spatrickafter destructor ends execution.<br>
776*e5dd7070SpatrickC++11: Undefined behavior: non-static member of a class with a non-trivial
777*e5dd7070Spatrickdestructor is referred after destructor ends execution.
778*e5dd7070Spatrick<p>Source: C++03 12.7p1; C++11 12.7p1.</p></div></div></td>
779*e5dd7070Spatrick<td><div class="exampleContainer expandable">
780*e5dd7070Spatrick<div class="example"><pre>
781*e5dd7070Spatrickclass C {
782*e5dd7070Spatrickpublic:
783*e5dd7070Spatrick  C();
784*e5dd7070Spatrick  void f();
785*e5dd7070Spatrick};
786*e5dd7070Spatrick
787*e5dd7070Spatrickvoid test() {
788*e5dd7070Spatrick  C *c = new C();
789*e5dd7070Spatrick  c-&gt;~C();
790*e5dd7070Spatrick  c-&gt;f(); // warn
791*e5dd7070Spatrick}
792*e5dd7070Spatrick</pre></div></div></td>
793*e5dd7070Spatrick<td class="aligned"></td></tr>
794*e5dd7070Spatrick
795*e5dd7070Spatrick
796*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
797*e5dd7070Spatrickundefbehavior.CtorForeignCall</span><span class="lang">
798*e5dd7070Spatrick(C++)</span><div class="descr">
799*e5dd7070SpatrickUndefined behavior: call to virtual function of an object under construction
800*e5dd7070Spatrickwhose type is neither the constructors own class or one of its bases.
801*e5dd7070Spatrick<p>Source: C++11 12.7p4.</p></div></div></td>
802*e5dd7070Spatrick<td><div class="exampleContainer expandable">
803*e5dd7070Spatrick<div class="example"><pre>
804*e5dd7070Spatrickclass A {
805*e5dd7070Spatrickpublic:
806*e5dd7070Spatrick  virtual void f() {};
807*e5dd7070Spatrick};
808*e5dd7070Spatrick
809*e5dd7070Spatrickclass B {
810*e5dd7070Spatrickpublic:
811*e5dd7070Spatrick  B(A* a) { a-&gt;f(); } // warn
812*e5dd7070Spatrick};
813*e5dd7070Spatrick
814*e5dd7070Spatrickclass C : public A, B {
815*e5dd7070Spatrickpublic:
816*e5dd7070Spatrick  C() : B((A*)this) {}
817*e5dd7070Spatrick};
818*e5dd7070Spatrick</pre></div></div></td>
819*e5dd7070Spatrick<td class="aligned"></td></tr>
820*e5dd7070Spatrick
821*e5dd7070Spatrick
822*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
823*e5dd7070Spatrickundefbehavior.CtorForeignTypeid</span><span class="lang">
824*e5dd7070Spatrick(C++)</span><div class="descr">
825*e5dd7070SpatrickUndefined behavior: the operand of <code>typeid</code> is an object under
826*e5dd7070Spatrickconstruction whose type is neither the constructors own class or one of its
827*e5dd7070Spatrickbases.
828*e5dd7070Spatrick<p>Source: C++11 12.7p5.</p></div></div></td>
829*e5dd7070Spatrick<td><div class="exampleContainer expandable">
830*e5dd7070Spatrick<div class="example"><pre>
831*e5dd7070Spatrick#include &lt;typeinfo&gt;
832*e5dd7070Spatrick
833*e5dd7070Spatrickclass A {};
834*e5dd7070Spatrick
835*e5dd7070Spatrickclass B {
836*e5dd7070Spatrickpublic:
837*e5dd7070Spatrick  B(A* a) {
838*e5dd7070Spatrick    (void)typeid(*a); // warn
839*e5dd7070Spatrick  }
840*e5dd7070Spatrick};
841*e5dd7070Spatrick
842*e5dd7070Spatrickclass C : public A, B {
843*e5dd7070Spatrickpublic:
844*e5dd7070Spatrick  C() : B((A*)this) {}
845*e5dd7070Spatrick};
846*e5dd7070Spatrick</pre></div></div></td>
847*e5dd7070Spatrick<td class="aligned"></td></tr>
848*e5dd7070Spatrick
849*e5dd7070Spatrick
850*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
851*e5dd7070Spatrickundefbehavior.CtorForeignCast</span><span class="lang">
852*e5dd7070Spatrick(C++)</span><div class="descr">
853*e5dd7070SpatrickUndefined behavior: the operand of <code>dynamic_cast</code> is an object under
854*e5dd7070Spatrickconstruction whose type is neither the constructors own class or one of its
855*e5dd7070Spatrickbases.
856*e5dd7070Spatrick<p>Source: C++11 12.7p6.</p></div></div></td>
857*e5dd7070Spatrick<td><div class="exampleContainer expandable">
858*e5dd7070Spatrick<div class="example"><pre>
859*e5dd7070Spatrick#include &lt;typeinfo&gt;
860*e5dd7070Spatrick
861*e5dd7070Spatrickclass A {
862*e5dd7070Spatrickpublic:
863*e5dd7070Spatrick  virtual void f() {};
864*e5dd7070Spatrick};
865*e5dd7070Spatrick
866*e5dd7070Spatrickclass B {
867*e5dd7070Spatrickpublic:
868*e5dd7070Spatrick  B(A* a) {
869*e5dd7070Spatrick    (void)dynamic_cast&lt;B*&gt;(a); //warn
870*e5dd7070Spatrick  }
871*e5dd7070Spatrick};
872*e5dd7070Spatrick
873*e5dd7070Spatrickclass C : public A, B {
874*e5dd7070Spatrickpublic:
875*e5dd7070Spatrick  C() : B((A*)this) {}
876*e5dd7070Spatrick};
877*e5dd7070Spatrick</pre></div></div></td>
878*e5dd7070Spatrick<td class="aligned"></td></tr>
879*e5dd7070Spatrick
880*e5dd7070Spatrick
881*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
882*e5dd7070Spatrickundefbehavior.MemberOrBaseRefInCatch</span><span class="lang">
883*e5dd7070Spatrick(C++)</span><div class="descr">
884*e5dd7070SpatrickUndefined behavior: referring to any non-static member or base class of an
885*e5dd7070Spatrickobject in the handler for a function-try-block of a constructor or destructor
886*e5dd7070Spatrickfor that object results in undefined behavior.
887*e5dd7070Spatrick<p>Source: C++11 15.3p10.</p></div></div></td>
888*e5dd7070Spatrick<td><div class="exampleContainer expandable">
889*e5dd7070Spatrick<div class="example"><pre>
890*e5dd7070Spatrickvoid f() { throw 1; }
891*e5dd7070Spatrick
892*e5dd7070Spatrickclass C {
893*e5dd7070Spatrick  int i;
894*e5dd7070Spatrickpublic :
895*e5dd7070Spatrick  C()
896*e5dd7070Spatrick  try {
897*e5dd7070Spatrick    f();
898*e5dd7070Spatrick  }
899*e5dd7070Spatrick  catch (...) {
900*e5dd7070Spatrick    i=2; // warn
901*e5dd7070Spatrick  }
902*e5dd7070Spatrick};
903*e5dd7070Spatrick</pre></div>
904*e5dd7070Spatrick<div class="example"><pre>
905*e5dd7070Spatrickvoid f() { throw 1; }
906*e5dd7070Spatrick
907*e5dd7070Spatrickclass Base {
908*e5dd7070Spatrickpublic:
909*e5dd7070Spatrick  int i;
910*e5dd7070Spatrick};
911*e5dd7070Spatrick
912*e5dd7070Spatrickclass C: public Base {
913*e5dd7070Spatrickpublic :
914*e5dd7070Spatrick  ~C() try {
915*e5dd7070Spatrick    f();
916*e5dd7070Spatrick  }
917*e5dd7070Spatrick  catch (...) {
918*e5dd7070Spatrick    i=2; // warn
919*e5dd7070Spatrick  }
920*e5dd7070Spatrick};
921*e5dd7070Spatrick</pre></div></div></td>
922*e5dd7070Spatrick<td class="aligned"></td></tr>
923*e5dd7070Spatrick
924*e5dd7070Spatrick
925*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
926*e5dd7070Spatrickundefbehavior.ReturnAtCatchEnd</span><span class="lang">
927*e5dd7070Spatrick(C++)</span><div class="descr">
928*e5dd7070SpatrickUndefined behavior: a function returns when control reaches the end of a
929*e5dd7070Spatrickhandler. This results in undefined behavior in a value-returning function.
930*e5dd7070Spatrick<p>Source: C++11 15.3p10.</p></div></div></td>
931*e5dd7070Spatrick<td><div class="exampleContainer expandable">
932*e5dd7070Spatrick<div class="example"><pre>
933*e5dd7070Spatrickvoid f() { throw 1; }
934*e5dd7070Spatrick
935*e5dd7070Spatrickint test() try {
936*e5dd7070Spatrick  f();
937*e5dd7070Spatrick  return 1;
938*e5dd7070Spatrick}
939*e5dd7070Spatrickcatch(int) {
940*e5dd7070Spatrick} // warn
941*e5dd7070Spatrick</pre></div></div></td>
942*e5dd7070Spatrick<td class="aligned"></td></tr>
943*e5dd7070Spatrick
944*e5dd7070Spatrick
945*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
946*e5dd7070Spatrickundefbehavior.AutoptrsOwnSameObj</span><span class="lang">
947*e5dd7070Spatrick(C++03)</span><div class="descr">
948*e5dd7070SpatrickUndefined behavior: if more than one <code>auto_ptr</code> owns the same object
949*e5dd7070Spatrickat the same time the behavior of the program is undefined.
950*e5dd7070Spatrick<p>Source: C++03 20.4.5p3; C++11 <code>auto_ptr</code> is deprecated
951*e5dd7070Spatrick(D.10).</p></div></div></td>
952*e5dd7070Spatrick<td><div class="exampleContainer expandable">
953*e5dd7070Spatrick<div class="example"><pre>
954*e5dd7070Spatrick#include &lt;memory&gt;
955*e5dd7070Spatrick
956*e5dd7070Spatrickvoid test() {
957*e5dd7070Spatrick  int *data = new int;
958*e5dd7070Spatrick  std::auto_ptr&lt;int&gt; p(data);
959*e5dd7070Spatrick  std::auto_ptr&lt;int&gt; q(data); // warn
960*e5dd7070Spatrick}
961*e5dd7070Spatrick</pre></div></div></td>
962*e5dd7070Spatrick<td class="aligned"></td></tr>
963*e5dd7070Spatrick
964*e5dd7070Spatrick
965*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
966*e5dd7070Spatrickundefbehavior.BasicStringOutOfBound</span><span class="lang">
967*e5dd7070Spatrick(C++03)</span><div class="descr">
968*e5dd7070SpatrickUndefined behavior: out-of-bound <code>basic_string</code> access/modification.
969*e5dd7070Spatrick<br>Note: possibly an enhancement to <span class="name">
970*e5dd7070Spatrickalpha.security.ArrayBoundV2</span>.
971*e5dd7070Spatrick<p>Source: C++03 21.3.4p1; C++11 behavior is defined
972*e5dd7070Spatrick(21.4.5p2).</p></div></div></td>
973*e5dd7070Spatrick<td><div class="exampleContainer expandable">
974*e5dd7070Spatrick<div class="example"><pre>
975*e5dd7070Spatrick#include &lt;string&gt;
976*e5dd7070Spatrick
977*e5dd7070Spatrickvoid test() {
978*e5dd7070Spatrick  std::basic_string&lt;char&gt; s;
979*e5dd7070Spatrick  char c = s[10]; // warn
980*e5dd7070Spatrick}
981*e5dd7070Spatrick</pre></div>
982*e5dd7070Spatrick<div class="example"><pre>
983*e5dd7070Spatrick#include &lt;string&gt;
984*e5dd7070Spatrick
985*e5dd7070Spatrickvoid test() {
986*e5dd7070Spatrick  std::basic_string&lt;char&gt; s;
987*e5dd7070Spatrick  s[10] = 0; // warn
988*e5dd7070Spatrick}
989*e5dd7070Spatrick</pre></div></div></td>
990*e5dd7070Spatrick<td class="aligned"></td></tr>
991*e5dd7070Spatrick
992*e5dd7070Spatrick
993*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
994*e5dd7070Spatrickundefbehavior.EosDereference</span><span class="lang">
995*e5dd7070Spatrick(C++)</span><div class="descr">
996*e5dd7070SpatrickUndefined behavior: the result of <code>operator*()</code> on an end of a
997*e5dd7070Spatrickstream is undefined.
998*e5dd7070Spatrick<p>Source: C++03 24.5.3p2; C++11 24.6.3p2.</p></div></div></td>
999*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1000*e5dd7070Spatrick<div class="example"><pre>
1001*e5dd7070Spatrick#include &lt;vector&gt;
1002*e5dd7070Spatrick
1003*e5dd7070Spatrickint test() {
1004*e5dd7070Spatrick  std::vector&lt;int&gt; v;
1005*e5dd7070Spatrick  return *v.end(); // warn
1006*e5dd7070Spatrick}
1007*e5dd7070Spatrick</pre></div></div></td>
1008*e5dd7070Spatrick<td class="aligned"></td></tr>
1009*e5dd7070Spatrick
1010*e5dd7070Spatrick
1011*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1012*e5dd7070Spatrickundefbehavior.QsortNonPODNonTrivial</span><span class="lang">
1013*e5dd7070Spatrick(C++)</span><div class="descr">
1014*e5dd7070SpatrickC++03: Undefined behavior: the objects in the array passed to qsort are of
1015*e5dd7070Spatricknon-POD type.<br>
1016*e5dd7070SpatrickC++11: Undefined behavior: the objects in the array passed to qsort are of
1017*e5dd7070Spatricknon-trivial type.
1018*e5dd7070Spatrick<p>Source: C++03 25.4p4; C++11 25.5p4.</p></div></div></td>
1019*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1020*e5dd7070Spatrick<div class="example"><pre>
1021*e5dd7070Spatrick// C++03
1022*e5dd7070Spatrick#include &lt;cstdlib&gt;
1023*e5dd7070Spatrick
1024*e5dd7070Spatrick
1025*e5dd7070Spatrickstruct non_POD {
1026*e5dd7070Spatrick  non_POD();
1027*e5dd7070Spatrick};
1028*e5dd7070Spatrick
1029*e5dd7070Spatricknon_POD values[] = { non_POD(), non_POD() };
1030*e5dd7070Spatrick
1031*e5dd7070Spatrickint compare(const void *a, const void *b);
1032*e5dd7070Spatrick
1033*e5dd7070Spatrickvoid test() {
1034*e5dd7070Spatrick  qsort(values, 2, sizeof(non_POD), compare); // warn
1035*e5dd7070Spatrick}
1036*e5dd7070Spatrick</pre></div>
1037*e5dd7070Spatrick<div class="example"><pre>
1038*e5dd7070Spatrick// C++11
1039*e5dd7070Spatrick#include &lt;cstdlib&gt;
1040*e5dd7070Spatrick
1041*e5dd7070Spatrickstruct S {};
1042*e5dd7070Spatrick
1043*e5dd7070Spatrickstruct trivial_non_POD : public S {
1044*e5dd7070Spatrick  int i;
1045*e5dd7070Spatrick};
1046*e5dd7070Spatrick
1047*e5dd7070Spatrickstruct non_trivial {
1048*e5dd7070Spatrick  int i;
1049*e5dd7070Spatrick  non_trivial();
1050*e5dd7070Spatrick};
1051*e5dd7070Spatrick
1052*e5dd7070Spatricktrivial_non_POD tnp[2];
1053*e5dd7070Spatricknon_trivial nt[2];
1054*e5dd7070Spatrick
1055*e5dd7070Spatrickint compare1(const void *a, const void *b);
1056*e5dd7070Spatrick
1057*e5dd7070Spatrickint compare2(const void *a, const void *b);
1058*e5dd7070Spatrick
1059*e5dd7070Spatrickvoid test() {
1060*e5dd7070Spatrick  qsort(tnp, 2, sizeof(trivial_non_POD), compare1); // ok
1061*e5dd7070Spatrick  qsort(nt, 2, sizeof(non_trivial), compare2); // warn
1062*e5dd7070Spatrick}
1063*e5dd7070Spatrick</pre></div></div></td>
1064*e5dd7070Spatrick<td class="aligned"></td></tr>
1065*e5dd7070Spatrick
1066*e5dd7070Spatrick
1067*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1068*e5dd7070Spatrickundefbehavior.ThrowWhileCopy</span><span class="lang">
1069*e5dd7070Spatrick(C++)</span><div class="descr">
1070*e5dd7070SpatrickUndefined behavior: copy constructor/assignment operator can throw an exception.
1071*e5dd7070SpatrickThe effects are undefined if an exception is thrown.</div></div></td>
1072*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1073*e5dd7070Spatrick<div class="example"><pre>
1074*e5dd7070Spatrickclass C {
1075*e5dd7070Spatrickpublic:
1076*e5dd7070Spatrick  int i, j;
1077*e5dd7070Spatrick  C (const C &amp;c) {
1078*e5dd7070Spatrick    i = c.i;
1079*e5dd7070Spatrick    throw 1; // warn
1080*e5dd7070Spatrick    j = c.j;
1081*e5dd7070Spatrick  };
1082*e5dd7070Spatrick};
1083*e5dd7070Spatrick</pre></div>
1084*e5dd7070Spatrick<div class="example"><pre>
1085*e5dd7070Spatrickclass C {
1086*e5dd7070Spatrickpublic:
1087*e5dd7070Spatrick  int i, j;
1088*e5dd7070Spatrick  C &amp;operator=(const C &amp;c) {
1089*e5dd7070Spatrick    i = c.i;
1090*e5dd7070Spatrick    throw 1; // warn
1091*e5dd7070Spatrick    j = c.j;
1092*e5dd7070Spatrick  };
1093*e5dd7070Spatrick};
1094*e5dd7070Spatrick</pre></div></div></td>
1095*e5dd7070Spatrick<td class="aligned"></td></tr>
1096*e5dd7070Spatrick
1097*e5dd7070Spatrick
1098*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1099*e5dd7070Spatrickundefbehavior.ValarrayArgBound</span><span class="lang">
1100*e5dd7070Spatrick(C++)</span><div class="descr">
1101*e5dd7070SpatrickUndefined behavior: the value of the <code><i>n</i></code> argument passed
1102*e5dd7070Spatrickto <code>valarray</code> constructor is greater than the number of values
1103*e5dd7070Spatrickpointed to by the first argument (source).
1104*e5dd7070Spatrick<p>Source: C++03 26.3.2.1p4; C++11 26.6.2.2p4.</p></div></div></td>
1105*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1106*e5dd7070Spatrick<div class="example"><pre>
1107*e5dd7070Spatrick#include &lt;valarray&gt;
1108*e5dd7070Spatrick
1109*e5dd7070Spatrickstruct S {
1110*e5dd7070Spatrick  int i;
1111*e5dd7070Spatrick  S(int ii) : i(ii) {};
1112*e5dd7070Spatrick};
1113*e5dd7070Spatrick
1114*e5dd7070Spatrickvoid test(void) {
1115*e5dd7070Spatrick  S s[] = { S(1), S(2) };
1116*e5dd7070Spatrick  std::valarray&lt;S&gt; v(s,3); // warn
1117*e5dd7070Spatrick}
1118*e5dd7070Spatrick</pre></div></div></td>
1119*e5dd7070Spatrick<td class="aligned"></td></tr>
1120*e5dd7070Spatrick
1121*e5dd7070Spatrick
1122*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1123*e5dd7070Spatrickundefbehavior.ValarrayLengthDiffer</span><span class="lang">
1124*e5dd7070Spatrick(C++)</span><div class="descr">
1125*e5dd7070SpatrickUndefined behavior: <code>valarray</code> operands are of different length.
1126*e5dd7070Spatrick<p>Source: C++03 26.3.2.2p1, 26.3.2.6p3, 26.3.3.1p3, 26.3.3.2p3;
1127*e5dd7070SpatrickC++11 defined (26.6.2.3p1), 26.6.2.7p3, 26.6.3.1p3,
1128*e5dd7070Spatrick26.6.3.2p3.</p></div></div></td>
1129*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1130*e5dd7070Spatrick<div class="example"><pre>
1131*e5dd7070Spatrick// C++03
1132*e5dd7070Spatrick#include &lt;valarray&gt;
1133*e5dd7070Spatrick
1134*e5dd7070Spatrickvoid test(void) {
1135*e5dd7070Spatrick  std::valarray&lt;int&gt; a(0, 1), b(0, 2);
1136*e5dd7070Spatrick  a = b; // warn
1137*e5dd7070Spatrick  b.resize(1);
1138*e5dd7070Spatrick  a = b; // ok
1139*e5dd7070Spatrick}
1140*e5dd7070Spatrick</pre></div>
1141*e5dd7070Spatrick<div class="example"><pre>
1142*e5dd7070Spatrick// C++03, C++11
1143*e5dd7070Spatrick#include &lt;valarray&gt;
1144*e5dd7070Spatrick
1145*e5dd7070Spatrickvoid test(void) {
1146*e5dd7070Spatrick  std::valarray&lt;int&gt; a(0, 1), b(0, 2);
1147*e5dd7070Spatrick  a *= b; // warn
1148*e5dd7070Spatrick}
1149*e5dd7070Spatrick</pre></div>
1150*e5dd7070Spatrick<div class="example"><pre>
1151*e5dd7070Spatrick// C++03, C++11
1152*e5dd7070Spatrick#include &lt;valarray&gt;
1153*e5dd7070Spatrick
1154*e5dd7070Spatrickvoid test(void) {
1155*e5dd7070Spatrick  std::valarray&lt;int&gt; a(0, 1), b(0, 2);
1156*e5dd7070Spatrick  a = a + b; // warn
1157*e5dd7070Spatrick}
1158*e5dd7070Spatrick</pre></div>
1159*e5dd7070Spatrick<div class="example"><pre>
1160*e5dd7070Spatrick// C++03, C++11
1161*e5dd7070Spatrick#include &lt;valarray&gt;
1162*e5dd7070Spatrick
1163*e5dd7070Spatrickvoid test(void) {
1164*e5dd7070Spatrick  std::valarray&lt;int&gt; a(0, 1), b(0, 2);
1165*e5dd7070Spatrick  std::valarray&lt;bool&gt; c(false, 1);
1166*e5dd7070Spatrick  c = a == b; // warn
1167*e5dd7070Spatrick}
1168*e5dd7070Spatrick</pre></div></div></td>
1169*e5dd7070Spatrick<td class="aligned"></td></tr>
1170*e5dd7070Spatrick
1171*e5dd7070Spatrick
1172*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1173*e5dd7070Spatrickundefbehavior.ValarrayZeroLength</span><span class="lang">
1174*e5dd7070Spatrick(C++)</span><div class="descr">
1175*e5dd7070SpatrickUndefined behavior: calling <code>sum()</code>/<code>min()</code>/<code>
1176*e5dd7070Spatrickmax()</code> methods of a zero length <code>valarray<code> the behavior is
1177*e5dd7070Spatrickundefined.
1178*e5dd7070Spatrick<p>Source: C++03 26.3.2.7p2, p3, p4; C++11 26.6.2.8p5, p6,
1179*e5dd7070Spatrickp7.</p></div></div></td>
1180*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1181*e5dd7070Spatrick<div class="example"><pre>
1182*e5dd7070Spatrick#include &lt;valarray&gt;
1183*e5dd7070Spatrick
1184*e5dd7070Spatrickvoid test(void) {
1185*e5dd7070Spatrick  std::valarray&lt;int&gt; v(0, 0);
1186*e5dd7070Spatrick  v.sum(); // warn
1187*e5dd7070Spatrick}
1188*e5dd7070Spatrick</pre></div></div></td>
1189*e5dd7070Spatrick<td class="aligned"></td></tr>
1190*e5dd7070Spatrick
1191*e5dd7070Spatrick
1192*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1193*e5dd7070Spatrickundefbehavior.ValarrayBadIndirection</span><span class="lang">
1194*e5dd7070Spatrick(C++)</span><div class="descr">
1195*e5dd7070SpatrickUndefined behavior: element is specified more than once in an indirection.
1196*e5dd7070Spatrick<p>Source: C++03 26.3.9.2p2, 26.3.9.3p2; C++11 26.6.9.2p2,
1197*e5dd7070Spatrick26.6.9.3p2.</p></div></div></td>
1198*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1199*e5dd7070Spatrick<div class="example"><pre>
1200*e5dd7070Spatrick#include &lt;valarray&gt;
1201*e5dd7070Spatrick
1202*e5dd7070Spatrickvoid test() {
1203*e5dd7070Spatrick  // '1' is specified more then once
1204*e5dd7070Spatrick  size_t addr[] = {0, 1, 1};
1205*e5dd7070Spatrick  std::valarray&lt;size_t&gt;indirect(addr, 3);
1206*e5dd7070Spatrick  std::valarray&lt;int&gt; a(0, 5), b(1, 3);
1207*e5dd7070Spatrick  a[indirect] = b; //warn
1208*e5dd7070Spatrick}
1209*e5dd7070Spatrick</pre></div>
1210*e5dd7070Spatrick<div class="example"><pre>
1211*e5dd7070Spatrick#include &lt;valarray&gt;
1212*e5dd7070Spatrick
1213*e5dd7070Spatrickvoid test() {
1214*e5dd7070Spatrick  // '1' is specified more then once
1215*e5dd7070Spatrick  size_t addr[] = {0, 1, 1};
1216*e5dd7070Spatrick  std::valarray&lt;size_t&gt;indirect(addr, 3);
1217*e5dd7070Spatrick  std::valarray&lt;int&gt; a(0, 5), b(1, 3);
1218*e5dd7070Spatrick  a[indirect] *= b; //warn
1219*e5dd7070Spatrick}
1220*e5dd7070Spatrick</pre></div></div></td>
1221*e5dd7070Spatrick<td class="aligned"></td></tr>
1222*e5dd7070Spatrick
1223*e5dd7070Spatrick
1224*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1225*e5dd7070Spatrickundefbehavior.IosBaseDestroyedBeforeInit</span><span class="lang">
1226*e5dd7070Spatrick(C++)</span><div class="descr">
1227*e5dd7070SpatrickUndefined behavior: <code>ios_base</code> object is destroyed before
1228*e5dd7070Spatrickinitialization have taken place. <code>basic_ios::init</code> should be call to
1229*e5dd7070Spatrickinitialize <code>ios_base</code> members.
1230*e5dd7070Spatrick<p>Source: C++03 27.4.2.7p1, 27.4.4.1p2; C++11 27.5.3.7p1,
1231*e5dd7070Spatrick27.5.5.2p2.</p></div></div></td>
1232*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1233*e5dd7070Spatrick<div class="example"><pre>
1234*e5dd7070Spatrick#include &lt;ios&gt;
1235*e5dd7070Spatrick
1236*e5dd7070Spatrickusing namespace std;
1237*e5dd7070Spatricktemplate &lt;class T, class Traits = std::char_traits&lt;T&gt; &gt;
1238*e5dd7070Spatrickclass my_stream1 : public std::basic_ios&lt;T, Traits&gt; {
1239*e5dd7070Spatrick};
1240*e5dd7070Spatrick
1241*e5dd7070Spatricktemplate &lt;class T, class Traits = std::char_traits&lt;T&gt; &gt;
1242*e5dd7070Spatrickclass my_stream2 : public std::basic_ios&lt;T, Traits&gt; {
1243*e5dd7070Spatrick  class my_streambuf
1244*e5dd7070Spatrick  : public std::basic_streambuf&lt;T, Traits&gt; {
1245*e5dd7070Spatrick  };
1246*e5dd7070Spatrickpublic:
1247*e5dd7070Spatrick  my_stream2() {
1248*e5dd7070Spatrick    this->init(new my_streambuf);
1249*e5dd7070Spatrick  }
1250*e5dd7070Spatrick};
1251*e5dd7070Spatrick
1252*e5dd7070Spatrickvoid test() {
1253*e5dd7070Spatrick  my_stream1&lt;char&gt; *p1 = new my_stream1&lt;char&gt;;
1254*e5dd7070Spatrick  my_stream2&lt;char&gt; *p2 = new my_stream2&lt;char&gt;;
1255*e5dd7070Spatrick  delete p1; // warn
1256*e5dd7070Spatrick  delete p2; // ok
1257*e5dd7070Spatrick}
1258*e5dd7070Spatrick</pre></div></div></td>
1259*e5dd7070Spatrick<td class="aligned"></td></tr>
1260*e5dd7070Spatrick
1261*e5dd7070Spatrick
1262*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1263*e5dd7070Spatrickundefbehavior.IosBaseUsedBeforeInit</span><span class="lang">
1264*e5dd7070Spatrick(C++11)</span><div class="descr">
1265*e5dd7070SpatrickUndefined behavior: <code>ios_base</code> object is used before initialization
1266*e5dd7070Spatrickhave taken place. <code>basic_ios::init</code> should be call to
1267*e5dd7070Spatrickinitialize <code>ios_base</code> members.
1268*e5dd7070Spatrick<p>Source: C++11 27.5.3.7p1, 27.5.5.2p2.</p></div></div></td>
1269*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1270*e5dd7070Spatrick<div class="example"><pre>
1271*e5dd7070Spatrick#include &lt;ios&gt;
1272*e5dd7070Spatrick
1273*e5dd7070Spatrickusing namespace std;
1274*e5dd7070Spatricktemplate &lt;class T, class Traits = std::char_traits&lt;T&gt; &gt;
1275*e5dd7070Spatrickclass my_stream1 : public std::basic_ios&lt;T, Traits&gt; {
1276*e5dd7070Spatrick};
1277*e5dd7070Spatrick
1278*e5dd7070Spatricktemplate &lt;class T, class Traits = std::char_traits&lt;T&gt; &gt;
1279*e5dd7070Spatrickclass my_stream2 : public std::basic_ios&lt;T, Traits&gt; {
1280*e5dd7070Spatrick  class my_streambuf
1281*e5dd7070Spatrick  : public std::basic_streambuf&lt;T, Traits&gt; {
1282*e5dd7070Spatrick  };
1283*e5dd7070Spatrickpublic:
1284*e5dd7070Spatrick  my_stream2() {
1285*e5dd7070Spatrick    this->init(new my_streambuf);
1286*e5dd7070Spatrick  }
1287*e5dd7070Spatrick};
1288*e5dd7070Spatrick
1289*e5dd7070Spatrickvoid test() {
1290*e5dd7070Spatrick  my_stream1&lt;char&gt; *p1 = new my_stream1&lt;char&gt;;
1291*e5dd7070Spatrick  my_stream2&lt;char&gt; *p2 = new my_stream2&lt;char&gt;;
1292*e5dd7070Spatrick  p1->narrow('a', 'b'); // warn
1293*e5dd7070Spatrick  p2->narrow('a', 'b'); // ok
1294*e5dd7070Spatrick}
1295*e5dd7070Spatrick</pre></div></div></td>
1296*e5dd7070Spatrick<td class="aligned"></td></tr>
1297*e5dd7070Spatrick
1298*e5dd7070Spatrick
1299*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1300*e5dd7070Spatrickundefbehavior.MinusOnePosType</span><span class="lang">
1301*e5dd7070Spatrick(C++)</span><div class="descr">
1302*e5dd7070SpatrickUndefined behavior: passing -1 to any <code>streambuf</code>/<code>
1303*e5dd7070Spatrickistream</code>/<code>ostream</code> member that accepts a value of
1304*e5dd7070Spatricktype <code>traits::pos_type</code> result in undefined behavior.
1305*e5dd7070Spatrick<p>Source: C++03 27.4.3.2p3; C++11 27.5.4.2p3.</p></div></div></td>
1306*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1307*e5dd7070Spatrick<div class="example"><pre>
1308*e5dd7070Spatrick#include &lt;fstream&gt;
1309*e5dd7070Spatrick
1310*e5dd7070Spatrickclass my_streambuf : public std::streambuf {
1311*e5dd7070Spatrick  void f() {
1312*e5dd7070Spatrick    seekpos(-1); // warn
1313*e5dd7070Spatrick  }
1314*e5dd7070Spatrick};
1315*e5dd7070Spatrick</pre></div>
1316*e5dd7070Spatrick<div class="example"><pre>
1317*e5dd7070Spatrick#include &lt;fstream&gt;
1318*e5dd7070Spatrick
1319*e5dd7070Spatrickvoid test() {
1320*e5dd7070Spatrick  std::filebuf fb;
1321*e5dd7070Spatrick  std::istream in(&amp;fb);
1322*e5dd7070Spatrick  std::filebuf::off_type pos(-1);
1323*e5dd7070Spatrick  in.seekg(pos); // warn
1324*e5dd7070Spatrick}
1325*e5dd7070Spatrick</pre></div></div></td>
1326*e5dd7070Spatrick<td class="aligned"></td></tr>
1327*e5dd7070Spatrick
1328*e5dd7070Spatrick</table>
1329*e5dd7070Spatrick
1330*e5dd7070Spatrick<!-- ============================ different ================================ -->
1331*e5dd7070Spatrick<h3>different</h3>
1332*e5dd7070Spatrick<table class="checkers">
1333*e5dd7070Spatrick<col class="namedescr"><col class="example"><col class="progress">
1334*e5dd7070Spatrick<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr>
1335*e5dd7070Spatrick</thead>
1336*e5dd7070Spatrick
1337*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1338*e5dd7070Spatrickdifferent.SuccessiveAssign</span><span class="lang">
1339*e5dd7070Spatrick(C)</span><div class="descr">
1340*e5dd7070SpatrickSuccessive assign to a variable.</div></div></td>
1341*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1342*e5dd7070Spatrick<div class="example"><pre>
1343*e5dd7070Spatrickint test() {
1344*e5dd7070Spatrick  int i;
1345*e5dd7070Spatrick  i=1;
1346*e5dd7070Spatrick  i=2; // warn
1347*e5dd7070Spatrick  return i;
1348*e5dd7070Spatrick}
1349*e5dd7070Spatrick</pre></div></div></td>
1350*e5dd7070Spatrick<td class="aligned"></td></tr>
1351*e5dd7070Spatrick
1352*e5dd7070Spatrick
1353*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1354*e5dd7070Spatrickdifferent.NullDerefStmtOrder</span><span class="lang">
1355*e5dd7070Spatrick(C)</span><div class="descr">
1356*e5dd7070SpatrickDereferencing of the null pointer might take place. Checking the pointer for
1357*e5dd7070Spatricknull should be performed first.
1358*e5dd7070Spatrick<br>Note: possibly an enhancement to <span class="name">
1359*e5dd7070Spatrickcore.NullDereference</span>.</div></div></td>
1360*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1361*e5dd7070Spatrick<div class="example"><pre>
1362*e5dd7070Spatrickstruct S {
1363*e5dd7070Spatrick  int x;
1364*e5dd7070Spatrick};
1365*e5dd7070Spatrick
1366*e5dd7070Spatrickstruct S* f();
1367*e5dd7070Spatrick
1368*e5dd7070Spatrickvoid test() {
1369*e5dd7070Spatrick  struct S *p1 = f();
1370*e5dd7070Spatrick  int x1 = p1-&gt;x; // warn
1371*e5dd7070Spatrick  if (p1) {};
1372*e5dd7070Spatrick
1373*e5dd7070Spatrick  struct S *p2 = f();
1374*e5dd7070Spatrick  int x2 = p2-&gt;x; // ok
1375*e5dd7070Spatrick}
1376*e5dd7070Spatrick</pre></div></div></td>
1377*e5dd7070Spatrick<td class="aligned"></td></tr>
1378*e5dd7070Spatrick
1379*e5dd7070Spatrick
1380*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1381*e5dd7070Spatrickdifferent.NullDerefCondOrder</span><span class="lang">
1382*e5dd7070Spatrick(C)</span><div class="descr">
1383*e5dd7070SpatrickDereferencing of the null pointer might take place. Checking the pointer for
1384*e5dd7070Spatricknull should be performed first.
1385*e5dd7070Spatrick<br>Note: possibly an enhancement to <span class="name">
1386*e5dd7070Spatrickcore.NullDereference</span>.</div></div></td>
1387*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1388*e5dd7070Spatrick<div class="example"><pre>
1389*e5dd7070Spatrickstruct S {int i;};
1390*e5dd7070Spatrick
1391*e5dd7070Spatrickstruct S* f();
1392*e5dd7070Spatrick
1393*e5dd7070Spatrickvoid test() {
1394*e5dd7070Spatrick  struct S *p = f();
1395*e5dd7070Spatrick  if (p-&gt;i && p) {}; // warn
1396*e5dd7070Spatrick}
1397*e5dd7070Spatrick</pre></div></div></td>
1398*e5dd7070Spatrick<td class="aligned"></td></tr>
1399*e5dd7070Spatrick
1400*e5dd7070Spatrick
1401*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1402*e5dd7070Spatrickdifferent.MultipleAccessors</span><span class="lang">
1403*e5dd7070Spatrick(C++)</span><div class="descr">
1404*e5dd7070SpatrickIdentical accessor bodies. Possibly a misprint.</div></div></td>
1405*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1406*e5dd7070Spatrick<div class="example"><pre>
1407*e5dd7070Spatrickclass A {
1408*e5dd7070Spatrick  int i;
1409*e5dd7070Spatrick  int j;
1410*e5dd7070Spatrickpublic:
1411*e5dd7070Spatrick  int getI() { return i; }
1412*e5dd7070Spatrick  int getJ() { return i; } // warn
1413*e5dd7070Spatrick};
1414*e5dd7070Spatrick</pre></div>
1415*e5dd7070Spatrick<div class="example"><pre>
1416*e5dd7070Spatrickclass A {
1417*e5dd7070Spatrick  int i;
1418*e5dd7070Spatrick  int j;
1419*e5dd7070Spatrickpublic:
1420*e5dd7070Spatrick  void setI(int& ii) { i = ii; }
1421*e5dd7070Spatrick  void setJ(int& jj) { i = jj; } // warn
1422*e5dd7070Spatrick};
1423*e5dd7070Spatrick</pre></div></div></td>
1424*e5dd7070Spatrick<td class="aligned"></td></tr>
1425*e5dd7070Spatrick
1426*e5dd7070Spatrick
1427*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1428*e5dd7070Spatrickdifferent.AccessorsForPublic</span><span class="lang">
1429*e5dd7070Spatrick(C++)</span><div class="descr">
1430*e5dd7070SpatrickAccessors exist for a public class field. Should this field really be
1431*e5dd7070Spatrickpublic?</div></div></td>
1432*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1433*e5dd7070Spatrick<div class="example"><pre>
1434*e5dd7070Spatrickclass A {
1435*e5dd7070Spatrickpublic:
1436*e5dd7070Spatrick  int i; // warn
1437*e5dd7070Spatrick  int getI() { return i; }
1438*e5dd7070Spatrick  void setI(int& ii) { i = ii; }
1439*e5dd7070Spatrick};
1440*e5dd7070Spatrick</pre></div></div></td>
1441*e5dd7070Spatrick<td class="aligned"></td></tr>
1442*e5dd7070Spatrick
1443*e5dd7070Spatrick
1444*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1445*e5dd7070Spatrickdifferent.LibFuncResultUnised</span><span class="lang">
1446*e5dd7070Spatrick(C, C++)</span><div class="descr">
1447*e5dd7070SpatrickCalling a function ignoring its return value is of no use (create the list of
1448*e5dd7070Spatrickknown system/library/API functions falling into this category).</div></div></td>
1449*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1450*e5dd7070Spatrick<div class="example"><pre>
1451*e5dd7070Spatrick#include &lt;vector&gt;
1452*e5dd7070Spatrick
1453*e5dd7070Spatrickvoid test() {
1454*e5dd7070Spatrick  std::vector&lt;int&gt; v;
1455*e5dd7070Spatrick  v.empty(); // warn
1456*e5dd7070Spatrick}
1457*e5dd7070Spatrick</pre></div></div></td>
1458*e5dd7070Spatrick<td class="aligned"></td></tr>
1459*e5dd7070Spatrick
1460*e5dd7070Spatrick
1461*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1462*e5dd7070Spatrickdifferent.WrongVarForStmt</span><span class="lang">
1463*e5dd7070Spatrick(C, C++)</span><div class="descr">
1464*e5dd7070SpatrickWrong variable is possibly used in the loop/cond-expression of
1465*e5dd7070Spatrickthe <code>for</code> statement. Did you mean
1466*e5dd7070Spatrick'proper_variable_name'?</div></div></td>
1467*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1468*e5dd7070Spatrick<div class="example"><pre>
1469*e5dd7070Spatrickvoid test() {
1470*e5dd7070Spatrick  int i = 0;
1471*e5dd7070Spatrick  int j = 0;
1472*e5dd7070Spatrick  for (i = 0; i < 3; j += 1); // warn
1473*e5dd7070Spatrick}
1474*e5dd7070Spatrick</pre></div>
1475*e5dd7070Spatrick<div class="example"><pre>
1476*e5dd7070Spatrickvoid test() {
1477*e5dd7070Spatrick  int i = 0;
1478*e5dd7070Spatrick  int j = 0;
1479*e5dd7070Spatrick  for (int j = 0; i < 3; ++j); // warn
1480*e5dd7070Spatrick}
1481*e5dd7070Spatrick</pre></div></div></td>
1482*e5dd7070Spatrick<td class="aligned"></td></tr>
1483*e5dd7070Spatrick
1484*e5dd7070Spatrick
1485*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1486*e5dd7070Spatrickdifferent.FloatingCompare</span><span class="lang">
1487*e5dd7070Spatrick(C)</span><div class="descr">
1488*e5dd7070SpatrickComparing floating point numbers may be not precise.</div></div></td>
1489*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1490*e5dd7070Spatrick<div class="example"><pre>
1491*e5dd7070Spatrick#include &lt;math.h&gt;
1492*e5dd7070Spatrick
1493*e5dd7070Spatrickdouble test() {
1494*e5dd7070Spatrick  double b = sin(M_PI / 6.0);
1495*e5dd7070Spatrick  if (b == 0.5) // warn
1496*e5dd7070Spatrick    b = 0;
1497*e5dd7070Spatrick  return b;
1498*e5dd7070Spatrick}
1499*e5dd7070Spatrick</pre></div></div></td>
1500*e5dd7070Spatrick<td class="aligned"></td></tr>
1501*e5dd7070Spatrick
1502*e5dd7070Spatrick
1503*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1504*e5dd7070Spatrickdifferent.BitwiseOpBoolArg</span><span class="lang">
1505*e5dd7070Spatrick(C, C++)</span><div class="descr">
1506*e5dd7070SpatrickBoolean value met at the left/right part of the bitwise <code>&amp;</code>
1507*e5dd7070Spatrickor <code>|</code> operator.
1508*e5dd7070SpatrickDid you mean <code>&amp;&amp;</code> (<code>||</code>) ?</div></div></td>
1509*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1510*e5dd7070Spatrick<div class="example"><pre>
1511*e5dd7070Spatrickint f();
1512*e5dd7070Spatrick
1513*e5dd7070Spatrickvoid test() {
1514*e5dd7070Spatrick  bool b = true;
1515*e5dd7070Spatrick  if (b &amp; f()) {} // warn
1516*e5dd7070Spatrick}
1517*e5dd7070Spatrick</pre></div></div></td>
1518*e5dd7070Spatrick<td class="aligned"></td></tr>
1519*e5dd7070Spatrick
1520*e5dd7070Spatrick
1521*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1522*e5dd7070Spatrickdifferent.LabelInsideSwitch</span><span class="lang">
1523*e5dd7070Spatrick(C)</span><div class="descr">
1524*e5dd7070SpatrickPossibly a misprint: label found inside a <code>switch()</code>
1525*e5dd7070Spatrickstatement.</div></div></td>
1526*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1527*e5dd7070Spatrick<div class="example"><pre>
1528*e5dd7070Spatrickvoid test(int c) {
1529*e5dd7070Spatrick  switch(c){
1530*e5dd7070Spatrick  case 1:
1531*e5dd7070Spatrick    c += 1; break;
1532*e5dd7070Spatrick  defalt: // warn (did you mean 'default'?)
1533*e5dd7070Spatrick    c -= 1; break;
1534*e5dd7070Spatrick  }
1535*e5dd7070Spatrick}
1536*e5dd7070Spatrick</pre></div></div></td>
1537*e5dd7070Spatrick<td class="aligned"></td></tr>
1538*e5dd7070Spatrick
1539*e5dd7070Spatrick
1540*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1541*e5dd7070Spatrickdifferent.IdenticalCondIfIf</span><span class="lang">
1542*e5dd7070Spatrick(C)</span><div class="descr">
1543*e5dd7070SpatrickThe conditions of two subsequent <code>if</code> statements are
1544*e5dd7070Spatrickidentical.</div></div></td>
1545*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1546*e5dd7070Spatrick<div class="example"><pre>
1547*e5dd7070Spatrickint test(int c) {
1548*e5dd7070Spatrick  if (c &gt; 5)
1549*e5dd7070Spatrick    c += 1;
1550*e5dd7070Spatrick  if (c &gt; 5) // warn
1551*e5dd7070Spatrick    c -= 1;
1552*e5dd7070Spatrick  return c;
1553*e5dd7070Spatrick}
1554*e5dd7070Spatrick</pre></div></div></td>
1555*e5dd7070Spatrick<td class="aligned"></td></tr>
1556*e5dd7070Spatrick
1557*e5dd7070Spatrick
1558*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1559*e5dd7070Spatrickdifferent.LogicalOpUselessArg</span><span class="lang">
1560*e5dd7070Spatrick(C)</span><div class="descr">
1561*e5dd7070SpatrickThe second operand of a <code>&amp;&amp;</code> operator has no impact on
1562*e5dd7070Spatrickexpression result.</div></div></td>
1563*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1564*e5dd7070Spatrick<div class="example"><pre>
1565*e5dd7070Spatrickvoid test(unsigned a) {
1566*e5dd7070Spatrick  if (a&lt;7 &amp;&amp; a&lt;10) {}; // warn
1567*e5dd7070Spatrick}
1568*e5dd7070Spatrick</pre></div></div></td>
1569*e5dd7070Spatrick<td class="aligned"></td></tr>
1570*e5dd7070Spatrick
1571*e5dd7070Spatrick
1572*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1573*e5dd7070Spatrickdifferent.SameResLogicalExpr</span><span class="lang">
1574*e5dd7070Spatrick(C)</span><div class="descr">
1575*e5dd7070SpatrickAn expression is always evaluated to true/false.</div></div></td>
1576*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1577*e5dd7070Spatrick<div class="example"><pre>
1578*e5dd7070Spatrickvoid test() {
1579*e5dd7070Spatrick  int i = 0;
1580*e5dd7070Spatrick  if (i != 0) {}; // warn
1581*e5dd7070Spatrick}
1582*e5dd7070Spatrick</pre></div>
1583*e5dd7070Spatrick<div class="example"><pre>
1584*e5dd7070Spatrickvoid test(int i) {
1585*e5dd7070Spatrick  if (i == 0 &amp;&amp; i == 1) {}; // warn
1586*e5dd7070Spatrick}
1587*e5dd7070Spatrick</pre></div>
1588*e5dd7070Spatrick<div class="example"><pre>
1589*e5dd7070Spatrickvoid test(int i) {
1590*e5dd7070Spatrick  if (i < 0 || i >= 0) {}; // warn
1591*e5dd7070Spatrick}
1592*e5dd7070Spatrick</pre></div></div></td>
1593*e5dd7070Spatrick<td class="aligned"></td></tr>
1594*e5dd7070Spatrick
1595*e5dd7070Spatrick
1596*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1597*e5dd7070Spatrickdifferent.OpPrecedenceAssignCmp</span><span class="lang">
1598*e5dd7070Spatrick(C, C++)</span><div class="descr">
1599*e5dd7070SpatrickComparison operation has higher precedence then assignment. Boolean value is
1600*e5dd7070Spatrickassigned to a variable of other type. Parenthesis may bee required around an
1601*e5dd7070Spatrickassignment.</div></div></td>
1602*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1603*e5dd7070Spatrick<div class="example"><pre>
1604*e5dd7070Spatrickint f();
1605*e5dd7070Spatrick
1606*e5dd7070Spatrickvoid test(int x, int y) {
1607*e5dd7070Spatrick  bool b;
1608*e5dd7070Spatrick  if((b = x != y)) {} // ok
1609*e5dd7070Spatrick  if((x = f() != y)) {} // warn
1610*e5dd7070Spatrick}
1611*e5dd7070Spatrick</pre></div></div></td>
1612*e5dd7070Spatrick<td class="aligned"></td></tr>
1613*e5dd7070Spatrick
1614*e5dd7070Spatrick
1615*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1616*e5dd7070Spatrickdifferent.OpPrecedenceIifShift</span><span class="lang">
1617*e5dd7070Spatrick(C, C++)</span><div class="descr">
1618*e5dd7070Spatrick<code>?:</code> has lower precedence then <code>&lt;&lt;</code>.
1619*e5dd7070Spatrick<p>Source: Stephen C. Dewhurst "C++ Gotchas: Avoiding Common Problems in Coding
1620*e5dd7070Spatrickand Design", advise 15.</p></div></div></td>
1621*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1622*e5dd7070Spatrick<div class="example"><pre>
1623*e5dd7070Spatrick#include &lt;iostream&gt;
1624*e5dd7070Spatrick
1625*e5dd7070Spatrickvoid test(int a) {
1626*e5dd7070Spatrick  std::cout &lt;&lt; a ? "a" : "b"; // warn
1627*e5dd7070Spatrick}
1628*e5dd7070Spatrick</pre></div>
1629*e5dd7070Spatrick<div class="example"><pre>
1630*e5dd7070Spatrickvoid test(int a) {
1631*e5dd7070Spatrick  a &lt;&lt; a &gt; 7 ? 1 : 2; // warn
1632*e5dd7070Spatrick}
1633*e5dd7070Spatrick</pre></div></div></td>
1634*e5dd7070Spatrick<td class="aligned"></td></tr>
1635*e5dd7070Spatrick
1636*e5dd7070Spatrick
1637*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1638*e5dd7070Spatrickdifferent.ObjectUnused</span><span class="lang">
1639*e5dd7070Spatrick(C++)</span><div class="descr">
1640*e5dd7070SpatrickThe object was created but is not being used.</div></div></td>
1641*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1642*e5dd7070Spatrick<div class="example"><pre>
1643*e5dd7070Spatrickstruct S {
1644*e5dd7070Spatrick  int x, y;
1645*e5dd7070Spatrick  S(int xx, int yy) : x(xx), y(yy) {}
1646*e5dd7070Spatrick  S(int xx) {
1647*e5dd7070Spatrick    S(xx, 0); // warn
1648*e5dd7070Spatrick  }
1649*e5dd7070Spatrick};
1650*e5dd7070Spatrick</pre></div>
1651*e5dd7070Spatrick<div class="example"><pre>
1652*e5dd7070Spatrick#include &lt;exception&gt;
1653*e5dd7070Spatrick
1654*e5dd7070Spatrickvoid test() {
1655*e5dd7070Spatrick  std::exception();
1656*e5dd7070Spatrick    // warn (did you mean 'throw std::exception()'?)
1657*e5dd7070Spatrick}
1658*e5dd7070Spatrick</pre></div></div></td>
1659*e5dd7070Spatrick<td class="aligned"></td></tr>
1660*e5dd7070Spatrick
1661*e5dd7070Spatrick
1662*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1663*e5dd7070Spatrickdifferent.StaticArrayPtrCompare</span><span class="lang">
1664*e5dd7070Spatrick(C)</span><div class="descr">
1665*e5dd7070SpatrickPointer to static array is being compared to NULL. May the subscripting is
1666*e5dd7070Spatrickmissing.</div></div></td>
1667*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1668*e5dd7070Spatrick<div class="example"><pre>
1669*e5dd7070Spatrickvoid test() {
1670*e5dd7070Spatrick  int a[1][1];
1671*e5dd7070Spatrick  if (a[0] == 0) {}; // warn
1672*e5dd7070Spatrick}
1673*e5dd7070Spatrick</pre></div></div></td>
1674*e5dd7070Spatrick<td class="aligned"></td></tr>
1675*e5dd7070Spatrick
1676*e5dd7070Spatrick
1677*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1678*e5dd7070Spatrickdifferent.ConversionToBool</span><span class="lang">
1679*e5dd7070Spatrick(C, C++)</span><div class="descr">
1680*e5dd7070SpatrickOdd implicit conversion to boolean.
1681*e5dd7070Spatrick<br>Note: possibly merge with <span class="name">
1682*e5dd7070Spatrickalpha.core.BoolAssignment</span>.</div></div></td>
1683*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1684*e5dd7070Spatrick<div class="example"><pre>
1685*e5dd7070Spatrickbool test() {
1686*e5dd7070Spatrick  return 1.; // warn
1687*e5dd7070Spatrick}
1688*e5dd7070Spatrick</pre></div>
1689*e5dd7070Spatrick<div class="example"><pre>
1690*e5dd7070Spatrickbool test() {
1691*e5dd7070Spatrick  return ""; // warn
1692*e5dd7070Spatrick}
1693*e5dd7070Spatrick</pre></div></div></td>
1694*e5dd7070Spatrick<td class="aligned"></td></tr>
1695*e5dd7070Spatrick
1696*e5dd7070Spatrick
1697*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1698*e5dd7070Spatrickdifferent.ArrayBound</span><span class="lang">
1699*e5dd7070Spatrick(C++)</span><div class="descr">
1700*e5dd7070SpatrickOut-of-bound dynamic array access.
1701*e5dd7070Spatrick<br>Note: possibly an enhancement to <span class="name">
1702*e5dd7070Spatrickalpha.security.ArrayBoundV2</span>.</div></div></td>
1703*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1704*e5dd7070Spatrick<div class="example"><pre>
1705*e5dd7070Spatrickvoid test() {
1706*e5dd7070Spatrick  int *p = new int[1];
1707*e5dd7070Spatrick  int i = 1;
1708*e5dd7070Spatrick  if(p[i]) {}; // warn
1709*e5dd7070Spatrick  delete[] p;
1710*e5dd7070Spatrick}
1711*e5dd7070Spatrick</pre></div></div></td>
1712*e5dd7070Spatrick<td class="aligned"></td></tr>
1713*e5dd7070Spatrick
1714*e5dd7070Spatrick
1715*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1716*e5dd7070Spatrickdifferent.StrcpyInputSize</span><span class="lang">
1717*e5dd7070Spatrick(C)</span><div class="descr">
1718*e5dd7070SpatrickBuffer copy without checking the size of input.
1719*e5dd7070Spatrick<br>Note: possibly an enhancement to <span class="name">
1720*e5dd7070Spatrickalpha.unix.cstring.OutOfBounds</span>.</div></div></td>
1721*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1722*e5dd7070Spatrick<div class="example"><pre>
1723*e5dd7070Spatrickvoid test(char* string) {
1724*e5dd7070Spatrick  char buf[24];
1725*e5dd7070Spatrick  strcpy(buf, string); // warn
1726*e5dd7070Spatrick}
1727*e5dd7070Spatrick</pre></div></div></td>
1728*e5dd7070Spatrick<td class="aligned"></td></tr>
1729*e5dd7070Spatrick
1730*e5dd7070Spatrick
1731*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1732*e5dd7070Spatrickdifferent.IntegerOverflow</span><span class="lang">
1733*e5dd7070Spatrick(C)</span><div class="descr">
1734*e5dd7070SpatrickInteger overflow.
1735*e5dd7070Spatrick<br>Note: partially handled by Clang core
1736*e5dd7070Spatrick(search for 'overflow in expression' warning in Clang tests).
1737*e5dd7070Spatrick<p>Source: <a href="https://cwe.mitre.org/data/definitions/190.html">
1738*e5dd7070SpatrickCWE-190</a>.</p></div></div></td>
1739*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1740*e5dd7070Spatrick<div class="example"><pre>
1741*e5dd7070Spatrick#include &lt;limits.h&gt;
1742*e5dd7070Spatrick
1743*e5dd7070Spatrickint f(int x);
1744*e5dd7070Spatrick
1745*e5dd7070Spatrickvoid test() {
1746*e5dd7070Spatrick  f(INT_MAX + 1); // warn
1747*e5dd7070Spatrick}
1748*e5dd7070Spatrick</pre></div>
1749*e5dd7070Spatrick<div class="example"><pre>
1750*e5dd7070Spatrick#include &lt;limits.h&gt;
1751*e5dd7070Spatrick
1752*e5dd7070Spatrickint test() {
1753*e5dd7070Spatrick  int x = INT_MAX / 2 + 1;
1754*e5dd7070Spatrick  return x * 2; // warn
1755*e5dd7070Spatrick}
1756*e5dd7070Spatrick</pre></div></div></td>
1757*e5dd7070Spatrick<td class="aligned"></td></tr>
1758*e5dd7070Spatrick
1759*e5dd7070Spatrick
1760*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1761*e5dd7070Spatrickdifferent.SignExtension</span><span class="lang">
1762*e5dd7070Spatrick(C)</span><div class="descr">
1763*e5dd7070SpatrickUnexpected sign extension might take place.
1764*e5dd7070Spatrick<p>Source: <a href="https://cwe.mitre.org/data/definitions/194.html">
1765*e5dd7070SpatrickCWE-194</a>.</p></div></div></td>
1766*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1767*e5dd7070Spatrick<div class="example"><pre>
1768*e5dd7070Spatrickunsigned long long test(long long sll) {
1769*e5dd7070Spatrick  unsigned long long ull = sll; // warn
1770*e5dd7070Spatrick  return ull;
1771*e5dd7070Spatrick}
1772*e5dd7070Spatrick</pre></div>
1773*e5dd7070Spatrick<div class="example"><pre>
1774*e5dd7070Spatrickvoid f(unsigned int i);
1775*e5dd7070Spatrick
1776*e5dd7070Spatrickvoid test(int si) {
1777*e5dd7070Spatrick  f(si); // warn
1778*e5dd7070Spatrick}
1779*e5dd7070Spatrick</pre></div>
1780*e5dd7070Spatrick<div class="example"><pre>
1781*e5dd7070Spatrickunsigned int test(int i) {
1782*e5dd7070Spatrick  return i;
1783*e5dd7070Spatrick}
1784*e5dd7070Spatrick</pre></div></div></td>
1785*e5dd7070Spatrick<td class="aligned"></td></tr>
1786*e5dd7070Spatrick
1787*e5dd7070Spatrick
1788*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1789*e5dd7070Spatrickdifferent.NumericTruncation</span><span class="lang">
1790*e5dd7070Spatrick(C)</span><div class="descr">
1791*e5dd7070SpatrickNumeric truncation might take place.
1792*e5dd7070Spatrick<p>Source: <a href="https://cwe.mitre.org/data/definitions/197.html">
1793*e5dd7070SpatrickCWE-197</a>.</p></div></div></td>
1794*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1795*e5dd7070Spatrick<div class="example"><pre>
1796*e5dd7070Spatrickunsigned long test(unsigned long long ull) {
1797*e5dd7070Spatrick  unsigned long ul = ull; // warn
1798*e5dd7070Spatrick  return ul;
1799*e5dd7070Spatrick}
1800*e5dd7070Spatrick</pre></div>
1801*e5dd7070Spatrick<div class="example"><pre>
1802*e5dd7070Spatrickvoid f(int i);
1803*e5dd7070Spatrick
1804*e5dd7070Spatrickvoid test(long long sll) {
1805*e5dd7070Spatrick  f(sll); // warn
1806*e5dd7070Spatrick}
1807*e5dd7070Spatrick</pre></div>
1808*e5dd7070Spatrick<div class="example"><pre>
1809*e5dd7070Spatrickint f();
1810*e5dd7070Spatrick
1811*e5dd7070Spatrickshort test(long long sll) {
1812*e5dd7070Spatrick  short ss = f();
1813*e5dd7070Spatrick  return ss;
1814*e5dd7070Spatrick}
1815*e5dd7070Spatrick</pre></div></div></td>
1816*e5dd7070Spatrick<td class="aligned"></td></tr>
1817*e5dd7070Spatrick
1818*e5dd7070Spatrick
1819*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1820*e5dd7070Spatrickdifferent.MissingCopyCtorAssignOp</span><span class="lang">
1821*e5dd7070Spatrick(C++)</span><div class="descr">
1822*e5dd7070SpatrickA class has dynamically allocated data members but do not define a copy
1823*e5dd7070Spatrickconstructor/assignment operator.
1824*e5dd7070Spatrick<p>Source: Scott Meyers "Effective C++", item 11: Prevent exceptions from
1825*e5dd7070Spatrickleaving destructors.</p></div></div></td>
1826*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1827*e5dd7070Spatrick<div class="example"><pre>
1828*e5dd7070Spatrickclass C {
1829*e5dd7070Spatrick  int *p; // warn
1830*e5dd7070Spatrickpublic:
1831*e5dd7070Spatrick  C() { p = new int; }
1832*e5dd7070Spatrick  ~C() { delete p; }
1833*e5dd7070Spatrick};
1834*e5dd7070Spatrick</pre></div></div></td>
1835*e5dd7070Spatrick<td class="aligned"></td></tr>
1836*e5dd7070Spatrick
1837*e5dd7070Spatrick</table>
1838*e5dd7070Spatrick
1839*e5dd7070Spatrick<!-- ============================ WinAPI =================================== -->
1840*e5dd7070Spatrick<h3>WinAPI</h3>
1841*e5dd7070Spatrick<table class="checkers">
1842*e5dd7070Spatrick<col class="namedescr"><col class="example"><col class="progress">
1843*e5dd7070Spatrick<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>
1844*e5dd7070Spatrick
1845*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1846*e5dd7070SpatrickWinAPI.CreateProcess</span><span class="lang">
1847*e5dd7070Spatrick(C)</span><div class="descr">
1848*e5dd7070Spatrick<code>CreateProcess()</code>: if the first parameter <code><i>
1849*e5dd7070SpatricklpApplicationName</i></code> is NULL then the executable name must be in the
1850*e5dd7070Spatrickwhite space-delimited string pointed to by <code><i>lpCommandLine</code></i>.
1851*e5dd7070SpatrickIf the executable or path name has a space in it, there is a risk that a
1852*e5dd7070Spatrickdifferent executable could be run because of the way the function parses
1853*e5dd7070Spatrickspaces.
1854*e5dd7070Spatrick<p>Source: <a href="https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#security-remarks">
1855*e5dd7070SpatrickMSDN: CreateProcess function, Security Remarks</a>.</p></div></div></td>
1856*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1857*e5dd7070Spatrick<div class="example"><pre>
1858*e5dd7070Spatrick#include &lt;windows.h&gt;
1859*e5dd7070Spatrick
1860*e5dd7070Spatrickvoid test() {
1861*e5dd7070Spatrick  STARTUPINFO si;
1862*e5dd7070Spatrick  PROCESS_INFORMATION pi;
1863*e5dd7070Spatrick  CreateProcess(NULL, TEXT("C:\\Program Files\\App -L -S"),
1864*e5dd7070Spatrick                NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
1865*e5dd7070Spatrick    // warn
1866*e5dd7070Spatrick}
1867*e5dd7070Spatrick</pre></div></div></td>
1868*e5dd7070Spatrick<td class="aligned"></td></tr>
1869*e5dd7070Spatrick
1870*e5dd7070Spatrick
1871*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1872*e5dd7070SpatrickWinAPI.LoadLibrary</span><span class="lang">
1873*e5dd7070Spatrick(C)</span><div class="descr">
1874*e5dd7070SpatrickThe <code>SearchPath()</code> function is used to retrieve a path to a DLL for
1875*e5dd7070Spatricka subsequent <code>LoadLibrary()</code> call.
1876*e5dd7070Spatrick<p>Source: <a href="https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya#security-remarks">
1877*e5dd7070SpatrickMSDN: LoadLibrary function, Security Remarks</a>.</p></div></div></td>
1878*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1879*e5dd7070Spatrick<div class="example"><pre>
1880*e5dd7070Spatrick#include &lt;windows.h&gt;
1881*e5dd7070Spatrick
1882*e5dd7070SpatrickHINSTANCE test() {
1883*e5dd7070Spatrick  char filePath[100];
1884*e5dd7070Spatrick  SearchPath(NULL, "file.dll", NULL, 100, filePath, NULL);
1885*e5dd7070Spatrick  return LoadLibrary(filePath); // warn
1886*e5dd7070Spatrick}
1887*e5dd7070Spatrick</pre></div></div></td>
1888*e5dd7070Spatrick<td class="aligned"></td></tr>
1889*e5dd7070Spatrick
1890*e5dd7070Spatrick
1891*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1892*e5dd7070SpatrickWinAPI.WideCharToMultiByte</span><span class="lang">
1893*e5dd7070Spatrick(C)</span><div class="descr">
1894*e5dd7070SpatrickBuffer overrun while calling <code>WideCharToMultiByte()</code>. The size of
1895*e5dd7070Spatrickthe input buffer equals the number of characters in the Unicode string, while
1896*e5dd7070Spatrickthe size of the output buffer equals the number of bytes.
1897*e5dd7070Spatrick<p>Source: <a href="https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte">
1898*e5dd7070SpatrickMSDN: WideCharToMultiByte function</a>.</p></div></div></td>
1899*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1900*e5dd7070Spatrick<div class="example"><pre>
1901*e5dd7070Spatrick#include &lt;windows.h&gt;
1902*e5dd7070Spatrick
1903*e5dd7070Spatrickvoid test() {
1904*e5dd7070Spatrick  wchar_t ws[] = L"abc";
1905*e5dd7070Spatrick  char s[3];
1906*e5dd7070Spatrick  WideCharToMultiByte(CP_UTF8, 0, ws, -1, s,
1907*e5dd7070Spatrick                      3, NULL, NULL); // warn
1908*e5dd7070Spatrick}
1909*e5dd7070Spatrick</pre></div></div></td>
1910*e5dd7070Spatrick<td class="aligned"></td></tr>
1911*e5dd7070Spatrick
1912*e5dd7070Spatrick
1913*e5dd7070Spatrick</table>
1914*e5dd7070Spatrick
1915*e5dd7070Spatrick<!-- =========================== optimization ============================== -->
1916*e5dd7070Spatrick<h3>optimization</h3>
1917*e5dd7070Spatrick<table class="checkers">
1918*e5dd7070Spatrick<col class="namedescr"><col class="example"><col class="progress">
1919*e5dd7070Spatrick<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>
1920*e5dd7070Spatrick
1921*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1922*e5dd7070Spatrickoptimization.PassConstObjByValue</span><span class="lang">
1923*e5dd7070Spatrick(C, C++)</span><div class="descr">
1924*e5dd7070SpatrickOptimization: It is more effective to pass constant parameter by reference to
1925*e5dd7070Spatrickavoid unnecessary object copying.</div></div></td>
1926*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1927*e5dd7070Spatrick<div class="example"><pre>
1928*e5dd7070Spatrickstruct A {};
1929*e5dd7070Spatrick
1930*e5dd7070Spatrickvoid f(const struct A a); // warn
1931*e5dd7070Spatrick</pre></div></div></td>
1932*e5dd7070Spatrick<td class="aligned"></td></tr>
1933*e5dd7070Spatrick
1934*e5dd7070Spatrick
1935*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1936*e5dd7070Spatrickoptimization.PostfixIncIter</span><span class="lang">
1937*e5dd7070Spatrick(C++)</span><div class="descr">
1938*e5dd7070SpatrickOptimization: It is more effective to use prefix increment operator with
1939*e5dd7070Spatrickiterator.
1940*e5dd7070Spatrick<p>Source: Scott Meyers "More Effective C++", item 6:
1941*e5dd7070SpatrickDistinguish between prefix and postfix forms of increment and decrement
1942*e5dd7070Spatrickoperators.</p></div></div></td>
1943*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1944*e5dd7070Spatrick<div class="example"><pre>
1945*e5dd7070Spatrick#include &lt;vector&gt;
1946*e5dd7070Spatrick
1947*e5dd7070Spatrickvoid test() {
1948*e5dd7070Spatrick  std::vector&lt;int&gt; v;
1949*e5dd7070Spatrick  std::vector&lt;int&gt;::const_iterator it;
1950*e5dd7070Spatrick  for(it = v.begin();
1951*e5dd7070Spatrick      it != v.end(); it++) {}; // warn
1952*e5dd7070Spatrick}
1953*e5dd7070Spatrick</pre></div></div></td>
1954*e5dd7070Spatrick<td class="aligned"></td></tr>
1955*e5dd7070Spatrick
1956*e5dd7070Spatrick
1957*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1958*e5dd7070Spatrickoptimization.MultipleCallsStrlen</span><span class="lang">
1959*e5dd7070Spatrick(C)</span><div class="descr">
1960*e5dd7070SpatrickOptimization: multiple calls to <code>strlen()</code> for a string in an
1961*e5dd7070Spatrickexpression. It is more effective to hold a value returned
1962*e5dd7070Spatrickfrom <code>strlen()</code> in a temporary variable.</div></div></td>
1963*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1964*e5dd7070Spatrick<div class="example"><pre>
1965*e5dd7070Spatrick#include &lt;string.h&gt;
1966*e5dd7070Spatrick
1967*e5dd7070Spatrickvoid test(const char* s) {
1968*e5dd7070Spatrick  if (strlen(s) &gt; 0 &amp;&amp;
1969*e5dd7070Spatrick      strlen(s) &lt; 7) {}; // warn
1970*e5dd7070Spatrick}
1971*e5dd7070Spatrick</pre></div></div></td>
1972*e5dd7070Spatrick<td class="aligned"></td></tr>
1973*e5dd7070Spatrick
1974*e5dd7070Spatrick
1975*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1976*e5dd7070Spatrickoptimization.StrLengthCalculation</span><span class="lang">
1977*e5dd7070Spatrick(C++)</span><div class="descr">
1978*e5dd7070SpatrickOptimization: it is more efficient to use <code>string::length()</code> to
1979*e5dd7070Spatrickcalculate the length of an <code>std::string</code>.</div></div></td>
1980*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1981*e5dd7070Spatrick<div class="example"><pre>
1982*e5dd7070Spatrick#include &lt;string&gt;
1983*e5dd7070Spatrick#include &lt;string.h&gt;
1984*e5dd7070Spatrick
1985*e5dd7070Spatrickvoid test() {
1986*e5dd7070Spatrick  std::string s;
1987*e5dd7070Spatrick  if (strlen(s.c_str()) != 0) {}; // warn
1988*e5dd7070Spatrick}
1989*e5dd7070Spatrick</pre></div></div></td>
1990*e5dd7070Spatrick<td class="aligned"></td></tr>
1991*e5dd7070Spatrick
1992*e5dd7070Spatrick
1993*e5dd7070Spatrick<tr><td><div class="namedescr expandable"><span class="name">
1994*e5dd7070Spatrickoptimization.EmptyContainerDetect</span><span class="lang">
1995*e5dd7070Spatrick(C++)</span><div class="descr">
1996*e5dd7070SpatrickOptimization: It is more efficient to use containers <code>empty()</code>
1997*e5dd7070Spatrickmethod to identify an empty container.</div></div></td>
1998*e5dd7070Spatrick<td><div class="exampleContainer expandable">
1999*e5dd7070Spatrick<div class="example"><pre>
2000*e5dd7070Spatrick#include &lt;list&gt;
2001*e5dd7070Spatrick
2002*e5dd7070Spatrickvoid test() {
2003*e5dd7070Spatrick  std::list&lt;int&gt; l;
2004*e5dd7070Spatrick  if (l.size() != 0) {}; // warn
2005*e5dd7070Spatrick}
2006*e5dd7070Spatrick</pre></div></div></td>
2007*e5dd7070Spatrick<td class="aligned"></td></tr>
2008*e5dd7070Spatrick
2009*e5dd7070Spatrick
2010*e5dd7070Spatrick</table>
2011*e5dd7070Spatrick
2012*e5dd7070Spatrick<br>
2013*e5dd7070Spatrick</div> <!-- page -->
2014*e5dd7070Spatrick</div> <!-- content -->
2015*e5dd7070Spatrick</body>
2016*e5dd7070Spatrick</html>
2017