c - 为什么 gcc(linux) 中考虑使用 fgets() 以及使用它的替代方法是什么?

标签 c linux gcc stdio fgets

Possible Duplicate:
Why is the `gets' function is dangerous? Why should not be used?

我建议用户使用 fgets() 输入一个字符串,该字符串将使用 scanf() 进行分析,以区分整数、 float 和字符。我想要一个可靠的程序,但使用 gcc 时收到以下警告:

在函数中main': : warning: the gets' 函数是危险的,不应使用。

任何人都可以告诉我为什么它很危险以及它的安全替代方案是什么? 如果有人能告诉我 fgets() 致命性的严重性,那将非常有帮助。

最佳答案

你可能有点困惑。简而言之:gets 不好,fgets 很好。

man 页面解释了为什么不应使用 gets:

BUGS
       Never use gets().  Because it is impossible to tell without knowing the
       data  in  advance  how  many  characters  gets() will read, and because
       gets() will continue to store characters past the end of the buffer, it
       is  extremely  dangerous  to  use.   It has been used to break computer
       security.  Use fgets() instead.

fgets 将缓冲区的大小作为其参数之一,如果使用正确,则不会出现此问题。

常见问题解答有 entry with more details .

关于c - 为什么 gcc(linux) 中考虑使用 fgets() 以及使用它的替代方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6760907/

相关文章:

arrays - 对数组元素的地址进行减法时出现意外结果

c - 程序在C中无限运行,在编译或运行时没有错误

c++ - 如何读取 gmon.out?

c - 如何在 Mac 上设置 Code::Blocks 的工作目录?

c - 关于 BYTE 类型的 strlen() 的警告

linux - bash:使用 sudo 权限打开文件描述符

C——修改栈基指针地址

linux - OpenLiteSpeed 性能不佳和 SSL 握手失败 (5)

无法将简单的 "hellow world"与 Simd 库链接

python - Theano : MissingGxx , g++ 不可用