zoukankan      html  css  js  c++  java
  • C++静态代码分析工具对比cppCheck与PreFast

    具体内容参看文件《CppCheck和PreFast对Cplusplus代码静态分析测试.zip》

    C++测试源代码main.cpp

    #define NULL 0

    #include <stdio.h>

    #include <string.h>

    // 没有初始化

    void defect1()

    {

             int a;

             int b;

             b = a;

    }

    // 空指针取值

    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 ;

    }

    //可能错误的运算符优先级

    void defect3()

    {

             int a = 1 ;

             int b = 1 ;

             int c = 1 ;

             if (a & b == c)

                       return ;

    }

    //可能的buffer overrun

    void defect4()

    {

             char buf[ 100 ];

             char buf2[ 200 ];

             int i = 100 ;

             sprintf(buf, " hello world %d " , i);

             strcpy(buf, buf2);

    }

    // 可能的无穷循环

    void defect5()

    {

             signed char i;

             for (i = 100 ; i >= 0 ; i ++ ) {

                       ;

             }

    }

    // 格式字符串错误

    void defect6()

    {

             char buff[ 5 ];

             sprintf(buff, " %s %s " , " a " );

    }

    //=和==误用

    void defect7()

    {

             int a = 1 ;

             if (a = 2 )

                       return ;

    }

    // 逻辑运算问题

    void defect8()

    {

             int x;

             if ( 0 && x ++ ) {

                       ;

             }

    }

    void main()

    {

    }

    1、使用PreFast前后对比

    图1取消C/C++代码分析

    图2不使用PreFast时VS2010输出的警告

    图3 启用C/C++代码分析

    图4 使用PreFast时VS2010输出的警告

    2、Cppcheck对main.cpp源代码的分析结果

     

    图5 Cppcheck的分析结果

  • 相关阅读:
    Java 第十一届 蓝桥杯 省模拟赛 洁净数
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 70044与113148的最大公约数
    Java 第十一届 蓝桥杯 省模拟赛 70044与113148的最大公约数
    20. Valid Parentheses
    290. Word Pattern
    205. Isomorphic Strings
    71. Simplify Path
  • 原文地址:https://www.cnblogs.com/coolbear/p/4014996.html
Copyright © 2011-2022 走看看