禁用警告:“获取” 通过头文件在 GCC 中的函数是危险的吗?

发布于 2024-07-29 17:57:01 字数 306 浏览 5 评论 0原文

我在 C 代码中使用函数 gets() 。 我的代码工作正常,但我收到一条警告消息,

(.text+0xe6): warning: the `gets' function is dangerous and should not be used.

我希望不要弹出此警告消息。 有什么办法吗?

我想知道通过创建一个头文件来禁用某些警告是否可能存在这种可能性。 或者编译过程中是否有任何选项可以满足我的目的? 或者可能有一种特殊的方式使用 gets() 来避免弹出此警告?

I am using the function gets() in my C code.
My code is working fine but I am getting a warning message

(.text+0xe6): warning: the `gets' function is dangerous and should not be used.

I want this warning message not to pop up. Is there any way?

I am wondering that there might be such possibilities by creating a header file for disabling some warnings. Or is there any option during compiling that can serve my purpose? Or may be there is a particular way of using gets() for this warning not to pop up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

沒落の蓅哖 2024-08-05 17:57:01

显而易见的答案是从编译器试图告诉您的内容中学习 - 您永远不应该使用 gets(),因为它完全不安全。 使用 fgets() 代替,这样可以防止可能的缓冲区溢出。

#define BUFFER_SIZE 100
char buff[BUFFER_SIZE];
gets( buff);   // unsafe!
fgets( buff, sizeof(buff), stdin );   // safe

The obvious answer is to learn from what the compiler is trying to tell you - you should never, ever, use gets(), as it is totally unsafe. Use fgets() instead, which allows you to prevent possible buffer overruns.

#define BUFFER_SIZE 100
char buff[BUFFER_SIZE];
gets( buff);   // unsafe!
fgets( buff, sizeof(buff), stdin );   // safe
奢华的一滴泪 2024-08-05 17:57:01

如果你真的想用它。

这是答案来自: http://www.gamedev.net/community/ forums/topic.asp?topic_id=523641

如果您使用最新版本的 gcc,则可以使用:

#pragma GCC diagnostic ignored "your option here"

例如,如果这些标头产生“浮点比较不安全”错误,则可以使用:

#pragma GCC diagnostic ignored "-Wfloat-equal".

不幸的是,您不能以这种方式禁用“-Wall”(这太容易了,不是吗...),您必须手动执行 -Wall 启用的各个警告选项(至少是冲突的选项)。

文档: http://gcc.gnu.org/onlinedocs/gcc /Diagnostic-Pragmas.html#Diagnostic-Pragmas

编辑:
但它似乎不适用于收到警告...我在我的电脑上尝试过。

If you really want use it.

Here is answer From: http://www.gamedev.net/community/forums/topic.asp?topic_id=523641

If you use a reasonably recent version of gcc, you can use:

#pragma GCC diagnostic ignored "your option here"

For example, if those headers produce a "floating point comparison is unsafe" error, you would use:

#pragma GCC diagnostic ignored "-Wfloat-equal".

Unluckily, you cannot disable "-Wall" that way (that would be too easy, wouldn't it...), you have to do the individual warning options which -Wall enables by hand (at least, the conflicting ones).

Docs: http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas

EDIT:
But it seems not work for gets warning... I tried on my pc.

蓝眼泪 2024-08-05 17:57:01

使用 fgets() 而不是 gets()

char buffer[BUFSIZ];
/* gets(buffer); */
fgets(buffer,sizeof(buffer), stdin);

gets() 函数不检查缓冲区的长度,并且可以写入超出末尾并更改堆栈。 这就是您听到的“缓冲区溢出”。

Use fgets() instead of gets()

char buffer[BUFSIZ];
/* gets(buffer); */
fgets(buffer,sizeof(buffer), stdin);

The gets() function does not check the length of buffer and can write past the end and alter the stack. This is the "buffer overflow" you hear about.

七月上 2024-08-05 17:57:01

我会留意警告并替换 gets。 这对我来说已经足够清楚了:

错误

切勿使用 gets()。 因为如果不知道里面的数据就无法判断
提前 gets() 将读取多少个字符,并且因为 gets() 将继续存储
字符超过缓冲区末尾,使用起来极其危险。 它有
被用来破坏计算机安全。 请改用 fgets()。

I would heed the warning and replace gets. This is clear enough for me:

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.

绮烟 2024-08-05 17:57:01

确实没有充分的理由使用 gets()。 甚至 C 标准也说它已经过时了! 请改用fgets()

[编辑]

看起来警告来自链接器。 使用 -c 编译时是否收到警告? (这会禁用链接。)

There really is no good reason to use gets(). Even the C standard says it's obsolescent! Use fgets() instead.

[Edit]

It looks like the warning comes from the linker. Do you get warning when compiling with -c? (Which disables linking.)

下壹個目標 2024-08-05 17:57:01

您根本不应该使用 gets 函数,联机帮助页上说要使用 fgets 来代替。

GCC 不提供使用编译指示禁用警告的功能。 您必须使用各种警告选项作为编译器代替。

You shouldn't use the gets function at all, the manpage says to use fgets instead.

GCC does not provide the functionality that GCC does to disable warnings using pragmas. You must use the various warning options as flags to the compiler instead.

三生一梦 2024-08-05 17:57:01

-fno-stack-protector 是一个允许使用 gets() 函数的选项,尽管它非常不安全。

-Wno-deprecated-declarations 关闭弃用警告

这是一个使用 gets() 进行编译的示例

gcc myprogram.c -o myprogram -fno-stack-protector -Wno-deprecated-declarations

我同意每个人的说法,因为它会允许程序完全不安全超出缓冲区。 这可能非常危险,因此它已被弃用,取而代之的是 fgets。

但是,如果您正在学习安全性课程,那么能够编写一个小型测试程序来研究缓冲区溢出和堆栈溢出的概念是非常有帮助的。

-fno-stack-protector is an option that allows the gets() function to be used in spite of how unsafe it is.

-Wno-deprecated-declarations turns off the deprecation warning

Here's an example of compiling with gets()

gcc myprogram.c -o myprogram -fno-stack-protector -Wno-deprecated-declarations

I agree with everyone who says that it's completely unsafe since it will allow a program to overrun a buffer. This can be quite dangerous and hence the reason it has been deprecated in favor of fgets.

However, if you're taking an intro to security course, being able to write a small test program to play with the concepts of buffer overrun and stack overflows is very helpful.

我最亲爱的 2024-08-05 17:57:01

建议使用 gets() 的安全替代品。

在现有代码中,要替代 gets(),可能不需要使用 fgets(),因为该函数需要额外的 char 来保存两个函数都会使用 '\n',但 gets() 不会保存。 以下是不需要更大缓冲区大小的替代方案。

每个 gets(dest) 替换为:
如果 dest 是数组,请使用 gets_sz(dest, sizeof dest)
如果 dest 是指向大小为 nchar 数组的指针,请使用 gets_sz(dest, n)

char *gets_sz(char *dest, size_t size) {
    if (size <= 1) {
        if (size <= 0 || feof(stdin)) {
            return NULL;
        }
    }
    size--;
    size_t i;
    for (i = 0; i < size; i++) {
        int ch = getchar();
        if (ch == EOF) {
            if (i == 0)
                return NULL;
            break;
        }
        if (ch == '\n')
            break;
        dest[i] = (char) ch;
    }
    dest[i] = 0;
    return dest;
}

Suggest a safe substitute for gets().

In existing code, to substitute gets(), it may not be desired to use fgets() as that function requires an additional char to save the '\n' which both functions consume, but gets() does not save. Following is a substitute that does not require a larger buffer size.

Each gets(dest) is replace with:
If dest is an array, use gets_sz(dest, sizeof dest)
If dest is a pointer to an char array of size n, use gets_sz(dest, n)

char *gets_sz(char *dest, size_t size) {
    if (size <= 1) {
        if (size <= 0 || feof(stdin)) {
            return NULL;
        }
    }
    size--;
    size_t i;
    for (i = 0; i < size; i++) {
        int ch = getchar();
        if (ch == EOF) {
            if (i == 0)
                return NULL;
            break;
        }
        if (ch == '\n')
            break;
        dest[i] = (char) ch;
    }
    dest[i] = 0;
    return dest;
}
儭儭莪哋寶赑 2024-08-05 17:57:01

如果您确实想使用它,请尝试使用 -fsyntax-only 标志。

gcc 网站中的手册说:

-fsyntax-only

检查代码是否有语法错误,但不要执行任何其他操作。 
  

If you really want to use it, try the flag -fsyntax-only.

The manual in gcc website says:

-fsyntax-only

Check the code for syntax errors, but don't do anything beyond that.
泛泛之交 2024-08-05 17:57:01

与流行的观点相反,并非所有程序员都同样不专心于他们所写的内容。 gets() 将始终是 C90 中的标准,并且出于几个充分的原因将其放入库中。 如果使用得当,它并不比任何其他字符串函数更“危险”,例如在程序示例、文档、单元测试脚手架、家庭作业等中。

更重要的是,gets() 在某种程度上增强了可读性fgets() 永远不会。 人们永远不必打断自己的思路来查找参数的放置顺序。

以下解决方法使用我最喜欢的另一个函数来删除换行符。 :)

 #define gets GET_LOST
 #include "stdio.h"
 #undef gets

 #include "limits.h"

 char *gets(char *s)
 {
    return strtok(fgets(s, INT_MAX, stdin), "\n");
 }

Contrary to popular opinion, not all programmers are equally inattentive to what they are writing. gets() will always be standard in C90, and it was put in the library for several good reasons. It's no more "dangerous" than any other string function when used appropriately, such as in program examples, documentation, unit test scaffolding, homework assignments, etc.

What's more, gets() enhances readability in a way that fgets() never will. And one never has to interrupt one's train of thought to look up what order to put its arguments in.

The following workaround uses my other favorite function to remove the newline. :)

 #define gets GET_LOST
 #include "stdio.h"
 #undef gets

 #include "limits.h"

 char *gets(char *s)
 {
    return strtok(fgets(s, INT_MAX, stdin), "\n");
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文