See the document "Cppcheck and PREfast static analysis test for Cplusplus code". zip
C + + Test source code main.cpp
#define NULL 0
#include <stdio.h>
#include <string.h>
Not initialized
void Defect1 ()
{
int A;
int b;
b = A;
}
Null pointer value
void Defect2 (int b, int c)
{
int * p = NULL;
int a = 1;
if (b = = 1)
{
if (c = = 1)
{
p = & A;
}
Else
{
}
}
Else
{
if (c = = 1)
{
}
Else
{
p = & A;
}
}
* p;
return;
}
Operator precedence for possible errors
void Defect3 ()
{
int a = 1;
int b = 1;
int c = 1;
if (A & b = = c)
return;
}
Possible Buffer overrun
void Defect4 ()
{
Char buf[100];
Char buf2[200];
int i = 100;
sprintf (buf, "Hello World%d", i);
strcpy (buf, BUF2);
}
Possible infinite loops
void Defect5 ()
{
Signed Char i;
for (i = +; I >= 0; i + +) {
;
}
}
Format string Error
void Defect6 ()
{
Char buff[5];
sprintf (Buff, "%s%s", "a");
}
= and = = Misuse
void Defect7 ()
{
int a = 1;
if (A = 2)
return;
}
Logical Operation Problems
void Defect8 ()
{
int x;
if (0 && x + +) {
;
}
}
void Main ()
{
}
1, the use of PREfast before and after comparison
Figure 1 Canceling the C + + code Analysis
Figure 2 Warning of VS2010 output when not using PREfast
Figure 3 Enabling C/+ + code Analysis
Figure 4 Warning of VS2010 output when using PREfast
2, Cppcheck to main.cpp source code analysis results
Fig. 5 Analysis Results of Cppcheck
C + + Static code analysis tool comparison Cppcheck and PREfast