解释这个运行函数而不明确调用它的代码?

wil*_*ibi 8 c buffer-overflow

下面代码的输出是"溢出",但我没有明确调用该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;
}
Run Code Online (Sandbox Code Playgroud)

oua*_*uah 11

copy函数溢出函数中的var缓冲区并用copy函数的main地址覆盖返回地址func.

copy函数返回时,它返回函数而不是maincopy函数调用之后返回func.