C + + Static code analysis tool comparison Cppcheck and PREfast

Source: Internet
Author: User
Tags define null sprintf

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.