假设在if语句中不发生有符号溢出 [英] assuming signed overflow does not occur in if statement

查看:2134
本文介绍了假设在if语句中不发生有符号溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会出现此警告?如果我检查边界,这不是一个假设。如何修复?



如果 num_actions_to_skip 设置为1而不是2,则错误消失。 / p>

感谢

 错误:假设假设有符号溢出(X-c)<= X始终为真[-Werror =严格溢出] 
cc1plus:所有警告被视为错误

if(loc> = 0&& loc< action_list.count()){

  const QList< QAction *> & action_list = tool_menu-> actions(); 
static const int num_actions_to_skip = 2;
const int loc = action_list.count() - num_actions_to_skip;
if(loc> = 0&& loc< action_list.count()){
tool_menu-> insertAction(action_list.at(loc),
action);
}

开头为

  Q_ASSERT_X(i> = 0&& i< p.size()

在qlist.h:454,它执行相同的检查,并抛出此错误,只需

  tool_menu-> insertAction(action_list.at(action_list.count() -  2),
action);


解决方案

从此 GCC资源


-Wstrict-溢出



-Wstrict-overflow = n



此选项仅在-fstrict-警告编译器基于假设未发生有符号溢出的情况进行优化的情况。请注意,它不会对代码可能溢出的所有情况发出警告:它仅警告编译器实现某些优化的情况。因此,此警告取决于优化级别。



如果所涉及的变量的值使得溢出从未发生,那么假定不发生带符号溢出的优化是完全安全的,实际上,发生。因此,这个警告很容易给出一个假阳性:关于代码的警告,实际上不是问题。为了帮助关注重要问题,定义了几个警告级别。























$ b





-Wstrict-overflow = 1



警告有问题且容易避免的情况。例如,使用-fstrict-overflow,编译器将x + 1> x简化为1.这个级别的-Wstrict溢出由-Wall;



$ b

b

还警告其他将比较简化为常量的情况。例如:abs(x)> = 0.这只能在-fstrict-overflow有效时才能被简化,因为abs(INT_MIN)溢出到INT_MIN,它小于零。 -Wstrict-overflow(无级别)与-Wstrict-overflow = 2相同。



-Wstrict-overflow = 3 p>

也对其他简化比较的情况发出警告。例如:x + 1> 1简化为x> 0。



-Wstrict-overflow = 4



还警告上述情况未涵盖的其他简化。例如:(x * 10)/ 5简化为x * 2。



-Wstrict-overflow = 5



还警告编译器减少比较中涉及的常量的大小的情况。例如:x + 2> y简化为x + 1> = y。这仅在最高警告级别报告,因为此简化适用于许多比较,因此此警告级别给出非常大量的假阳性。



Why is this warning appearing? It's not really an assumption if I check the bounds. And how to fix?

If num_actions_to_skip is set to 1, instead of 2, the error goes away.

Thanks

error: assuming signed overflow does not occur when assuming that (X - c) <= X is always true [-Werror=strict-overflow]
cc1plus: all warnings being treated as errors

On if (loc >= 0 && loc < action_list.count()) {

const QList<QAction *> &action_list = tool_menu->actions();
static const int num_actions_to_skip = 2;
const int loc = action_list.count() - num_actions_to_skip;
if (loc >= 0 && loc < action_list.count()) {
    tool_menu->insertAction(action_list.at(loc),
                            action);
}

It started with

Q_ASSERT_X(i >= 0 && i < p.size()

at qlist.h:454, which performs the same check, and throws this error as well, with just

tool_menu->insertAction(action_list.at(action_list.count() - 2),
                                action);

解决方案

From this GCC resource:

-Wstrict-overflow

-Wstrict-overflow=n

This option is only active when -fstrict-overflow is active. It warns about cases where the compiler optimizes based on the assumption that signed overflow does not occur. Note that it does not warn about all cases where the code might overflow: it only warns about cases where the compiler implements some optimization. Thus this warning depends on the optimization level.

An optimization that assumes that signed overflow does not occur is perfectly safe if the values of the variables involved are such that overflow never does, in fact, occur. Therefore this warning can easily give a false positive: a warning about code that is not actually a problem. To help focus on important issues, several warning levels are defined. No warnings are issued for the use of undefined signed overflow when estimating how many iterations a loop requires, in particular when determining whether a loop will be executed at all.


-Wstrict-overflow=1

Warn about cases that are both questionable and easy to avoid. For example, with -fstrict-overflow, the compiler simplifies x + 1 > x to 1. This level of -Wstrict-overflow is enabled by -Wall; higher levels are not, and must be explicitly requested.

-Wstrict-overflow=2

Also warn about other cases where a comparison is simplified to a constant. For example: abs (x) >= 0. This can only be simplified when -fstrict-overflow is in effect, because abs (INT_MIN) overflows to INT_MIN, which is less than zero. -Wstrict-overflow (with no level) is the same as -Wstrict-overflow=2.

-Wstrict-overflow=3

Also warn about other cases where a comparison is simplified. For example: x + 1 > 1 is simplified to x > 0.

-Wstrict-overflow=4

Also warn about other simplifications not covered by the above cases. For example: (x * 10) / 5 is simplified to x * 2.

-Wstrict-overflow=5

Also warn about cases where the compiler reduces the magnitude of a constant involved in a comparison. For example: x + 2 > y is simplified to x + 1 >= y. This is reported only at the highest warning level because this simplification applies to many comparisons, so this warning level gives a very large number of false positives.

这篇关于假设在if语句中不发生有符号溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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