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