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

  • 相关阅读:
    三、Gradle初级教程——Gradle除了签名打包还能配置jar包
    四、Android Studio使用——什么样的Project都能导入Studio
    二、Android Studio使用——导入jar包,运行、debug都不是问题
    android-studio安装过程详解
    一、Android Studio入门——Eclipse快捷键配置
    84、PullToRefresh使用详解
    ViewPagerindicator 源码解析
    83、android的消息处理机制(图+源码分析)——Looper,Handler,Message
    82、文字混动效果
    Bogart BogartPublic.vb
  • 原文地址:https://www.cnblogs.com/coolbear/p/4014996.html
Copyright © 2011-2022 走看看