bufdemo函数测试
/* 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;
}
/*gcc -S bufdemo.c的时候会有警告
bufdemo.c: In function ‘echo’:
bufdemo.c:22:5: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
gets(buf);
^
运行结果:
***@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out
Type a string:0123
0123
***@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out
Type a string:012345
012345
*** stack smashing detected ***: ./a.out terminated
已放弃 (核心已转储)
objdump -S a.out
a.out: 文件格式 elf32-i386
Disassembly of section .init:
08048354 <_init>:
8048354: 53 push