gpt4 book ai didi

c - 运行时如何检测缓冲区溢出?

转载 作者:太空狗 更新时间:2023-10-29 15:45:31 26 4
gpt4 key购买 nike

{
char bufBef[32];
char buf[8];
char bufAfter[32];
sprintf(buf,"AAAAAAA\0");
buf[8]='\0';
printf("%s\n",buf);
}

在 Windows 7 上,我使用 Visual Studio 2008 编译程序作为调试项目。 3个缓冲区相邻。我用调试器找到了他们的地址,如下:

bufBef           0x001afa50 

buf 0x001afa40

bufAfter 0x001afa18

语句“buf[8]='\0'”将地址写出buf。当我运行该程序时,操作系统报告“调试错误:运行时检查失败 #2 - 变量‘buf’周围的堆栈已损坏。”

然后我将它编译为一个发布项目。它安静地运行,没有出现错误报告。

我的问题是运行时如何检测缓冲区溢出?

最佳答案

/RTCs你看效果是什么开关。

约翰罗宾斯的书 Debugging Applications for Microsoft .NET and Microsoft Windows对此进行了深入讨论。

相关摘录:

Fortunately for us, Microsoft extended the /RTCs switch to also do overrun and underrun checking of all multibyte local variables such as arrays. It does this by adding four bytes to the front and end of those arrays and checking them at the end of the function to ensure those extra bytes are still set to 0xCC.

请注意,此开关仅适用于未优化的构建(调试构建)。

关于c - 运行时如何检测缓冲区溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4282628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com