【问题标题】:How to correctly use "code analysis" in Visual Studio 2010 for C++?如何在 Visual Studio 2010 for C++ 中正确使用“代码分析”?
【发布时间】:2011-03-16 11:56:04
【问题描述】:

如果我安装 Windows 7 和 Visual Studio 2010 Premium SP1 的新副本,请创建向导生成的“C++ 控制台应用程序”并将 3 个标头添加到 .cpp 文件:

#include <winsock2.h>
#include <WS2tcpip.h>
#include <wspiapi.h>

执行“运行代码分析”将在 Microsoft 代码中显示警告(项目本身是向导生成的,因此没有错误):

1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\ws2tcpip.h(729): warning C6386: Buffer overrun: accessing 'argument 1', the writable size is '1*4' bytes, but '4294967272' bytes might be written: Lines: 703, 704, 705, 707, 713, 714, 715, 720, 721, 722, 724, 727, 728, 729
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wspiapi.h(294): warning C6386: Buffer overrun: accessing 'argument 1', the writable size is '1' bytes, but '1025' bytes might be written: Lines: 263, 264, 265, 267, 268, 270, 271, 273, 294
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wspiapi.h(236): warning C6387: '*pptResult' might be '0': this does not adhere to the specification for the function 'WspiapiQueryDNS': Lines: 263, 264, 265, 267, 268, 270, 271, 273, 294, 296
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wspiapi.h(687): warning C6387: 'argument 1' might be '0': this does not adhere to the specification for the function 'WspiapiLegacyFreeAddrInfo': Lines: 504, 505, 506, 507, 508, 509, 510, 512, 513, 514, 515, 516, 520, 528, 532, 538, 550, 551, 555, 556, 560, 563, 568, 575, 577, 578, 589, 591, 592, 593, 596, 598, 599, 600, 604, 607, 610, 611, 627, 662, 664, 680, 685, 687
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wspiapi.h(481): warning C6387: '*pptResult' might be '0': this does not adhere to the specification for the function 'WspiapiLegacyGetAddrInfo': Lines: 504, 505, 506, 507, 508, 509, 510, 512, 513, 514, 515, 516, 520, 528, 532, 538, 550, 551, 555, 556, 560, 563, 568, 575, 577, 578, 589, 591, 592, 593, 596, 598, 599, 600, 604, 607, 610, 611, 627, 662, 664, 680, 685, 687, 688, 691

这使得代码分析无法用于大型项目 - 我在 Microsoft 头文件中收到数以千计的警告,但我无法在其中找到自己的警告 :(。有什么方法可以将 Microsoft 头文件/SDK 从代码分析中排除,所以它会变得可用吗?

附:我知道 C++ 不是 Visual Studio 支持良好的语言,但由于我需要支持和改进现有项目,我无法更改语言:(。

【问题讨论】:

    标签: visual-studio-2010


    【解决方案1】:

    您可以使用 warning() #pragma 为不是 PREfast 清理的头文件禁用代码分析警告:

    #include <codeanalysis\warnings.h>
    #pragma warning(push)
    #pragma warning (disable: ALL_CODE_ANALYSIS_WARNINGS)
    
    // include headers
    
    #pragma warning(pop)
    

    【讨论】:

    • 这非常有用。谢谢。我从不正确的 Windows 功能中收到警告。警告说我不能将 0 指定为 #3 参数,但在 msdn 中说我可以这样做。
    【解决方案2】:

    您是否尝试过使用 CAExcludePath 环境变量?删除那些包含系统头的目录?

    CAExcludePath='C:\Program Files (x86)\Windows Kits\10\Include\10.0.10586.0\ucrt;c:\program files (x86)\microsoft visual studio 14.0\vc\include;c:\program files (x86)\windows kits\8.1\include\um;c:\program files (x86)\microsoft visual studio 14.0\vc\include\sys;'
    

    【讨论】:

      猜你喜欢
      • 2011-07-29
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 1970-01-01
      相关资源
      最近更新 更多