日期:2014-05-16  浏览次数:20605 次

ubuntu 使用gets函数编译警告不能运行
各位大侠:
  帮忙看看下面代码,通过ubuntu下的gcc编译出现警告之后,就退出编译。不知错在哪里,请各位大侠帮忙指点一下,谢谢!

C/C++ code

   #include <stdio.h>
  
   int main()
   {
  
        char str[8];
        gets(str);
        puts(str);
  
       return 0;
  }


小弟文件名为gets.c 编译命令 gcc gets.c -o gets

------解决方案--------------------
那就别用gets了,用fgets
------解决方案--------------------
++ 用fgets
------解决方案--------------------
可能是怕越界造成危险
------解决方案--------------------
Linux下查man,说的很详细了,如下:
Reads characters from standard input until a newline is found. The characters up
to the newline are stored in BUF. The newline is discarded, and the buffer is ter‐
minated with a 0.

This is a _dangerous_ function, as it has no way of checking the amount of space
available in BUF. One of the attacks used by the Internet Worm of 1988 used this to
overrun a buffer allocated on the stack of the finger daemon and overwrite the
return address, causing the daemon to execute code downloaded into it over the con‐
nection.

------解决方案--------------------
警告而已,并没有说不能编译
你运行./gets