如何禁用有关在GCC中使用已弃用的Get的警告?

问题描述 投票:0回答:2

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

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

这通常很好,因为它警告您这样做是不安全的,但是很遗憾,在我的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
c gcc exploit gcc-warning gets
2个回答
0
投票

如果您在C99以下,可以尝试:

    char str[256];

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

0
投票

首先,您似乎正在使用兼容C99或C89 / C90的编译器,或者使用std=c99std=c89 / std=c90选项进行编译,因为仅编译器符合C11之前的标准,警告gets()。不推荐使用。

ISO / IEC删除了C11中的gets()功能。如果使用C11或更新的符合标准的编译器进行编译,则在代码中使用gets()的隐式声明时会出现错误:

错误:函数'gets'的隐式声明;您的意思是'fgets'?[-Werror=implicit-function-declaration]


如果要在编译时禁止显示警告,请在编译时使用-Wno-deprecated-declarations选项为已弃用的声明禁用诊断。

从GCC在线文档:

-Wno弃用的声明

不要警告使用不赞成使用的属性标记为不赞成使用的函数,变量和类型。 (请参见函数属性,请参见变量属性,请参见类型属性。)

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

如果要在代码中嵌入警告抑制,请使用David´s answer中的方法,并通过使用-Wno-deprecated-declarations实现对#pragma的抑制。

© www.soinside.com 2019 - 2024. All rights reserved.