c - gets 有用吗?

标签 c history standard-library gets

<分区>

在我看来,人们,尤其是在学习 C 编程语言时,仍在使用 gets从标准输入读取数据的函数。尽管如此,它现在已从 C11 标准中删除1,并且在 cppreference 上有免责声明。阅读:

The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and gets_s() are the recommended replacements.

Never use gets().

然而,这似乎并不是一个新问题,它是由更现代的编程哲学提出来的。它总是会被破坏并导致程序崩溃,我不明白“限制标准输入上出现的内容的环境”可能意味着什么。

那么,它在过去曾经有用吗?或者它被添加到以前的标准和 C 的准标准版本中的原因是什么?


(1) ... 或至少更改为具有一个附加参数,指示要读取的最大长度。然而,我询问的是旧签名,只收到一个指针。

最佳答案

是的,它很有用,同时“极易受到缓冲区溢出攻击”。

It would have always been broken and have caused programs to crash and I don't see what could possibly be meant by an "environment which restricts what can appear on stdin".

不,gets 不会导致程序崩溃。这主要是一个安全问题。您可以阅读有关缓冲区溢出攻击的信息 here

另请参阅此问题:Why is the gets function so dangerous that it should not be used?

关于c - gets 有用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15225565/

相关文章:

http - ELM-LANG 如何在单击浏览器后退按钮时发出 http 请求

git - 如何在提交后删除本地 git 历史记录?

c - 多键一值数据结构 (C)

c - 在C中输出两个二进制字符串及其十进制值相加的结果

缓存模拟器::命中与未命中问题

jquery - 页面加载时滚动到给定的 jCarousel 幻灯片

c++ - 在没有嵌套 for 循环的情况下查找另一个 vector 中 vector 条目的出现

python - 中止 python 交互式控制台的评估

c - BMI计算器不工作

c - 闪烁问题但不想使用 CLIPCHILDREN 风格