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的分析结果

  • 相关阅读:
    分布式_理论_03_2PC
    分布式_理论_02_Base 理论
    分布式_理论_01_CAP定理
    分布式_理论_00_资源帖
    Git_学习_09_指定某些文件不上传
    Java_脚本引擎_03_nashorn支持es6
    Idea_学习_10_Idea远程debug
    Mybatis_总结_06_用_插件开发
    Mybatis_总结_05_用_Java API
    【BZOJ】2212: [Poi2011]Tree Rotations
  • 原文地址:https://www.cnblogs.com/coolbear/p/4014996.html
Copyright © 2011-2022 走看看