怎么可能在一个平台上发生缓冲区溢出,而在另一个平台上却不会发生缓冲区溢出?
由于情况含糊,我很犹豫是否要问这个问题,但我想了解这是怎么可能的。我有一个使用 Visual Studio 2008 开发的 C++ 应用程序。当我在 Windows 7 64 位(或 Vista 32 位)上编译该应用程序时,该应用程序运行良好。当我在 32 位 Windows XP SP3 上编译应用程序时,我收到缓冲区溢出警告,并且进程终止。它使用相同版本的 Visual Studio 2008 C++ 编译器。为什么我在 XP 上收到缓冲区溢出,但在其他 Windows 平台上却没有?
I'm hesitant to ask this question because of the vagueness of the situation, but I'd like to understand how this is possible. I have a C++ application developed using Visual Studio 2008. When I compile the application on Windows 7 64-bit (or Vista 32-bit), the application runs fine. When I compile the application on 32-bit Windows XP SP3, I receive a buffer overrun warning and the process terminates. This is using the same verison of the Visual Studio 2008 C++ compiler. How is it that I receive a buffer overrun on XP, but not on other Windows platforms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编写代码,这样就不会出现缓冲区溢出,并且在任何平台上都不会出现此问题。也就是说,请确保检查正在访问的缓冲区的边界,以确保您不会尝试在正确的边界之外进行读/写。
Write code so you don't have buffer overruns and you won't have this problem on any platform. Namely, make sure you check the bounds for the buffer you are accessing to make sure you aren't trying to read/write outside of the proper bounds.
幸运的是,宇宙的根本不确定性,或者(比以前更有可能)在 XP 和 7 之间 msvcrt.dll 中发生了更改的实现细节。
底线是您的应用程序中存在错误,您应该修复它。
Luck, the fundamental undeterminedness of the Universe, or (more likely than the previous) an implementation detail that changed in msvcrt.dll between XP and 7.
Bottom line is you have a bug in your application, and you should fix it.
在这两种情况下,您可能都会遇到缓冲区溢出,在第一种情况下,它未被检测到并且(显然)不会造成任何损害。在第二个中它被检测到。 (如果它位于动态分配的内存上,您必须知道分配器通常会分配比要求的更多的内存,因此一个合理的解释是,在第一种情况下,溢出会保留在该区域中,而在第二种情况下则不会)。
You probably have a buffer overrun in both case, in the first it isn't detected and doesn't (apparently) do any harm. In the second it is detected. (If it is on a dynamically allocated memory, you have to know that allocators often allocate more than what asked, thus a plausible explanation is that in the first case the overrun stay in that zone, in the second it doesn't).
数据类型的大小可能会因一个编译器而异(感谢@AndreyT)。使用诸如
sizeof(4)
之类的硬编码数字来表示代码中数据类型的大小,可能会在您的应用程序中弹出错误。您应该使用sizeof(int)
来代替或使用您感兴趣的任何类型。Sizes of data types might change from one compiler to another (thanks @AndreyT). Using hardcoded numbers like
sizeof(4)
to represent the size of a data type on your code, might pop up a bug on your application. You should usesizeof(int)
instead or whatever type you are interested in.Windows-7 有一个称为容错堆的功能,正如它所说,它可以容忍一些错误的缓冲区访问。 Windows XP没有这个功能(Vista,我不知道)。在channel9.msdn.com 或sysinternal.com(忘记具体位置)上有一个由Mark Russinovich 制作的关于它的视频。
Windows-7 has a feature called fault-tolerant-heap which ,as it says, is tolerant about some faulty buffer accesses. Windows XP doesn't have this feature (Vista ,I don't know). There is a video about it on channel9.msdn.com or sysinternal.com (forgot exactly where) by Mark Russinovich.