Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 42547 - Inconsistent warning: comparison is always true due to limited range of data type for uint16_t to uint16_t comparison
Summary: Inconsistent warning: comparison is always true due to limited range of data...
Status: RESOLVED INVALID
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] GCC Porting (show other bugs)
Hardware: x86 Linux
: High minor (vote)
Assignee: Please assign to toolchain
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-02-22 22:34 UTC by Stephen Torri
Modified: 2004-02-23 06:38 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stephen Torri 2004-02-22 22:34:51 UTC
Unable to find a logical reason behind why a <= operator gives a warning when the < and == operators do not. I have inclduded a test program which produces the warning when the program is compiled.

Reproducible: Always
Steps to Reproduce:
1.Make app: g++ -W -Wall -o test test.cpp (warning will be visible)

Actual Results:  
g++ -o test test.cpp
test.cpp: In function `int main(int, char*)':
test.cpp:25: warning: comparison is always true due to limited range of data 
   type

Expected Results:  
g++ -o test test.cpp
(No warning)

#include <inttypes.h>
#include <iostream>

static const uint16_t LO_MAC = 0xFF00;
static const uint16_t HI_MAC = 0xFFFF;

int main (int, char*) {

  uint16_t b = 0xFFFF;
  if (b >= LO_MAC) {
    std::cout << "[OK] Low MAC: " << b << std::endl;
  }

  b = 0;
  if (b < HI_MAC) {
    std::cout << "[OK] Less than HI_MAC: " << b << std::endl;
  }

  b = HI_MAC;
  if (b == HI_MAC) {
    std::cout << "[OK] Equal to HI_MAC: " << b << std::endl;
  }

  b = 50;
  if (b <= HI_MAC) {
    std::cout << "[COMPILE WARNING] High MAC: " << b << std::endl;
  }
  return 0;
}
Comment 1 Stephen Torri 2004-02-23 06:38:33 UTC
Since a uint16_t cannot have a value greater than 0xFFFF the last check is correct. The problem was more programmer misunderstanding of type limits.