这些所谓的“灾难”到底是什么?指针使用不当会导致什么?
我在程序中越来越多地使用指针,在阅读有关指针的内容时,我发现的每一个指南或教程都说,不正确地使用指针可能会产生“灾难性”结果。
现在,我遇到过一些大内存泄漏的情况,以及指针取消引用错误的指针变量,返回错误的值,但除此之外没有发生任何“灾难性”的情况;就像我的电脑和/或其他程序崩溃一样。
有人可以给我一个简单的代码示例,它肯定会产生“灾难性”的结果,也许还有一些发生的事情的背景故事,以防万一您不小心使用了那段代码?我所说的“灾难性”结果是指可能干扰其他程序或操作系统并可能导致它们崩溃的代码。
I've been using pointers more and more in my programs, and while reading up about pointers, every single guide or tutorial I found said that incorrect use of pointers could yield 'disastrous' results.
Now, I've had a few cases of some big memory leaks, and pointers dereferencing a wrong pointer variable, returning an incorrect value, but other than that nothing 'disastrous' has ever occurred; like my computer and/or other programs crashing.
Can someone give me a simple code example that will definitely yield 'disastrous' results, perhaps with some back-story of what happened, in case you've ever accidentally used that piece of code? By 'disastrous' results, I mean code that might interfere with other programs or the OS, and possibly make them crash.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不正确的指针运算也可能导致灾难,因为边界错误会导致缓冲区溢出,而缓冲区溢出会导致数据损坏,例如堆栈崩溃:
当然,仅调用
strcpy
或memcpy
[*],您不必自己进行指针算术。如果攻击者控制了 i 的值(可能是因为它是从输入文件读取的,并且攻击者制作了恶意文件),那么您可能会遇到比崩溃更糟糕的情况。结合更多特定于平台的技巧,攻击者可能能够安排返回 i 最终执行攻击者提供的代码。[*] 或
strncpy
、strlcpy
、strcpy_s
或std::copy
,在任何人开始之前。一旦你以某种方式得到了错误的边界,那么向边界检查函数提供错误的边界仍然是错误的......Incorrect pointer arithmetic can lead to disasters too, because getting the bounds wrong leads to buffer overflows, and buffer overflows lead to corrupted data, for example stack smashing:
Of course, you can make the same mistake just calling
strcpy
ormemcpy
[*], you don't have to be doing the pointer arithmetic yourself. If an attacker controls the value ofi
(perhaps because it's read from an input file, and the attacker crafts a malicious file), then you could have worse than a crash on your hands. In combination with more platform-specific tricks, the attacker might be able to arrange that returning toi
eventually ends up executing code supplied by the attacker.[*] or
strncpy
, orstrlcpy
, orstrcpy_s
, orstd::copy
, before anyone starts. Once you've got a bound wrong somehow, then supplying that wrong bound to a bounds-checking function is still wrong...有两种主要的灾难——悬空指针和内存泄漏。
悬空指针是指指针存储的地址不是对象的地址:
内存泄漏是指没有指针存储堆分配对象的地址:
悬空指针不好,因为使用它们会导致未定义的行为 -程序可能会崩溃或修改一些不相关的数据,这将导致以后出现问题。内存泄漏很糟糕,因为分配的内存无法重用,因此程序一段时间后可能会出现内存不足的情况。
There're two main kinds of disasters - dangling pointers and memory leaks.
Dangling pointer is when a pointer stores an address that is not an address of an object:
memory leak is when there're no pointers storing an address of a heap-allocated object:
Dangling pointers are bad because using them you leads to undefined behavior - program might crash or modify some unrelated data and this will cause problems later. Memory leaks are bad because allocated memory can't be reused and so the program can get short on memory some time later.
我想到了一些情况:
A few cases come to mind:
我见过的最令人讨厌的是“延迟故障”,即错误执行的写入访问会损坏稍后使用的数据结构,从而产生完全不相关的代码的错误输出。在调试过程中,您会观察到“机器的崛起”——数据结构神秘地获得了从未分配过的错误值,这违背了程序员的意愿。您可能正在搜索距离实际位置数千个 LOC 的错误。
The most nasty I have seen are "delayed failures", when the wrongly done write access damages a data structure that is used only later, producing incorrect output of the completely irrelevant code. During debugging, you observe "the rise of machines" - the data structure mysteriously obtains the wrong values that have never been assigned, against the programmers will. You may be searching for an error thousands of LOC away from where it really is.