1.bufdemo.c的代码展示 2.产生的汇编代码 3.在Linux操作系统下的举例 4.代码运行的详细解释及涉及的知识内容
1.bufdemo.c代码如下
/* 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; }2.汇编代码
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); ^意思是本来头文件声明的gets已被弃用,现在引用的是我们定义的函数gets
bufdemo.c产生的汇编如下
Disassembly of section .init: 08048354 <_init>: 8048354: 53 push