小心小心再小心-缓冲区溢出

来源:互联网 发布:网络机顶盒能看卫视吗 编辑:程序博客网 时间:2024/04/29 23:23
#include <stdio.h>#include <string.h>int checkpass( void){int x;char a[9];x=0;fprintf(stderr,"a at %p and\nx at %p\n",(void *)a,(void *)&x);printf("enter a short word:");scanf( "%8s", a);if( strcmp( a, "mypass")==0 ){x=1;}return x;}int main(){printf("%d\n", checkpass() );return 0;}

注意:   上面代码中输入字符串的部分,如果写成   scanf("%s",a); 的话,则有可能产生缓冲区缓出的问题。 假设用户输入一段很长的字符串,则会造成缓冲区溢出,有可能重写返回地址或是x的值,而且程序很可能会试图返回到一个程序地址空间范围以外的单元上去,并造成段保护错误和信息转储。
0 0