-fpermissive部分错误降低为警告--谨慎使用

Posted 奇妙之二进制

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了-fpermissive部分错误降低为警告--谨慎使用相关的知识,希望对你有一定的参考价值。

man手册该选项的说明如下:

   -fpermissive
           Downgrade some diagnostics about nonconformant code from errors to warnings.  Thus, using -fpermissive will allow some nonconforming code to compile.

该选项会将不一致代码的诊断从错误降级为警告。

该选项最好不要使用,因为会降低对于代码检查的严格性。

例子:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const static char *source = "source";

int main(int argc, char *argv[])

	char buf[512];
	snprintf(buf, "%s", source);
	return 0;

在该代码中,snprintf并没有使用第二个参数sizeof(buf),如果不使用该选项,编译结果如下所示:

syrius@AI-MACHINE:~ $ g++ g.cc     
g.cc: In function ‘int main(int, char**)’:
g.cc:10:23: error: invalid conversion from ‘const char*’ to ‘size_t’ aka ‘long unsigned int’ [-fpermissive]
   10 |         snprintf(buf, "%s", source);
      |                       ^~~~
      |                       |
      |                       const char*
In file included from g.cc:1:
/usr/include/stdio.h:378:51: note:   initializing argument 2 of ‘int snprintf(char*, size_t, const char*, ...)378 | extern int snprintf (char *__restrict __s, size_t __maxlen,
      |                                            ~~~~~~~^~~~~~~~
g.cc:10:17: warning: format not a string literal and no format arguments [-Wformat-security]
   10 |         snprintf(buf, "%s", source);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~
syrius@AI-MACHINE:~ $                                                                                                             
syrius@AI-MACHINE:~ $ g++ g.cc -fpermissive                                                                                       
g.cc: In function ‘int main(int, char**)’:
g.cc:10:23: warning: invalid conversion from ‘const char*’ to ‘size_t’ aka ‘long unsigned int’ [-fpermissive]
   10 |         snprintf(buf, "%s", source);
      |                       ^~~~
      |                       |
      |                       const char*
In file included from g.cc:1:
/usr/include/stdio.h:378:51: note:   initializing argument 2 of ‘int snprintf(char*, size_t, const char*, ...)378 | extern int snprintf (char *__restrict __s, size_t __maxlen,
      |                                            ~~~~~~~^~~~~~~~
g.cc:10:17: warning: format not a string literal and no format arguments [-Wformat-security]
   10 |         snprintf(buf, "%s", source);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~

以上是关于-fpermissive部分错误降低为警告--谨慎使用的主要内容,如果未能解决你的问题,请参考以下文章

字符数组的错误初始化字符串太长 [-fpermissive]

错误:从“uint8_t* aka unsigned char*”转换为“unsigned int”会丢失精度 [-fpermissive]

二叉树插入()const fpermissive错误

出现错误:“ISO C++ 禁止指针和整数 [-fpermissive] 之间的比较”如何解决?

为啥此代码会出现 -fpermissive 错误?

奇怪的错误,错误:从‘const char*’到‘char’的无效转换[-fpermissive]