aboutsummaryrefslogtreecommitdiff
path: root/www/analyzer/potential_checkers.html
blob: 025c0a8e1696f9191032de83e89592c62ac32fd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>List of potential checkers</title>
  <link type="text/css" rel="stylesheet" href="content.css">
  <link type="text/css" rel="stylesheet" href="menu.css">
  <script type="text/javascript" src="scripts/menu.js"></script>
  <script type="text/javascript" src="scripts/dbtree.js"></script>
</head>
<body>

<div id="page">

<!-- menu -->
<!--#include virtual="menu.html.incl"-->
<!-- page content -->
<div id="content">
<h1>List of potential checkers</h1>

<!-- ========================= allocation/deallocation ======================= -->
<h3>allocation/deallocation</h3>
<table class="checkers">
<col class="namedescr"><col class="example"><col class="progress">
<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>

<tr><td><span class="name">memory.LeakNeverReleased<br>
(C, C++)</span><br><br>
Memory may be never released, potential leak of memory
</td><td>
<pre>
#include &lt;stdlib.h&gt;

int f() {};

void test() { 
  int *p1 = (int*)malloc(sizeof(int)); // warn
  int *p2 = new int; // warn
  int x = f();
  if (x==1)
    return;
  delete p2;
}
</pre></td><td></td></tr>

<tr><td><span class="name">memory.MismatchedFree
<br>enhancement to unix.Malloc<br>(C, C++)</span><br><br>
Mismatched deallocation function is used
</td><td><pre>
#include &lt;stdlib.h&gt;

void test() {
  int *p1 = new int;
  int *p2 = new int[1];

  free(p1); // warn
  free(p2); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">memory.MismatchedDelete
<br>(C, C++)</span><br><br>
Mismatched deallocation function is used
</td><td><pre>
#include &lt;stdlib.h&gt;

void test() {
  int *p1 = new int;
  int *p2 = new int[1];
  int *p3 = (int*)malloc(sizeof(int));

  delete[] p1; // warn
  delete p2; // warn
  delete p3; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">memory.MultipleDelete
<br>(C++)</span><br><br>
Attempt to deallocate released memory
</td><td><pre>
#include &lt;new&gt;

void test() {
  int *p1 = new int;
  int *p2 = new(p1) int;
  int *p3 = p1;
  delete p1;
  delete p1; // warn
  delete p2; // warn
  delete p3; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">memory.LeakPtrValChanged
<br>enhancement to unix.Malloc<br>(C, C++)</span><br><br>
Potential memory leak: a pointer to newly allocated data loses its original 
value
</td><td><pre>
#include &lt;stdlib.h&gt;

void f(const int *);
void g(int *);

void test() {
  int *p1 = new int;
  p1++; // warn
  int *p2 = (int *)malloc(sizeof(int));
  p2 = p1; // warn
  int *p3 = new int;
  f(p3);
  p3++; // warn
  int *p4 = new int;
  f(p4);
  p4++; // ok
}
</pre></td><td></td></tr>

<tr><td><span class="name">memory.DeallocateNonPtr
<br>enhancement to unix.Malloc<br>(C, C++)</span><br><br>
Deallocation function is applied to non-pointer
</td><td><pre>
#include &lt;stdlib.h&gt;

class A {
  int *p;
public:
  operator int *() { return p; }  
};

void test() {
  A a;
  delete a; // warn
  free(a); // warn
  const char *s = "text";
  delete s; // warn
  free(s); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">memory.LeakEvalOrder<br>
(C, C++)</span><br><br>
Potential memory leak: argument evaluation order is undefined, g() may never be called
</td><td><pre>
#include &lt;stdlib.h&gt;

void f1(int, int);
void f2(int*, int*);
int g(int *) { throw 1; };
int h();

void test() {
  f1(g(new int), h()); // warn
  f1(g((int *)malloc(sizeof(int))), h()); // warn
  f2(new int, new int);
}
</pre></td><td></td></tr>

<tr><td><span class="name">memory.DstBufferTooSmall
<br>(C, C++)</span><br><br>
Destination buffer too small
</td><td><pre>
#include &lt;string.h&gt;

void test() {
  const char* s1 = "abc";
  char *s2 = new char;
  strcpy(s2, s1); // warn

  int* p1 = new int[3];
  int* p2 = new int;
  memcpy(p2, p1, 3); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">memory.NegativeArraySize
<br>enhancement to experimental.security.MallocOverflow<br>(C, C++)
</span><br><br>
'n' is used to specify the buffer size may be negative
</td><td><pre>
#include &lt;stdlib.h&gt;

void test() {
  int *p;
  int n1 = -1;
  p = new int[n1]; // warn
}
</pre></td><td></td></tr>

</table>

<!-- ======================= constructors/destructors ====================== -->
<h3>constructors/destructors</h3>
<table class="checkers">
<col class="namedescr"><col class="example"><col class="progress">
<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>

<tr><td><span class="name">ctordtor.ExptInsideDtorExplicit<br>
(C++)</span><br><br>
It is dangerous to let an exception leave a destructor. Using try..catch will 
solve the problem.
</td><td><pre>
void f();

class A {
  A() {}
  ~A() { throw 1; } // warn
};
</pre></td><td></td></tr>

<tr><td><span class="name">ctordtor.ExptInsideDtorImplicit<br>
(C++)</span><br><br>
Calls to functions inside a destructor that are known to throw exceptions is 
dangerous. Using try..catch will solve the problem.
</td><td><pre>
void f() { throw 1; };

class A {
  A() {}
  ~A() { f(); } // warn
};
</pre></td><td></td></tr>

</table>

<!-- ============================== exceptions ============================= -->
<h3>exceptions</h3>
<table class="checkers">
<col class="namedescr"><col class="example"><col class="progress">
<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>

<tr><td><span class="name">exceptions.ThrowSpecButNotThrow
<br>(C++)</span><br><br>
Function prototype has throw(T) specifier but the function do not throw
</td><td><pre>
void f() throw(int) { // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">exceptions.NoThrowSpecButThrows
<br>(C++)</span><br><br>
An exception is throw from a function having the throw() specifier
</td><td><pre>
void f() throw() {
  throw(1); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">exceptions.ThrownTypeDiffersSpec
<br>(C++)</span><br><br>
The type of a thrown exception differs from those specified in the throw(T) 
specifier
</td><td><pre>
struct S{};
void f() throw(int) {
  S s;
  throw (s); // warn
}
</pre></td><td></td></tr>

</table>

<!-- ========================= smart pointers ============================== -->
<h3>smart pointers</h3>
<table class="checkers">
<col class="namedescr"><col class="example"><col class="progress">
<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>

<tr><td><span class="name">smartptr.SmartPtrInit<br>
(C++)</span><br><br>
C++03: auto_ptr should store a pointer to an object obtained via new as allocated
memory will be cleaned using delete<br>
C++11: one should use unique_ptr&lt;T[]&gt; to keep a pointer to memory
allocated by new[]<br>
C++11: to keep a pointer to memory allocated by new[] in a shared_ptr one
should use a custom deleter that calls delete[]
</td><td><pre>
#include &lt;stdlib.h&gt;
#include &lt;memory&gt;

void test() {
  std::auto_ptr&lt;int&gt; p1(new int); // Ok
  std::auto_ptr&lt;int&gt; p2(new int[3]); // warn
  std::auto_ptr&lt;int&gt; 
         p3((int *)malloc(sizeof(int))); // warn
}
</pre></td><td></td></tr>

</table>

<!-- ========================= undefined behavior ========================== -->
<h3>undefined behavior</h3>
<table class="checkers">
<col class="namedescr"><col class="example"><col class="progress">
<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>

<tr><td><span class="name">undefbehavior.ExitInDtor
<br>(C++)</span><br><br>
Undefined behavior: std::exit is called to end the program during the 
destruction of an object with static storage duration
</td><td><pre>
#include &lt;cstdlib&gt;

class A {
public:
  ~A() {
    std::exit(1); // warn
  }
};

A a;
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.LocalStaticDestroyed
<br>(C++)</span><br><br>
Undefined behavior: function containing a definition of static local object is 
called during the destruction of an object with static storage duration so that 
flow of control passes through the definition of the previously destroyed 
static local object
</td><td><pre>
void f();

class A {
public:
  ~A() {
    f(); // warn
  }
};

class B {};

A a;

void f() {
  static B b; // &lt;-
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.UseAfterRelease
<br>enhancement to unix.Malloc<br>(C, C++)</span><br><br>
Pointer to deleted object is referenced (The effect of using an invalid pointer 
value is undefined)
</td><td><pre>
#include &lt;stdlib.h&gt;

void test() {
  int *p = new int;
  delete p;
  int i = *p; // warn
}

</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.ZeroAllocDereference
<br>enhancement to unix.Malloc<br>(C, C++)</span><br><br>
The effect of dereferencing a pointer returned as a request for zero size is 
undefined
</td><td><pre>
#include &lt;stdlib.h&gt;

int *p = new int[0];
int i = p[0]; // warn
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.DeadReferenced
<br>(C++)</span><br><br>
Undefined behavior: the following usage of the pointer to the object whose 
lifetime has ended can result in undefined behavior
</td><td><pre>
// C++03
#include &lt;new&gt;

class A {
public:
  int i;
  void f() {};
};

class B : public A {
};

void test() {
  B *b = new B;
  new(b) A;
  b->i; // warn
  b->f(); // warn
  static_cast&lt;A*&gt;(b); // warn
  dynamic_cast&lt;A*&gt;(b); // warn
  delete b; // warn
}

// C++11
#include &lt;new&gt;

class A {
public:
  int i;
  void f() {};
};

class B : public A {
public:
  ~B() {};
};

void test() {
  A *a = new A;
  new(a) B;
  a->i; // warn
  a->f(); // warn
  B *b = new B;
  new(b) A;
  b->i; // warn
  b->f(); // warn
  static_cast&lt;A*&gt;(b); // warn
  dynamic_cast&lt;A*&gt;(b); // warn
  delete b; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.ObjLocChanges
<br>(C++)</span><br><br>
Undefined behavior: the program must ensure that an object occupies the same 
storage location when the implicit or explicit destructor call takes place
</td><td><pre>
#include &lt;new&gt;

class T { };
struct B {
  ~B();
};

void test() {
  B *b1 = new B;
  B b2;
  new (b1) T;
  new (&amp;b2) T;
  delete b1; // warn
} // warn
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.ExprEvalOrderUndef
<br>(C, C++03)</span><br><br>
Undefined behavior: a scalar object shall have its stored value modified at 
most once by the evaluation of an expression
</td><td><pre>
void test () {
  int i = 0;
  int v[1] = {0};
  i = v[i++]; // warn
  i = ++i + 1; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.StaticInitReentered
<br>(C)</span><br><br>
Undefined behavior: static declaration is re-entered while the object is being 
initialized
</td><td><pre>
int test(int i) {
  static int s = test(2*i); // warn
  return i+1;
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.ConstModified
<br>(C, C++)</span><br><br>
Undefined behavior: const object is being modified
</td><td><pre>
#include &lt;stdlib.h&gt;

class X {
public :
  mutable int i;
  int j;
};
class Y {
public :
  X x;
  Y();
};

void test() {
  const int *ciq = 
    (int *)malloc(sizeof(int));
  int *iq = const_cast&lt;int *&gt;(ciq);
  *iq = 1; // warn

  const Y y;
  Y* p = const_cast&lt;Y*&gt;(&amp;y);
  p-&gt;x.i = 1; // ok
  p-&gt;x.j = 1; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.DeadDestructed
<br>(C++)</span><br><br>
Undefined behavior: the destructor is invoked for an object whose lifetime 
has ended
</td><td><pre>
class A {
public:
  void f() {};
  A() {};
  ~A() {};
};

void test() {
  A a;
  a.~A();
} // warn
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.MethodCallBeforeBaseInit
<br>(C++)</span><br><br>
Undefined behavior: calls member function but base not yet initialized
</td><td><pre>
class A {
public :
  A(int );
};
class B : public A {
public :
  int f();
  B() : A(f()) {} // warn
};
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.MemberOrBaseRefBeforeCtor
<br>(C++)</span><br><br>
C++ Undefined behavior: non-static member or base class of non-POD class type 
is referred before constructor begins execution<br>
C++11 Undefined behavior: non-static member or base class of a class with a 
non-trivial constructor is referred before constructor begins execution
</td><td><pre>
// C++03
struct POD { 
  int i; 
};

struct non_POD : public POD { 
  int j; 
  POD pod;
};

extern POD pod;
extern non_POD non_pod;

int *p1 = &amp;non_pod.j; // warn
int *p2 = &amp;non_pod.pod.i; // warn
int *p3 = &amp;pod.i; // ok
POD *p4 = &amp;non_pod; // warn

POD a;
non_POD b;

struct S {
  int *k;
  non_POD non_pod;
  S() : k(&amp;non_pod.j) {} // warn
};

// C++11
struct trivial { 
  int i; 
};

struct non_trivial: public trivial { 
  non_trivial() {};
  int j; 
  trivial pod;
};

extern trivial t;
extern non_trivial nt;

int *p1 = &amp;nt.j; // warn
int *p2 = &amp;nt.i; // warn
int *p3 = &amp;t.i; // ok
trivial *p4 = &amp;nt;

trivial t;
non_trivial nt;

struct S {
  int *k;
  non_trivial nt;
  S() : k(&amp;nt.j) {} // warn
};
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.MemberRefAfterDtor
<br>(C++)</span><br><br>
C++03: Undefined behavior: non-static member of non-POD class type is referred 
after destructor ends execution<br>
C++11: Undefined behavior: non-static member of a class with a non-trivial 
destructor is referred after destructor ends execution
</td><td><pre>
// C++03
struct non_POD {
  virtual void f() {};
};

void test() {
  non_POD *non_pod = new non_POD();
  non_pod->~non_POD();  
  non_pod->f(); // warn
}

// C++11
struct S {
  ~S() {};
  void f() {};
};

void test() {
  S *s = new S();
  s->~S();  
  s->f(); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.CtorForeignCall
<br>(C++)</span><br><br>
Undefined behavior: call to virtual function of an object under construction 
whose type is neither the constructors own class or one of its bases
</td><td><pre>
class A {
public:
  virtual void f() {};
};

class B {
public:
  B(A* a) { a-&gt;f(); } // warn
};

class C : public A, B {
public:
  C() : B((A*)this) {}
};
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.CtorForeignCast 
undefbehavior.CtorForeignTypeid
<br>(C++)</span><br><br>
Undefined behavior: the operand of typeid/dynamic_cast is an object under 
construction whose type is neither the constructors own class or one of its 
bases
</td><td><pre>
#include &lt;typeinfo&gt;

class A {
public:
  virtual void f() {};
};

class B {
public:
  B(A* a) { 
    typeid(*a); // warn
    dynamic_cast&lt;B*&gt;(a); //warn
  }
};

class C : public A, B {
public:
  C() : B((A*)this) {}
};
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.MemberRefInCatch 
undefbehavior.BaseRefInCatch
<br>(C++)</span><br><br>
Undefined behavior: referring to any non-static member or base class of an 
object in the handler for a function-try-block of a constructor or destructor 
for that object results in undefined behavior
</td><td><pre>
class C {
  int i;
public :
  C()
  try
  : i(1) {}
  catch (...)
  {
    i=2; // warn
  }
};
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.ReturnAtCatchEnd
<br>(C++)</span><br><br>
Undefined behavior: a function returns when control reaches the end of a 
handler. This results in undefined behavior in a value-returning 
function
</td><td><pre>
int test() try {
}
catch(int) {
} // warn
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.AutoptrsOwnSameObj
<br>(C++03)</span><br><br>
Undefined behavior: if more than one auto_ptr owns the same object at the same 
time the behavior of the program is undefined.
</td><td><pre>
#include &lt;memory&gt;

void test() {
  int *data = new int;
  std::auto_ptr&lt;int&gt; p(data);
  std::auto_ptr&lt;int&gt; q(data); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.BasicStringBoundAccess
<br>(C++03)</span><br><br>
Undefined behavior: out-of-bound basic_string access
</td><td><pre>
void test() {
  std::basic_string&lt;char&gt; s;
  char c = s[10]; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.BasicStringBoundModification
<br>(C++)</span><br><br>
Undefined behavior: out-of-bound basic_string modification
</td><td><pre>
void test() {
  std::basic_string&lt;char&gt; s;
  s[10] = 0; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.EosDereference
<br>(C++)</span><br><br>
Undefined behavior: the result of operator*() on an end of stream is 
undefined
</td><td><pre>
#include &lt;vector&gt;

void test() {
  std::vector&lt;int&gt; v;
  int i = *v.end(); // warn
  *v.end() = 0; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.QsortNonPOD 
undefbehavior.QsortNonTrivial
<br>C++</span><br><br>
C++03: Undefined behavior: the objects in the array passed to qsort are of 
non-POD type<br>
C++11: Undefined behavior: the objects in the array passed to qsort are of 
non-trivial type
</td><td><pre>
// C++03
#include &lt;cstdlib&gt;

struct non_POD {
  int i;
  non_POD(int ii) : i(ii) {}
};

non_POD values[] = { non_POD(2), non_POD(1) };

int compare(const void *a, 
            const void *b) {
  return ( (*(non_POD*)a).i -
           (*(non_POD*)b).i );
}

void test() {
  qsort(values, 2, sizeof(non_POD), 
        compare); // warn
}

// C++11
#include &lt;cstdlib&gt;

struct S {};

struct trivial_non_POD : public S {
  int i;
};

struct non_trivial {
  int i;
  non_trivial() {}
};

trivial_non_POD tnp[2];
non_trivial nt[2];

int compare1(const void *a, 
             const void *b) {
  return ( (*(trivial_non_POD *)a).i -
           (*(trivial_non_POD *)b).i );
}

int compare2(const void *a, 
             const void *b) {
  return ( (*(non_trivial *)a).i -
           (*(non_trivial *)b).i );
}

void test() {
  qsort(tnp, 2, sizeof(trivial_non_POD), 
        compare1); // ok
  qsort(nt, 2, sizeof(non_trivial), 
        compare2); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.ThrowWhileCopy
<br>C++</span><br><br>
Undefined behavior: copy constructor/assignment operator can throw an exception.
The effects are undefined if an exception is thrown.
</td><td><pre>
struct S {
  int i, j;
  S (const S &amp;s) {
    i = s.i;
    throw 1; // warn
    j = s.j;
  };
  S &amp;operator=(const S &amp;s) {
    i = s.i;
    throw 1; // warn
    j = s.j;
  }
};
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.ValarrayArgBound
<br>(C++)</span><br><br>
Undefined behavior: the value of the second argument is greater than the number 
of values pointed to by the first argument
</td><td><pre>
#include &lt;valarray&gt;

struct S {
  int i;
  S(int ii) : i(ii) {};
};

void test(void) {
  S s[] = { S(1), S(2) };
  std::valarray&lt;S&gt; v(s,3); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.ValarrayLengthDiffer
<br>(C++)</span><br><br>
Undefined behavior: valarray operands are of different length
</td><td><pre>
// C++03
#include &lt;valarray&gt;

void test(void) {
  std::valarray&lt;int&gt; a(0, 1), b(0, 2);
  std::valarray&lt;bool&gt; c(false, 1);
  a = b; // warn
  a *= b; // warn
  a = a * b; // warn
  c = a == b; // warn
  b.resize(1);
  a = b; // OK
}

// C++11
#include &lt;valarray&gt;

void test(void) {
  std::valarray&lt;int&gt; a(0, 1), b(0, 2);
  std::valarray&lt;bool&gt; c(false, 1);
  a = b; // ok
  a *= b; // ok
  a = a * b; // warn
  c = a == b; // warn
  b.resize(1);
  a = b; // OK
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.ValarrayZeroLength
<br>(C++)</span><br><br>
Undefined behavior: calling sum()/min()/max() method of an array having zero 
length, the behavior is undefined
</td><td><pre>
#include &lt;valarray&gt;

void test(void) {
  std::valarray&lt;int&gt; v(0, 0);
  v.sum(); // warn
  v.min(); // warn
  v.max(); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.ValarrayBadIndirection
<br>(C++)</span><br><br>
Undefined behavior: element N is specified more than once in the 
indirection
</td><td><pre>
#include &lt;valarray&gt;

void test() {
  size_t addr[] = {0, 1, 1}; // N is 1
  std::valarray&lt;size_t&gt;indirect(addr, 3);
  std::valarray&lt;int&gt; a(0, 5), b(1, 3);
  a[indirect] = b; //warn
  a[indirect] *= b; //warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.IosBaseDestroyedBeforeInit
<br>(C++)</span><br>
<br>Undefined behavior: ios_base object is destroyed before initialization have 
taken place. basic_ios::init should be call to initialize ios_base 
members
</td><td><pre>
#include &lt;ios&gt;

using namespace std;
template &lt;class T, class Traits = std::char_traits&lt;T&gt;&gt;
class my_stream1 : public std::basic_ios&lt;T, Traits&gt; {
};

template &lt;class T, class Traits = std::char_traits&lt;T&gt;&gt;
class my_stream2 : public std::basic_ios&lt;T, Traits&gt; {
  class my_streambuf : public std::basic_streambuf&lt;T, Traits&gt; {
  };
public:
  my_stream2() {
    this->init(new my_streambuf);
  }
};

void test() {
  my_stream1&lt;char&gt; *p1 = new my_stream1&lt;char&gt;
  my_stream2&lt;char&gt; *p2 = new my_stream2&lt;char&gt;
  delete p1; // warn
  delete p2; // ok
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.IosBaseUsedBeforeInit
<br>(C++11)</span><br><br>
Undefined behavior: ios_base object is used before initialization have taken 
place. basic_ios::init should be call to initialize ios_base members
</td><td><pre>
#include &lt;ios&gt;

using namespace std;
template &lt;class T, class Traits = std::char_traits&lt;T&gt;&gt;
class my_stream1 : public std::basic_ios&lt;T, Traits&gt; {
};

template &lt;class T, class Traits = std::char_traits&lt;T&gt;&gt;
class my_stream2 : public std::basic_ios&lt;T, Traits&gt; {
  class my_streambuf : public std::basic_streambuf&lt;T, Traits&gt; {
  };
public:
  my_stream2() {
    this->init(new my_streambuf);
  }
};

void test() {
  my_stream1&lt;char&gt; *p1 = new my_stream1&lt;char&gt;
  my_stream2&lt;char&gt; *p2 = new my_stream2&lt;char&gt;
  p1->narrow('a', 'b'); // warn
  p2->narrow('a', 'b'); // ok
  delete p1; // warn
  delete p2; // ok
}
</pre></td><td></td></tr>

<tr><td><span class="name">undefbehavior.MinusOnePosType
<br>(C++)</span><br><br>
Undefined behavior: passing -1 to any streambuf/istream/ostream member that 
accepts a value of type traits::pos_type result in undefined behavior
</td><td><pre>
#include &lt;fstream&gt;

class my_streambuf : public std::streambuf {
  void f() {
    seekpos(-1); // warn
  }
};

void test() {
  std::filebuf fb;
  std::istream in(&amp;fb);
  std::ostream out(&amp;fb);
  std::filebuf::off_type pos(-1);
  in.seekg(pos); // warn
  out.seekp(-1); // warn
}
</pre></td><td></td></tr>
</table>

<!-- ============================ different ================================ -->
<h3>different</h3>
<table class="checkers">
<col class="namedescr"><col class="example"><col class="progress">
<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr>
</thead>

<tr><td><span class="name">different.ArgEvalOrderUndef
<br>(C)</span><br><br>
Errors because of the order of evaluation of function arguments is undefined
</td><td><pre>
void f(int, int);

void test() {
  int i = 0;
  int v[1] = {0};
  f(v[i], i++); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.IdenticalExprBinOp
<br>(C)</span><br><br>
There are identical sub-expressions to the left and to the right of the 
operator
</td><td><pre>
#define A 1
#define B 1

bool isNan(double d) { 
  return d != d; // ok
}

int f();

void test() {
  int i = 0;
  if (i != 0 && i != 0) {} // warn

  if(i == A || i == B) {} // ok

  if (++i != 0 && ++i != 0) {} // ok

  if (f() && f()) {} // ok
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.FuncPtrInsteadOfCall
<br>(C)</span><br><br>
Possibly a function call should be used instead of a pointer to function
</td><td><pre>
int f();

void test() {
  if (f == 0) {} // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.IdenticalCondIfElseIf
<br>(C)</span><br><br>
The use of 'if (A) {...} else if (A) {...}' pattern was detected. There is a 
probability of logical error presence
</td><td><pre>
void test() { 
  int i = 7;
  if (i == 1) {}
  else if (i == 1) {} // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">SuccessiveAssign
<br>(C)</span><br><br>
Successive assign to a variable
</td><td><pre>
void test() { 
  int i=0;
  i=1;
  i=2; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.NullDerefStmtOrder
<br>enhancement to core.NullDereference<br>(C)</span><br><br>
Dereferencing of the null pointer might take place. Checking the pointer for 
null should be performed first
</td><td><pre>
struct S {
  int x;
};

S* f();

void test() {
  S *p1 = f();
  int x1 = p1-&gt;x; // warn
  if (p1) {};

  S *p2 = f();
  int x2 = p2-&gt;x; // ok
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.NullDerefCondOrder
<br>enhancement to core.NullDereference<br>(C)</span><br><br>
Dereferencing of the null pointer might take place. Checking the pointer for 
null should be performed first
</td><td><pre>
struct S{bool b;};

S* f();

void test() {
  S *p = f();
  if (p-&gt;b && p) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.IdenticalStmtThenElse
<br>(C)</span><br><br>
The 'else' statement is equivalent to the 'then' statement
</td><td><pre>
void test() {
  int i;
  if (i==1) {
    i++;
  }
  else { // warn
    i++;
  }
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.MultipleAccessors
<br>(C++)</span><br><br>
multiple accessors met for 'class::field'
</td><td><pre>
class A {
  int i;
  int j;
public:
  int getI() { return i; }
  int getJ() { return i; } // warn
  void setI(int& ii) { i = ii; }
  void setJ(int& jj) { i = jj; } // warn
};
</pre></td><td></td></tr>

<tr><td><span class="name">different.AccessorsForPublic
<br>(C++)</span><br><br>
Accessors exist for 'class::field'. Should this field really be public?
</td><td><pre>
class A {
public:
  int i; // warn
  int getI() { return i; }
  void setI(int& ii) { i = ii; }
};
</pre></td><td></td></tr>

<tr><td><span class="name">different.LibFuncResultUnised
<br>(C, C++)</span><br><br>
Calling 'f' ignoring its return value is of no use (* create the list of known 
system/library/API functions falling into this category)
</td><td><pre>
#include &lt;vector&gt;

void test() {
  std::vector&lt;int&gt; v;
  v.empty(); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.WrongVarForStmt
<br>(C, C++)</span><br><br>
Possibly wrong variable is used in the loop/cond-expression of the 'for'
statement. Did you mean 'proper_variable_name'?
</td><td><pre>
void test() {
  int i;
  int j;
  for (j=0; j&lt;3; ++i); // warn
  for (int j=0; i&lt;3; ++j); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.FloatingCompare
<br>(C)</span><br><br>
Comparing floating point numbers may be not precise
</td><td><pre>
#include &lt;math.h&gt;

void test() {
  double b = sin(M_PI / 6.0);
  if (b == 0.5) // warn
    b = 0;
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.BoolCompare
<br>maybe merge with experimental.core.BoolAssignment<br>(C, C++)</span><br><br>
Comparing boolean to a value other then 0 or 1
</td><td><pre>
void test() {
  int i;
  if (0 < i < 3) {}; // warn
  bool b;
  if (b == 3) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.BitwiseOpBoolArg
<br>maybe join with experimental.core.BoolAssignment<br>(C, C++)</span><br><br>
bool value is used at the left/right part of the &amp; (|) operator. Did you mean 
&amp;&amp; (||) ?
</td><td><pre>
int f();

void test() {
  bool b = true;
  if (b &amp; f()) {} // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.LabelInsideSwitch
<br>(C)</span><br><br>
Possible misprint: label found inside the switch() statement. (* did you mean 
'default'?)
</td><td><pre>
void test() {
  int c = 7;
  switch(c){
  case 1:
    c += 1; break;
  defalt: // warn
    c -= 1; break;
  }
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.IdenticalCondIfIf
<br>(C)</span><br><br>
The conditions of two subsequent 'if' statements are identical
</td><td><pre>
void test() {
  int c = 7;
  if (c &gt; 5) // &lt;-
    c += 1;
  if (c &gt; 5) // warn
    c -= 1;
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.CondOpIdenticalReturn
<br>(C)</span><br><br>
The return expressions of the '?:' operator are identical
</td><td><pre>
void test() {
  unsigned a;
  a = a > 5 ? a : a; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.UnaryPlusWithUnsigned
<br>(C)</span><br><br>
Using 'unary +' with unsigned is meaningless
</td><td><pre>
void test() {
  unsigned a;
  a = +a; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.LogicalOpUselessArg
<br>(C)</span><br><br>
The second operand of the &amp;&amp; operator has no impact on expression result
</td><td><pre>
void test() {
  unsigned a;
  if (a&lt;7 &amp;&amp; a&lt;10) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.SameResLogicalExpr
<br>(C)</span><br><br>
The expression always evaluates to true/false
</td><td><pre>
void test() {
  int i=0;
  if (i!=0) {}; // warn
  if (i==0 &amp;&amp; i==1) {}; // warn
  if (i<0 || i>=0) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.SameResUnsignedCmp
<br>(C)</span><br><br>
Comparison of unsigned expression 'op expr' is always true/false
</td><td><pre>
void test() {
  unsigned u;
  if (u &lt; -1) {}; // warn
  if (u &gt;= 0) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.OpPrecedenceAssignCmp
<br>(C)</span><br><br>
Comparison operation has higher precedence then assignment. Bool value is 
assigned to variable of type 'type'. Parenthesis may bee required around an 
assignment
</td><td><pre>
int f();

void test() {
  bool b;
  int x, y;
  if((b = x != y)) {} // ok
  if((x = f() != y)) {} // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.OpPrecedenceIifShift
<br>(C)</span><br><br>
?: has lower precedence then &lt;&lt;
</td><td><pre>
#include &lt;iostream&gt;

void test() {
  int a;
  std::cout &lt;&lt; a ? "a" : "b"; // warn
  a &lt;&lt; a&gt;7 ? 1 : 2; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.ObjectUnused
<br>(C++)</span><br><br>
The object was created but is not being used<br><br>
The exception object was created but is not being used. Did you mean 
'throw std::exception();'?
</td><td><pre>
#include &lt;exception&gt;

struct S {
  int x, y;
  S(int xx, int yy) : x(xx), y(yy) {
  }
  S(int xx) {
    S(xx, 0); // warn
  }
};

void test() {
  S(0, 0); // warn
  std::exception(); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.StaticArrayPtrCompare
<br>(C)</span><br><br>
Pointer to static array is being compared to NULL. May the subscripting is 
missing
</td><td><pre>
void test() {
  int a1[1];
  if (a1 == 0) {}; // warn

  int a2[1][1];
  if (a2[0]) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.ConversionToBool
<br>maybe join with experimental.core.BoolAssignment<br>(C, C++)</span><br><br>
Odd implicit conversion from 'type' to 'bool'
</td><td><pre>
bool test() {
  return 1.; // warn
  return ""; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.ArrayBound
<br>enhancement to experimental.security.ArrayBound[v2]<br>(C, C++)</span><br><br>
Out-of-bound dynamic array access
</td><td><pre>
#include &lt;stdlib.h&gt;

void test() {
  int *p2 = new int[1];
  if(p2[1]) {}; // warn
  int i = 1;
  if(p2[i]) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.StrcpyInputSize
<BR>enhancement to experimental.unix.cstring.OutOfBounds<br>(C)</span><br><br>
Buffer copy without checking size of input
</td><td><pre>
void test(char* string) {
  char buf[24];
  strcpy(buf, string); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.IntegerOverflow
<br>(C)</span><br><br>
Integer overflow
</td><td><pre>
#include &lt;limits.h&gt;

int f(int x) {
  return INT_MAX+1; // warn
}

void test() {
  int x = INT_MAX+1; // warn
  f(INT_MAX+1); // warn

  int y = INT_MAX/2+1; // warn
  x = y*2; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.SignExtension
<br>(C)</span><br><br>
Unexpected sign extension might take place
</td><td><pre>
void f(unsigned int i);
int g();

unsigned int test() {
  long long sll;
  unsigned long long ull = sll; // warn
  long sl;
  unsigned long ul = sl; // warn
  int si;
  unsigned int ui = si; // warn
  short ss;
  unsigned short us = ss; // warn
  signed char sc;
  unsigned char uc = sc; // warn
  f(si); // warn
  ui = g(); // warn
  return si; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.NumericTruncation
<br>(C)</span><br><br>
Numeric truncation might take place
</td><td><pre>
void f(int i);
int g();

int test() {
  unsigned long long ull;
  long long sll;
  unsigned long ul = ull; // warn
  long sl = sll; // warn
  unsigned int ui = ul; // warn
  int si = sl; // warn
  unsigned short us = ui; // warn
  short ss = si; // warn
  unsigned char uc = us; // warn
  signed char sc = uc; // warn
  f(sll); // warn
  ss = g(); // warn
  return sll; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">different.MissingCopyCtorAssignOp
<br>(C, C++)</span><br><br>
The class has dynamically allocated data members but do not define a copy 
constructor/assignment operator
</td><td><pre>
class C { // warn
  int *p; // &lt;-
public:
  C() { p = new int; }
  ~C() { delete p; }
};
</pre></td><td></td></tr>

</table>

<!-- ============================ WinAPI =================================== -->
<h3>WinAPI</h3>
<table class="checkers">
<col class="namedescr"><col class="example"><col class="progress">
<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>

<tr><td><span class="name">WinAPI.CreateProcess
<br>(C)</span><br><br>
After calling CreateProcess(), ensure that process and thread handles get closed
(* for the given example: examine data flow from pi, pi.hProcess and pi.hThread)
</td><td><pre>
#include &lt;windows.h&gt;

void test() {
  STARTUPINFO si;
  PROCESS_INFORMATION pi;
  BOOL fSuccess;
  fSuccess = CreateProcess(
    NULL, TEXT("MyProgram.exe"), NULL, NULL, 
    TRUE, 0, NULL, NULL, &amp;si, &amp;pi);
} // warn
</pre></td><td></td></tr>

<tr><td><span class="name">WinAPI.LoadLibrary
<br>(C)</span><br><br>
Calling LoadLibrary without a fully qualified path may allow to load a DLL from 
arbitrary location
</td><td><pre>
#include &lt;windows.h&gt;

void test() {
  HINSTANCE h = LoadLibrary("X.dll"); // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">WinAPI.WideCharToMultiByte
<br>(C)</span><br><br>
Buffer overrun while calling WideCharToMultiByte
</td><td><pre>
#include &lt;windows.h&gt;

void test() 
{
  wchar_t ws[] = L"abc";
  char s[3];
  int res1 = WideCharToMultiByte(
               CP_UTF8, 0, ws, -1, s, 
               3, NULL, NULL); // warn
  int res2 = WideCharToMultiByte(
               CP_UTF8, 0, ws, -1, s, 
               3, NULL, NULL); // ok
  if (res2 == sizeof(s))
    s[res2-1] = 0;
  else
   s[res2] = 0;
}
</pre></td><td></td></tr>

</table>

<!-- =========================== optimization ============================== -->
<h3>optimization</h3>
<table class="checkers">
<col class="namedescr"><col class="example"><col class="progress">
<thead><tr><td>Name, Description</td><td>Example</td><td>Progress</td></tr></thead>

<tr><td><span class="name">optimization.PassConstObjByValue
<br>(C, C++)</span><br><br>
Optimization: It is more effective to pass const n-th parameter by reference to 
avoid unnecessary object copying
</td><td><pre>
struct A {
  int a[20];
  int b;
};

bool FirstIsZero(const struct A a) { // warn
  return a.a[0] == 0;
}
</pre></td><td></td></tr>

<tr><td><span class="name">optimization.PostfixIncIter
<br>(C++)</span><br><br>
Optimization: It is more effective to use prefix ++ with iterator here
</td><td><pre>
#include &lt;vector&gt;

void test() {
  std::vector&lt;int&gt; v;
  std::vector&lt;int&gt;::const_iterator it;
  for(it = v.begin(); 
      it != v.end(); it++) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">optimization.MultipleCallsStrlen
<br>(C)</span><br><br>
Optimization: multiple calls to strlen for a given string in the given 
expression. It is more effective to hold strlen result in a temporary 
variable
</td><td><pre>
#include &lt;string.h&gt;

void test() {
  const char* s = "abc";
  if (strlen(s) &gt; 0 &amp;&amp;
      strlen(s) &lt; 7) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">optimization.EmptyCstrDetect
<br>(C)</span><br><br>
Optimization: it is more efficient to use "str[0] != '\0'" to identify an empty 
string
</td><td><pre>
#include &lt;string.h&gt;

void test() {
  const char* s = "abc";
  if (strlen(s) &gt; 0) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">optimization.StrLengthCalculation
<br>(C, C++)</span><br><br>
Optimization: it is more efficient to use string::length() method to calculate 
string length
</td><td><pre>
#include &lt;string&gt;
#include &lt;string.h&gt;

void test() {
  std::string s;
  if (strlen(s.c_str()) != 0) {}; // warn
}
</pre></td><td></td></tr>

<tr><td><span class="name">optimization.EmptyContainerDetect
<br>(C, C++)</span><br><br>
Optimization: It is more efficient to use container.empty() to identify an 
empty container
</td><td><pre>
#include &lt;list&gt;

void test() {
  std::list&lt;int&gt; l;
  if (l.size() != 0) {}; // warn
}
</pre></td><td></td></tr>

</table>

<br>
</div> <!-- page -->
</div> <!-- content -->
</body>
</html>