san*_*ser 0 c++ visual-c++ address-sanitizer visual-studio-2022
我已在 Visual Studio 中为我的项目启用了 Address Sanitizer,并成功使用Microsoft Learn中的以下代码对其进行了测试。
#include <stdio.h>
int x[100];
int main() {
printf("Hello!\n");
x[100] = 5; // Boom!
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,清理程序无法在以下代码中找到丢失的删除语句:
struct Object {
int x;
int y;
};
int main() {
Object* obj = new Object();
// Boom!
return 0;
}
Run Code Online (Sandbox Code Playgroud)
查看生成的程序集,我们可以看到 new 运算符确实被调用并且没有被优化掉。以下输出取自 Debug/x86 配置,但对于配置 Debug/x64、Release/x86 和 Release/x64 可以获得类似的输出。
; 6 : int main() {
push ebp
mov ebp, esp
sub esp, 12 ; 0000000cH
mov ecx, OFFSET __62A33F1D_Source@cpp
call @__CheckForDebuggerJustMyCode@4
; 7 : Object* obj = new Object();
push 8
call ??2@YAPAXI@Z ; operator new
Run Code Online (Sandbox Code Playgroud)
Address Sanitizer 可以检测到这种类型的错误吗?如果是,我怎样才能成功检测到错误?