以下代码的输出是"溢出",但我没有明确调用func
函数。它是如何工作的?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int copy(char *input)
{
char var[20];
strcpy(var, input);
return 0;
}
int func(void)
{
printf("Overflow\n");
return 0;
}
int main(int argc, char *argv[])
{
char str[] = "AAAABBBBCCCCDDDDEEEEFFFFGGGG";
int *p = (int *)&str[24];
*p = (int)func;
copy(str);
return 0;
}
答案 0 :(得分:11)
copy
函数溢出var
函数中的copy
缓冲区,并使用main
函数的地址覆盖func
返回地址。
当copy
函数返回时,它不会在main
函数调用后返回copy
,而是返回func
函数。