Bug 426445 - GCC 4.1.2 Reports Range Warnings for Const Buildin Types
GCC 4.1.2 Reports Range Warnings for Const Buildin Types
Product: Fedora
Classification: Fedora
Component: gcc4 (Show other bugs)
i686 Linux
low Severity low
: ---
: ---
Assigned To: Jakub Jelinek
Depends On:
  Show dependency treegraph
Reported: 2007-12-20 21:27 EST by Shea Tomsin
Modified: 2008-01-29 08:54 EST (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2008-01-29 08:54:27 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Shea Tomsin 2007-12-20 21:27:02 EST
const unsigned short min = 0, max = 10;

int main(int argc,char* argv[]) {
    unsigned short      test = 0;
    if ( (test >= min) && (test <= max) )
        return 0;
        return 1;

$ g++ -o test main.cpp
has the following error:
main.cpp: In function âint main(int, char**)â:
main.cpp:6: warning: comparison is always true due to limited range of data type

$ gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk
--disable-dssi --enable-plugin
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-cpu=generic
Thread model: posix
gcc version 4.1.2 20070925 (Red Hat 4.1.2-27)

Notes:  If I remove the const declaration, then no warning--why?
Comment 1 Jakub Jelinek 2008-01-29 08:54:27 EST
Because if the min variable is not const, 0 is just an initializer and the
comparison must be done at runtime, as it could have been changed by other
compilation units (e.g. in constructors).
When it is const, there is test >= 0 comparison were test is unsigned short.
Which is always true, there are no negative unsigned values.

Note You need to log in before you can comment on or make changes to this bug.