缓冲区溢出显示

    xiaoxiao2025-02-01  58

    以下为检测的代码

    /* Demonstration of buffer overflow (缓冲区溢出显示)*/ #include <stdio.h> #include <stdlib.h> /* Implementation of library function gets() */ char *gets(char *dest) { int c = getchar(); char *p = dest; while (c != EOF && c != '\n') { *p++ = c; c = getchar();//从缓冲区中一个一个读取代码 } *p = '\0';//结尾字符 return dest; }/* Read input line and write it back */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); }void call_echo() { echo();//再次调用函数 } /*void smash() { printf("I've been smashed!\n"); exit(0); } */int main() { printf("Type a string:"); call_echo();//调用这个函数 return 0; }

    当你正常输入四个字符时,没有问题,当字符再多时,他就要占用别的内存,使得溢出。

    最新回复(0)