假设 if 语句中没有发生有符号溢出

assuming signed overflow does not occur in if statement(假设 if 语句中没有发生有符号溢出)

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

问题描述

为什么会出现此警告?如果我检查界限,这并不是一个真正的假设.以及如何解决?

如果 num_actions_to_skip 设置为 1,而不是 2,则错误消失.

谢谢

错误:假设 (X - c) <= X 始终为真 [-Werror=strict-overflow]cc1plus:所有警告都被视为错误

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

const QList&action_list = tool_menu->actions();静态常量 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),行动);}

它开始于

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

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

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

解决方案

从此GCC 资源:

<块引用>

-Wstrict-溢出

-Wstrict-overflow=n

此选项仅在 -fstrict-overflow 处于活动状态时才处于活动状态.它警告编译器基于不发生带符号溢出的假设进行优化的情况.请注意,它不会警告代码可能溢出的所有情况:它只警告编译器实现某些优化的情况.因此,此警告取决于优化级别.

如果所涉及的变量的值使得溢出实际上永远不会发生,则假设不会发生有符号溢出的优化是完全安全的.因此,此警告很容易给出误报:关于实际上不是问题的代码的警告.为了帮助关注重要问题,定义了几个警告级别.在估计循环需要多少次迭代时,特别是在确定是否要执行循环时,不会针对使用未定义的有符号溢出发出警告.

<小时><块引用>

-Wstrict-overflow=1

对可疑且易于避免的情况发出警告.例如,使用 -fstrict-overflow,编译器将 x + 1 > x 简化为 1. -Wstrict-overflow 的这一级别由 -Wall 启用;更高级别不是,必须明确要求.

-Wstrict-overflow=2

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

-Wstrict-overflow=3

还警告其他简化比较的情况.例如: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 语句中没有发生有符号溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:假设 if 语句中没有发生有符号溢出

基础教程推荐