Bug 426445 - GCC 4.1.2 Reports Range Warnings for Const Buildin Types
Summary: GCC 4.1.2 Reports Range Warnings for Const Buildin Types
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: gcc4
Version: 7
Hardware: i686
OS: Linux
low
low
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2007-12-21 02:27 UTC by Shea Tomsin
Modified: 2008-01-29 13:54 UTC (History)
0 users

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2008-01-29 13:54:27 UTC
Type: ---
Embargoed:


Attachments (Terms of Use)

Description Shea Tomsin 2007-12-21 02:27:02 UTC
const unsigned short min = 0, max = 10;

int main(int argc,char* argv[]) {
    unsigned short      test = 0;
    if ( (test >= min) && (test <= max) )
        return 0;
    else
        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
--disable-libunwind-exceptions
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk
--disable-dssi --enable-plugin
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-cpu=generic
--host=i386-redhat-linux
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 13:54:27 UTC
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.