问题来源于《 c与指针》 1.7章节中的一个问题:
问题:下面的代码可能出现什么问题?
- while ( gets (input) != NULL){}
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- char * input = NULL;
- while (gets(input) != NULL){
- puts(input);
- }
- return EXIT_SUCCESS;
- }
编译运行结果如下:
- [root@localhost program]# gcc -g pgetsDemo.c -o pgetsDemo
- /tmp/cc0OMzQL.o: In function `main':
- /program/pgetsDemo.c:6: warning: the `gets' function is dangerous and should not be used.
- [root@localhost program]# ./pgetsDemo
- hello
- Segmentation fault
寻找解决办法途中得知:现在linux下使用fgets函数代替了gets函数,原因:
安全性问题
fgets函数原型为
char *fgets(char *s, int n, FILE *stream);从stream所指的文件读入字符到s所指的内存空间中,直到读到换行符、文件尾或n-1个字符为止,最后会加上NULL 作为字符串结束,即s[n-1] = NULL;如果在未读到n - 1个字符时,读到了换行符或者 文件结束标志(EOF),那么就将换行符或者文件结束标志(EOF)都读到s中,此时,再在换行符或是文件结束标志(EOF)后面添加 NULL。
gets函数原型为
char * gets(char s);
没有限制输入缓冲区的大小,容易造成溢出
从中容易得知,fgets函数比gets函数安全。
于是,修改源代码为:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- char * input = NULL;
- while (fgets(input,10,stdin) != NULL){
- puts(input);
- }
- return EXIT_SUCCESS;
- }
编译,运行:
- [root@localhost program]# gcc -g getsDemo.c -o getsDemo
- [root@localhost program]# ./getsDemo
- hello
- Segmentation fault
编译通过了但是还有还有 段错误的问题,使用GDB断点调试;提示:
Program received signal SIGSEGV, Segmentation fault.
此种错误一般是程序访问了不该访问的内存导致,内存设置了访问权限等等,
经查询,造成段错误的原因归纳如下(引用 cnblogs just_a_coder):
1.内存访问越界
a) 由于使用错误的下标,导致数组访问越界
b) 搜索字符串时,依靠字符串结束符来判断字符串是否结束,但是字符串没有正常的使用结束符
c) 使用strcpy, strcat, sprintf, strcmp, strcasecmp等字符串操作函数,将目标字符串读/写爆。应该使用strncpy, strlcpy, strncat, strlcat, snprintf, strncmp, strncasecmp等函数防止读写越界。
2 多线程程序使用了线程不安全的函数。
3 多线程读写的数据未加锁保护。对于会被多个线程同时访问的全局数据,应该注意加锁保护,否则很容易造成core dump
4 非法指针
a) 使用空指针
b) 随意使用指针转换。一个指向一段内存的指针,除非确定这段内存原先就分配为某种结构或类型,或者这种结构或类型的数组,否则不要将它转换为这种结构或类型的指针,而应该将这段内存拷贝到一个这种结构或类型中,再访问这个结构或类型。这是因为如果这段内存的开始地址不是按照这种结构或类型对齐的,那么访问它时就很容易因为bus error而core dump.
5 堆栈溢出.不要使用大的局部变量(因为局部变量都分配在栈上),这样容易造成堆栈溢出,破坏系统的栈和堆结构,导致出现莫名其妙的错误。
经过分析:
- char * input = NULL;
- while (fgets(input,10,stdin) != NULL)
char input[10];
输入 "hello"
赋值过程为:
- input[0] = 'h';
- input[1] = 'e';
- input[2] = 'l';
- input[3] = 'l';
- input[4] = 'o';
- input[5] = NULL;
而我上面是这样写的:
- char * input = NULL;
- while (fgets(input,10,stdin) != NULL)
- input[0] = 'h';
- input[1] = 'e';
- input[2] = 'l';
- input[3] = 'l';
- input[4] = 'o';
- input[5] = NULL;
下面的问题就是指针能不能经过下标赋值,以下代码验证:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- char * p;
- p[0] = 'a';
- printf("%s",p);
- return EXIT_SUCCESS;
- }
- Segmentation fault
OK,原因找到了.
正确的代码应该是这样的:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- char input[10];
- while (fgets(input,10,stdin) != NULL){
- puts(input);
- }
- return EXIT_SUCCESS;
- }
编译运行,输出为
hello
hello
此程序稍加修改:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int i = 0;
- char input[10];
- while (fgets(input,10,stdin) != NULL){
- puts(input);
- i ++;
- printf("i = %d\n",i);
- }
- return EXIT_SUCCESS;
- }
编译,运行看执行效果:
- [root@localhost program]# ./getsDemo
- hello
- hello
- i = 1
- aaaaaaaaasssssssssddddddddfgggggghhhhh
- aaaaaaaaa
- i = 2
- sssssssss
- i = 3
- ddddddddf
- i = 4
- gggggghhh
- i = 5
- hh
- i = 6
当字符串较长时,fgets会分多次读入。