fprintf中,错误:格式不是字符串文字,没有格式参数[-Werror =格式的安全性 [英] fprintf, error: format not a string literal and no format arguments [-Werror=format-security

查看:2829
本文介绍了fprintf中,错误:格式不是字符串文字,没有格式参数[-Werror =格式的安全性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译 fprintf中(标准错误,用法)在Ubuntu我得到这个错误:

when I try to compile fprintf(stderr,Usage) on Ubuntu I got this error:

 error: format not a string literal and no format arguments [-Werror=format-security

但是当我编译的其它Linux发行版(红帽的Fedora,SUSE)已成功编译。

but when I compiled that on other linux distributions (RedHat, Fedora, SUSE) that is compiled successfully.

任何人有一个想法?

推荐答案

您应该使用的fputs(用法,标准错误);

有没有必要使用fprintf中如果arn't做格式。如果你想使用fprintf中,使用 fprintf中(标准错误,%S,用法);

There is no need to use fprintf if you arn't doing formatting. If you want to use fprintf, use fprintf(stderr, "%s", Usage);

借助默认的编译器标志在Ubuntu 中包括 -Wformat -Wformat安全这是什么让这个错误。

The default compiler flags on Ubuntu includes -Wformat -Wformat-security which is what gives this error.

这标志用作对引入安全相关的bug一个precaution,想象一下
如果你确实不知何故会发生这种情况:

That flag is used as a precaution against introducing security related bugs, imagine what would happen if you somehow did this:

char *Usage = "Usage %s, [options] ... ";
...
fprintf(stderr, Usage);

这将是相同
fprintf中(标准错误,用法%S,[选项] ...]); 这是错误的。

This would be the same as fprintf(stderr, "Usage %s, [options] ... ]"); which is wrong.

现在的用法串包括格式说明,%S ,但你不提供参数 fprintf中,导致不确定的行为,有可能崩溃的程序或允许它被利用。这是更相关的,如果你传递给fprintf中的字符串来自于用户的输入。

Now the Usage string includes a format specifier, %s, but you do not provide that argument to fprintf, resulting in undefined behavior, possibly crashing your program or allowing it to be exploited. This is more relevant if the string you pass to fprintf comes from user input.

但是,如果你 fprintf中(标准错误,%S,用法%S,[选项] ...]); 有没有这样的问题。该2. %S 不会PTED作为格式specifer间$ P $。
GCC可以警告这一点,Ubuntu默认编译器标志使得编译错误。

But if you do fprintf(stderr,"%s", "Usage %s, [options] ... ]"); There is no such problem. The 2. %s will not be interpreted as a format specifer. gcc can warn about this, and the default Ubuntu compiler flags makes it a compiler error.

这篇关于fprintf中,错误:格式不是字符串文字,没有格式参数[-Werror =格式的安全性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆