代码:
#include<stdio.h>
#include<stdlib.h>
void bug()
{
system("reboot");
exit(0);
}
int stack_test(int a,int b)
{
printf("before write: 0x%x\n",b);
int* p=&a;
p++;
*p=0xdddd;
printf("after write: 0x%x\n",b);
int c=0xcccc;
return c;
}
int main()
{
int a=0xaaaa;
int b=0xbbbb;
int ret=stack_test(a,b);
printf("you should run here\n");
return 0;
}
运行结果:
before write: 0xaaaa
after write: 0xdddd
函数调用时参数是从右往左压的,所以p取到a的地址后,p++后指向b,*p改变b的内容,故after write后,b为0xdddd.
#include<stdio.h>
#include<stdlib.h>
void bug()
{
system("reboot");//重启
exit(0);
}
int stack_test(int a,int b)
{
int* p=&a;
p--;
*p=bug;
int c=0xcccc;
return c;
}
int main()
{
int a=0xaaaa;
int b=0xbbbb;
int ret=stack_test(a,b);
printf("you should run here\n");
return 0;
}