Why would buffer overruns cause segmentation faults when accessing an integer?(为什么在访问整数时缓冲区溢出会导致分段错误?)
问题描述
在从函数 A() 调用函数 B() 期间,B() 分配一个 100 字符的数组并多次填充,包括一次使用 101 字符的字符串和一次使用 110 字符的字符串.这是一个明显的错误.
During a call to function B() from function A(), B() allocates a 100-char array and fills it several times, including once with a 101-character string and once with a 110 character string. This is an obvious mistake.
后来,函数 A() 尝试访问完全不相关的 int 变量 i,结果出现分段错误.
Later, function A() tries to access completely unrelated int variable i, and a segmentation fault occurs.
我明白为什么会发生缓冲区溢出,但是为什么我在访问这个整数时会出现分段错误?为什么我不简单地获取垃圾数据?
I understand why the buffer overrun occurs, but why do I get a segmentation fault when I access this integer? Why is it that I don't simply get garbage data?
推荐答案
当A()
调用B()
时,B的前导指令保存了A的帧指针——位置在 A 保存局部变量的堆栈上,然后用 B 自己的帧指针替换它.它看起来像这样:
When A()
calls B()
, B's preamble instructions save A's frame pointer—the location on the stack where A keeps local variables, before replacing it with B's own frame pointer. It looks like this:
当 B 超出其局部变量时,它会弄乱将重新加载到帧指针中的值.这是作为帧指针值的垃圾,因此 A 的所有局部变量都被丢弃.更糟糕的是,未来对局部变量的写入会扰乱属于其他人的内存.
When B overruns its local variables, it messes up the value which will be reloaded into the frame pointer. This is garbage as a frame pointer value, so all of A's local variables are trashed. Worse, future writes to local variables are messing with memory belonging to someone else.
这篇关于为什么在访问整数时缓冲区溢出会导致分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么在访问整数时缓冲区溢出会导致分段错误?
基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01