为什么要使用"-flto"静默GCC的警告“字符串截断" [英] Why "-flto" silent GCC's warning "stringop-truncation"

查看:603
本文介绍了为什么要使用"-flto"静默GCC的警告“字符串截断"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,较新的GCC版本引入了针对可能错误的字符串操作"stringop-truncation"的警告

I understand that newer GCC versions introduced a warning for possibly wrong string operations "stringop-truncation"

这是我可以轻松触发此警告的示例代码:

And here's the sample code that I can easily trigger this warning:

$ cat strncpy-warning.cxx
#include <cstring>

extern char g_buf[16];

void mycopy ( const char* src_c_str )
{
    strncpy ( g_buf, src_c_str, sizeof ( g_buf ) );
}

使用以下标志对其进行编译会触发此警告:

Compile it with the following flags triggers this warning:

$ g++ --version
g++ (GCC) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -Wall -std=c++14 -Wextra -Werror -O3 -c strncpy-warning.cxx
strncpy-warning.cxx: In function ‘void mycopy(const char*)’:
strncpy-warning.cxx:7:13: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound 16 equals destination size [-Werror=stringop-truncation]
     strncpy ( g_buf, src_c_str, sizeof ( g_buf ) );
     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors

但是,如果我在标记中添加了 -flto ,则会出现以下警告并生成目标文件:

Which is expected, however, if I added an -flto to the flags, this warning is gone and the object file is generated:

$ g++ -Wall -std=c++14 -Wextra -Werror -O3 -c strncpy-warning.cxx -flto
$ ls strncpy-warning.o
strncpy-warning.o

显然, -flto 会捕获一些编译时警告,因此不确定为什么未捕获 stringop-truncation ,例如:

Apparently, -flto WILL catch some compile time warnings so not sure why that stringop-truncation is not caught, e.g.:

$ cat strncpy-warning.cxx
#include <cstring>

extern char g_buf[16];

void mycopy ( const char* src_c_str, const char* unused )
{
    strncpy ( g_buf, src_c_str, sizeof ( g_buf ) );
}

$ g++ -Wall -std=c++14 -Wextra -Werror -O3 -c strncpy-warning.cxx -flto
strncpy-warning.cxx: In function ‘void mycopy(const char*, const char*)’:
strncpy-warning.cxx:5:50: error: unused parameter ‘unused’ [-Werror=unused-parameter]
 void mycopy ( const char* src_c_str, const char* unused )
                                      ~~~~~~~~~~~~^~~~~~
cc1plus: all warnings being treated as errors                                               

所以问题是:

  1. 这是预期的行为吗?
  2. 我了解为源文件指定了 -flto ,GCC会将特殊内容写入生成的目标文件( *.o ),但是问题是为什么该警告是否被海湾合作委员会跳过?那是故意的吗?
  3. 即使给出了 -flto ,是否有办法启用此警告?
  4. 还是以上第3点有意义?
  1. Is this expected behavior?
  2. I understand that with -flto specified for a source file, GCC will write special things into the resulting object file ( *.o ), but the question is why the warning is skipped by GCC? Is that intentional?
  3. Is there a way to enable this warning even in case of -flto is given?
  4. Or does the point-3 above make any sense?

非常感谢!

推荐答案

粗略地说,GCC有两种警告:

Roughly speaking, GCC has two kinds of warnings:

  • 通过简单分析源代码生成的警告
  • 对数据流进行复杂分析而生成的警告

未使用的参数是第一类:可以很简单地看到一个函数中根本没有使用参数.

An unused parameter is the first kind: it's trivial to see that a parameter is simply not used in a function.

strncpy 的用法不正确,除了琐碎的情况外,第二种是:编译器需要了解 strncpy 的第一个和第三个参数之间的关系,它需要知道在第三个参数中传递的实际(或至少符号)值,并且它需要知道在第一个参数中可用的实际(或至少符号)值.

Incorrect usage of strncpy is, except for trivial cases, the second kind: the compiler needs to understand the relationship between the first and third arguments to strncpy, it needs to know the actual (or at least symbolic) value passed in the third argument, and it needs to know the actual (or at least symbolic) size available in the first argument.

第一种警告是由前端解析器生成的.因此,它总是会触发.

The first kind of warning is generated by the frontend parser. Thus, it always fires.

第二种是由优化代码的同一组件生成的,因为只有该组件才具有必要的信息.在 -flto 模式下,此组件不会针对单个源文件运行.

The second kind is generated by the same component that optimizes the code, because only this component has the necessary information. In -flto mode, this component doesn't run for individual source files.

第二种方法有时也完全取决于执行的特定优化:如果将第一个命令行中的优化级别更改为 -O1 ,则也不会发出警告.

The second kind also sometimes depends on specific optimizations being performed at all: if you change the optimization level in your first command line to -O1, the warning isn't emitted either.

这篇关于为什么要使用"-flto"静默GCC的警告“字符串截断"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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