/* 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();
}
一.代码作用
输入字符并输出
二、代码分析
该代码核心位gets函数,用该函数不断为数组赋值
三输入输出实例
四、个人总结
老师强调过该gets函数有漏洞,在我看来应该就是不能知道数组大小而一直为数赋值而导致溢出.