Warning: comparison is always incorrect due to the limited range of data types in gcc 4.1.2

I met the following warning from gcc 4.1.2:

warning: comparison is always false due to limited range of data type 

the corresponding C code is like:

 if ( unlikely(count < 0) ) BUG(); 

where 'count' is not specified.

I tried disabling the warning because I was not allowed to modify the source code:

 -Wno-type-limits 

but it seems gcc 4.1.2 does not support it.

 cc1: error: unrecognized command line option "-Wno-type-limits" 

Any other ways to get rid of this warning?

+4
source share
4 answers

An unsigned value will never be negative - hence a warning. It is not as "unlikely" as "impossible."

This usually indicates that there is an error in the code; the code was written expecting a type that can allow negative values, but the type does not allow negative values. Thus, it is possible that the code will be erroneous due to mismatch of expectations.

Note that on some machines, plain char signed, and on others, unsigned (and this is a type different from both signed char and unsigned char , although its range of values ​​overlaps with one or the other).

+8
source

Depending on how old the source code is, it can be written in a defensive way for a time when the compilers were not as type safe as gcc is now.

The warning looks like part of the -Wextra (aka -W) warning parameter, so if you need additional warnings, this will be one of them. I personally use -Wall, which they believe or not, do not include "extra" things. You would think that “everyone” will include “extra”, but I think not ...

+2
source

To fix this warning:

  • remove a condition that is always true

  • best: remove the condition, which is always true + add static_assert to ensure that the type is unsigned, (for version C static_assert see here )

  • for gcc before 4.3: remove the compiler option: -Wextra

  • for gcc 4.3+ add option: -Wno-type-limits

0
source

The variable used in the comparison was declared as an 8-bit unsigned int. When I changed it to 32-bit unsigned, the warning was removed. Old: uint8_t variable = 0; Fix: uint32_t variable = 0;

0
source

Source: https://habr.com/ru/post/1469106/


All Articles