6

I encountered the following warning from gcc 4.1.2:

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

the concerned C code is like:

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

where 'count' is unsigned.

I tried to disable the warning since 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?

user1783732
  • 1,599
  • 5
  • 22
  • 44
  • 4
    Why not keep the warning? Maybe the owner of the source code needs to see it; also getting rid of it could mask other bugs. –  Mar 14 '13 at 00:18
  • Are you stuck with this code? Is it generated code? – minopret Mar 14 '13 at 00:25
  • 1
    I think that it is GCC, not GDB, that generates the warning. GDB version 4 is a lot more ancient than GCC 4.1.2 (though GCC 4.1.2 is fairly old, perhaps circa 2006). – Jonathan Leffler Mar 14 '13 at 00:32

4 Answers4

9

An unsigned value will never be negative — hence the warning. It's not so much 'unlikely' as 'impossible'.

This usually indicates that there is a bug in the code of some sort; the code was written expecting a type that could allow negative values, but the type doesn't allow negative values. So, it is quite possible that the code will misbehave because of the mismatch in expectations.

Note that on some machines, plain char is signed and on others unsigned (and it is a type distinct from both signed char and unsigned char even though its range of values overlaps with one or the other).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Is it your code or someone else's code? If it is your code, under what circumstances is the type of `count` a signed quantity? Can you make the test conditional on the type of `count` being signed? If it is someone else's code, have you asked them about it? Have you considered upgrading to a more recent compiler — or is that not allowed, either? Have you considered compiling without `-Wall`? If you can't modify the code, there's no virtue in seeing the warnings because you aren't going to be able to do anything about them. Also, it's a warning; it's ignorable — why don't you just ignore it? – Jonathan Leffler Mar 14 '13 at 18:55
4

options to fix this warning:

  1. remove condition which alway true

  2. best: remove condition which alway true + add static_assert to ensure type is unsigned. (for C version of static_assert look here)

  3. for gcc prior 4.3: remove compiler option: -Wextra

  4. for gcc 4.3+ add option: -Wno-type-limits

Community
  • 1
  • 1
Maxim Kholyavkin
  • 4,463
  • 2
  • 37
  • 82
2

Depending on how old the source code is, it could be written in a defensive manner for a time where compilers were not as type-safe as gcc is now.

The warning looks like it's part of the -Wextra (aka -W) warning option set, so if you want the extra warnings, that will be one of them. I personally use -Wall, which believe it or not does not include the "extra" stuff. You would think that "all" would include "extra" but I guess not...

SeKa
  • 1,825
  • 12
  • 7
0

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

PriyaGp
  • 41
  • 2
  • Question is how your uint32_t is defined ? With uint32_t from stdint.h it is highly unlikely to help as uint32_t is unsigned as well as uint8_t . – Lazureus Feb 27 '19 at 07:47