[Bug c/60614] New: -Wtype-limits fails to warn on unsigned bitfields

hjp at liab dot dk gcc-bugzilla@gcc.gnu.org
Fri Mar 21 15:10:00 GMT 2014


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60614

            Bug ID: 60614
           Summary: -Wtype-limits fails to warn on unsigned bitfields
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hjp at liab dot dk

This is possibly related to #54787, but with a different case.

After finding a bug in code compiled with avr-gcc, I created this test case, as
no warning was issued when I expected it. The following test case is tested on:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc-4.8-20140206/configure --prefix=/usr
--libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared
--enable-threads=posix --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch
--disable-libssp --enable-gnu-unique-object --enable-linker-build-id
--enable-cloog-backend=isl --disable-cloog-version-check --enable-lto
--enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu
--disable-multilib --disable-werror --enable-checking=release
Thread model: posix
gcc version 4.8.2 20140206 (prerelease) (GCC)

Test case:
#include <stdio.h>

struct {
    unsigned char field1 :3;
    unsigned char field2 :5;
} teststruct;

int main ( void ) {

    unsigned char test;

    if (teststruct.field1 < 0) //issues no warning
        printf("Field1 was negative\n");

    if (test < 0) //issues warning
        printf("Test was negative\n");

    return 0;
}


// Compilation:
$ gcc -Wall -Wextra test.c -o test
test.c: In function ‘main’:
test.c:17:2: warning: comparison is always false due to limited range of data
type [-Wtype-limits]
  if (test < 0)
  ^


==
The warning is not issued on test for negative unsigned bitfield as I expected
it.


More information about the Gcc-bugs mailing list