如何禁用关于在 GCC 中使用已弃用的获取的警告?

How to disable the warning about using deprecated gets in GCC?

我正在 运行 编写一个 CTF,我目前正在编写一个利用 C 的 gets 函数的问题。我知道该函数已被弃用且很危险,我绝不会在任何其他情况下使用它。不幸的是,gcc 编译了我的代码,当我 运行 二进制文件时 gets 函数被命中,我收到一条友好的错误消息:

warning: this program uses gets(), which is unsafe.

这通常很好,因为它会警告您 gets 是不安全的,但不幸的是,在我的 CTF 中,我认为这个错误消息使问题变得有点太简单了。您知道我将如何禁用此警告吗?谢谢!

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

注意:我刚刚意识到您的问题标题似乎放错了地方 - 您收到的警告来自 macOS,关于执行使用 gets() 的程序。与使用GCC编译无关

:-/ 无论如何,我把我的答案留作参考。

就像评论一样:我用谷歌搜索了一下您要找的东西,但似乎没有可靠的方法可以在执行程序时禁用此警告。有人建议重建 /usr/lib/libSystem.B.dylib 如果它确实有效,没有任何结果或经验,但我个人认为这有点过于极端,甚至可能有害。 - 我不推荐这种技术。

如果您真的想创建一个漏洞利用程序,请尝试通过 costum-made 函数重建 gets() 并为函数命名,例如 f.e。 gets_c()。这应该是从 macOS 禁用此警告的解决方法。


旧答案(关于 GCC 本身):

首先,您似乎使用的是 C99 或 C89/C90-compliant 编译器,或者使用 std=c99std=c89/std=c90 选项进行编译,因为只有编译器符合C11 之前的标准警告 gets() 被弃用。

ISO/IEC 删除了 C11 中的 gets() 函数。如果您使用 C11 或更新的 standard-compliant 编译器进行编译,那么在代码中使用 gets() 的隐式声明时会出现错误:

"error: implicit declaration of function 'gets'; did you mean 'fgets'? [-Werror=implicit-function-declaration]"


如果您想在编译时抑制警告,请在编译时使用 -Wno-deprecated-declarations 选项来禁用对弃用声明的诊断。

来自 GCC 在线文档:

-Wno-deprecated-declarations

Do not warn about uses of functions, variables, and types marked as deprecated by using the deprecated attribute. (see Function Attributes, see Variable Attributes, see Type Attributes.)

Source: https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Warning-Options.html

如果您想在代码中嵌入对警告的抑制,请使用 David 已删除的答案中使用的方法,通过使用 #pragma:

实现对 -Wno-deprecated-declarations 的抑制
   char str[256];

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
    gets(str);
#pragma GCC diagnostic pop