GCC严格溢出

时间:2016-09-10 00:40:34

标签: c++ g++ gcc-warning

我们最近为-Wstrict-overflow=5打开了一个大型代码库,并且正在尝试了解启用优化时的~500个警告。有些似乎是合法的,但有些事情是这样的:

std::vector<std::string> files;
...
void Add (const std::string file)
{
  if (std::find(files.begin(), files.end(), file) == files.end())
  {
    files.push_back(file);
  }
}

产生警告:

example.cc: In member function ‘void Add(std::string)’:
example.cc:465:8: error: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Werror=strict-overflow]
void Add (const std::string file)
    ^

我假设比较在std::find(),并通过内联Add()函数公开。

我该怎么解决这个问题?

是的,我已经阅读了其他SO问题,没有什么帮助:

std::find上的

23020208 std::set。答:GCC错误,关闭警告

18521501重构条件

签名整数中的

22798709边缘情况

1 个答案:

答案 0 :(得分:1)

  

我该如何解决这个问题?

由于它们是由你无法控制的东西(即gcc)引起的误报,你需要适应它:

  1. 一个接一个(这就是为什么你首先启用它们,以检测可能发生溢出的地方,对吧?)
  2. 对于编译器正确的地方,应用更正
  3. 对于误报的地方,使用#pragma在本地禁用警告 - #pragma的存在意味着:&#34;支付到期费用,我检查了那里&#39;在这里,某些东西不会溢出&#34;。
  4. serenity prayer可能有帮助)

相关问题