我知道这是一种危险的行为,我只是想弄清楚发生了什么。如果我覆盖栈上的返回地址,会发生什么?
守则如下:
#include<stdio.h>
#include<stdlib.h>
static int count = 0;
void hello(void){
count ++;
fprintf(stderr,"hello! %d\n",count);
}
void foo(void){
void *buf[10];
static int i;
for(i = 0 ; i < 100 ; i++){ // put enough data to overwrite the return address on stack
buf[i] = hello;
}
}
int main(void){
int buf[1000];
foo();
return 0;
}
而结果:
……
hello! 83
hello! 84
hello! 85
hello! 86
hello! 87
hello! 88
hello! 89
Segmentation fault (core dumped)
为什么你好FUNC被称为89时间? 当foo返回时,pc寄存器获取hello func的地址,不是吗? 所以你好,叫做,做里面的事情。那又怎么样?程序是不是回到主要功能? 89从哪里来?我必须误解一些事情,请指出。
哪个编译器/操作系统您使用的?另外,你知道buf和buf中的buf没有任何关系,对吧? – Ashalynd
@Ashalynd centos 7,gcc – MMMMMCCLXXVII
哪个版本的gcc? – Ashalynd