简体   繁体   English

静态分析以检测Visual Studio C ++ 2012上的缓冲区溢出

[英]Static Analysis to detect Buffer Overrun on Visual Studio C++ 2012

Following code writes into invalid area in memory but there's no compilation error. 以下代码将写入内存中的无效区域,但没有编译错误。

int _tmain(int argc, _TCHAR* argv[])
{
    char* s1 = new char[10];
    for(int i=0;i<20;i++) s1[i]='a';
    cout << s1 << endl;
    return 0;
}

In runtime the code terminates with return code 0 printing 20 a's then some garbage before it met 0 but I assume this is very dangerous as it could contaminate / illegally access other area in memory. 在运行时,该代码以返回代码0终止(打印20 a),然后在遇到0之前出现一些垃圾,但是我认为这非常危险,因为它可能污染/非法访问内存中的其他区域。

Is there any way such mistake can be detected in compile time? 有什么办法可以在编译时发现这种错误? Or at least a runtime exception raised pointing straight into s1[i]='a' line? 还是至少出现一个运行时异常,直接指向s1[i]='a'行?

Setting /RTCs and /GS flag as recommended in other posts did not help. 按照其他帖子的建议设置/ RTC和/ GS标志没有帮助。

Running Visual Studio code analysis (ANALZYE -> Run Code Analysis) gives no result either. 运行Visual Studio代码分析(ANALZYE->运行代码分析)也不会产生任何结果。

There are list of tools third party tools posted in here: C++ static code analysis tool on Windows but I'm hoping there's a way this can be detected by Visual Studio alone? 这里发布了第三方工具列表: Windows上的C ++静态代码分析工具,但我希望有一种方法可以通过Visual Studio单独检测?

According to http://msdn.microsoft.com/en-us/library/8dbf701c.aspx , /GS (Buffer Security Check) is carried out in run-time, not in compile type. 根据http://msdn.microsoft.com/zh-cn/library/8dbf701c.aspx,/GS (缓冲区安全性检查)是在运行时执行的,而不是在编译类型中执行的。

/RTCs (according to http://msdn.microsoft.com/en-us/library/8wtf2dfz.aspx ) controls run-time checks. / RTC(根据http://msdn.microsoft.com/zh-cn/library/8wtf2dfz.aspx )控制运行时检查。 So neither of these two switches were designed to do static analysis of your code. 因此,这两个开关都不是为对代码进行静态分析而设计的。 That's, they are not supposed to detect your problem at compile time. 也就是说,他们不应该在编译时检测到您的问题。

I think static code analysis is still in research stage in general, I'd be surprised that VS 2012 would provide full fledged support. 我认为静态代码分析总体上仍处于研究阶段,对于VS 2012提供全面的支持我会感到惊讶。

Another possibility is that the specific types of error you are trying to detect is an array-out-of-bound error. 另一种可能性是,您尝试检测的特定类型的错误是数组越界错误。 Buffer-overrun may not be the right keyword to do search. 缓冲区溢出可能不是执行搜索的正确关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM