• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            小明思考

            高性能服務(wù)器端計(jì)算
            posts - 70, comments - 428, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            C++代碼靜態(tài)分析工具-Prefast

            Posted on 2006-03-28 13:59 小明 閱讀(13276) 評論(7)  編輯 收藏 引用 所屬分類: C/C++Tools

            1. 什么是Prefast

            Prefast是一種代碼分析工具,它能夠幫助你找到編譯器不能找到的錯(cuò)誤或者缺陷。Prefast首次被微軟集成到Visual Studio 2005 Team Suite中去,使用起來非常方便。

            2.怎么使用Prefast
            在vs2005 Team Suite中,使用Prefast非常簡單。修改你的工程屬性,設(shè)置Enable Code Analysis For C/C++為Yes.

            prefast1.jpg

            效果:
            prefast2.jpg

            注意到有可能錯(cuò)誤的地方以淺灰色顯示在編輯器中了。

            3.Prefast能幫你找到哪些錯(cuò)誤

            1)沒有初始化

            // no?initial
            void ?defect1()
            {
            ????????
            int ?a;
            ????????
            int ?b;

            ????????b?
            = ?a;
            }

            會報(bào): d:\test\testcode\testcode.cpp(18) : warning C6001: Using uninitialized memory 'a': Lines: 15, 16, 18

            2)空指針取值

            // one?path?dereference?NULL
            void ?defect4( 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 ;
            }????

            會報(bào):d:\test\testcode\testcode.cpp(65) : warning C6011: Dereferencing NULL pointer 'p': Lines: 45, 46, 48, 57, 65

            3)可能錯(cuò)誤的運(yùn)算符優(yōu)先級

            void ?defect5()
            {
            ????????
            int ?a? = ? 1 ;
            ????????
            int ?b? = ? 1 ;
            ????????
            int ?c? = ? 1 ;

            ????????
            if ?(a? & ?b? == ?c)
            ????????????????
            return ;
            }

            會報(bào): d:\test\testcode\testcode.cpp(76) : warning C6281: Incorrect order of operations: relational operators have higher precedence than bitwise operators

            4)可能的buffer overrun

            void ?defect8()
            {
            ????????
            char ?buf[ 100 ];
            ????????
            char ?buf2[ 200 ];
            ????????
            int ?i? = ? 100 ;

            ????????sprintf(buf,?
            " hello?world?%d " ,?i);
            ????????strcpy(buf,?buf2);
            }

            會報(bào): d:\test\testcode\testcode.cpp(133) : warning C6202: Buffer overrun for 'buf', which is possibly stack allocated, in call to 'strcpy': length '200' exceeds buffer size '100'

            5)可能的無窮循環(huán)

            // infinite?loop
            void ?defect14()
            {
            ????????signed?
            char ?i;

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

            會報(bào): d:\test\testcode\testcode.cpp(198) : warning C6292: Ill-defined for-loop: counts up from maximum

            6)格式字符串錯(cuò)誤

            // Format?string?mismatch
            void ?defect21()
            {
            ????????
            char ?buff[ 5 ];
            ????????sprintf(buff,?
            " %s?%s " ,? " a " );
            }

            會報(bào): d:\test\testcode\testcode.cpp(277) : warning C6063: Missing string argument to 'sprintf' that corresponds to conversion specifier '2'

            7)安全問題

            void ?defect27()
            {
            ????????CreateProcess(NULL,
            ???????????????
            " c:\\program?files\\Project.exe?arg1 " ,? // correct?"\"c:\\program?files\\Project.exe\"?arg1",
            ???????????????NULL,
            ???????????????NULL,
            ???????????????
            false ,
            ???????????????
            0 ,
            ???????????????NULL,
            ???????????????NULL,
            ???????????????NULL,
            ???????????????NULL);???????????????
            }

            會報(bào): d:\test\testcode\testcode.cpp(327) : warning C6277: NULL application name with an unquoted path in call to 'CreateProcessA': results in a security vulnerability if the path contains spaces

            8)=和==誤用

            void ?defect32()
            {
            ????????
            int ?a? = ? 1 ;

            ????????
            if ?(a? = ? 2 )
            ????????????????
            return ;
            }

            會報(bào): d:\test\testcode\testcode.cpp(405) : warning C6282: Incorrect operator: assignment of constant in Boolean context. Consider using '==' instead

            9)邏輯運(yùn)算問題

            // always?false
            void ?defect45()
            {
            ????????
            int ?x;

            ????????
            if ?( 0 ? && ?x ++ )?{
            ????????????????;
            ????????}
            }

            會報(bào): d:\test\testcode\testcode.cpp(564) : warning C6237: (<zero> && <expression>) is always zero. <expression> is never evaluated and might have side effects

            10)其他





            Feedback

            # re: C++代碼靜態(tài)分析工具-Prefast  回復(fù)  更多評論   

            2006-03-28 17:36 by 笑笑生
            不錯(cuò)

            # re: C++代碼靜態(tài)分析工具-Prefast  回復(fù)  更多評論   

            2006-03-28 18:32 by christanxw
            哪里可以下載?

            # re: C++代碼靜態(tài)分析工具-Prefast  回復(fù)  更多評論   

            2006-03-28 20:57 by fiestay
            樓上的哥們,樓主逗說了,這個(gè)是和VS.net 2005集成在一起的:)

            這工具看起來還不錯(cuò),沒有具體用過.有個(gè)叫C++Test的工具也能對代碼進(jìn)行分析,也可以進(jìn)行單元測試,也不錯(cuò):)

            # re: C++代碼靜態(tài)分析工具-Prefast  回復(fù)  更多評論   

            2006-03-29 12:12 by flyingxu
            有VC6能用的嗎?

            # re: flyingxu   回復(fù)  更多評論   

            2006-03-29 12:57 by 小明
            vc6中使用Prefast的方法:

            prefast是附帶在微軟的DDK中的

            In VC6 project

            1. Install Windows IFS Kit and DDK package
            2. Execute Development Kits->Windows IFS Kit and DDK ->Build environment -> windows 2000->windows 2000 checked build environment
            3. Export Visual Studio project to a .mak file
            4. remove /GZ in .mak file or link fail
            5. Edit a run.bat file (not necessary, only for set new include and lib path)

            run.bat file content
            ----------------
            rem set include and lib path
            set include=C:\Program Files\Microsoft Visual Studio\VC98\atl\include;C:\Program Files\Microsoft Visual Studio\VC98\mfc\include;C:\Program Files\Microsoft Visual Studio\VC98\include
            set lib=C:\Program Files\Microsoft Visual Studio\VC98\mfc\lib;C:\Program Files\Microsoft Visual Studio\VC98\lib
            rem clean environment
            nmake /f httpgetfile.mak clean
            rem run prefast command
            C:\WINDDK\3790.1830\bin\x86\prefast\scripts\prefast nmake /f httpgetfile.mak CFG="HttpGetFile - Win32 Debug"
            rem unset include and lib path
            set lib=
            set include=

            # re: C++代碼靜態(tài)分析工具-Prefast  回復(fù)  更多評論   

            2008-11-03 10:01 by 微波輻射
            C上可以用嗎?

            # re: C++代碼靜態(tài)分析工具-Prefast  回復(fù)  更多評論   

            2008-11-04 17:01 by zhangyz
            linux 有這樣的工具嗎?
            久久综合视频网| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 久久精品国产精品国产精品污| 一本一本久久a久久精品综合麻豆| 99久久精品午夜一区二区| 久久综合久久自在自线精品自| 噜噜噜色噜噜噜久久| 久久国产免费直播| 久久无码中文字幕东京热| 狠狠色丁香久久婷婷综合_中 | 久久AV高清无码| 久久亚洲精品无码VA大香大香| 久久大香萑太香蕉av| 国产精品一区二区久久精品涩爱| 日韩中文久久| 亚洲精品tv久久久久久久久| 亚洲精品美女久久久久99小说| 色综合久久中文字幕综合网| 亚洲国产成人久久综合碰| 久久精品久久久久观看99水蜜桃| 欧美伊人久久大香线蕉综合| 亚洲中文字幕无码久久精品1| 久久精品无码专区免费东京热| 99re久久精品国产首页2020| 国产 亚洲 欧美 另类 久久| 午夜福利91久久福利| 久久成人国产精品| 国产精品VIDEOSSEX久久发布| 久久久久久A亚洲欧洲AV冫| 久久人人爽人人爽人人片AV东京热| 久久久久人妻一区二区三区 | 日韩人妻无码精品久久久不卡 | 国产成人精品免费久久久久| 久久国产精品免费| 久久久久久精品久久久久| 99国产精品久久久久久久成人热| 精品久久人人做人人爽综合 | 久久人人妻人人爽人人爽| 久久精品视频91| 国产精品一区二区久久不卡 | 亚洲精品国产第一综合99久久|