[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#592649: gcc-4.4: seemingly incorrect behaviour with switch on out-of-range enum value



Package: gcc-4.4
Version: 4.4.4-8
Severity: important


Running the following code with gcc-4.4 will print "A" even though the
value of bar in is actually 64 at runtime.

The correct behaviour is surely to print "invalid bar"?

As you can see from the code, adding a default case changes the
behaviour which I find even more strange.

Looking at the asm it seems that gcc is making as assumsion that
bar must be in the valid range of the enum.

This problem does not exist in gcc-4.3

#include <stdio.h>

enum foo
{
    A = 0,
    B = 1
};

int main()
{
    foo bar = (foo)64;

    switch (bar)
    {
        case A: printf("A\n"); return 0;
        case B: printf("B\n"); return 0;
        // uncomment this line to stop A being printed!
        //default: break;
    }

    printf("invalid bar\n");
    return 1;
}



-- System Information:
Debian Release: squeeze/sid
  APT prefers unstable
  APT policy: (500, 'unstable'), (1, 'experimental')
Architecture: amd64 (x86_64)

Kernel: Linux 2.6.33-2-amd64 (SMP w/2 CPU cores)
Locale: LANG=en_GB.UTF-8, LC_CTYPE=en_GB.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages gcc-4.4 depends on:
ii  binutils                      2.20.1-13  The GNU assembler, linker and bina
ii  cpp-4.4                       4.4.4-8    The GNU C preprocessor
ii  gcc-4.4-base                  4.4.4-8    The GNU Compiler Collection (base 
ii  libc6                         2.11.2-2   Embedded GNU C Library: Shared lib
ii  libgcc1                       1:4.4.4-8  GCC support library
ii  libgomp1                      4.4.4-8    GCC OpenMP (GOMP) support library

Versions of packages gcc-4.4 recommends:
ii  libc6-dev                     2.11.2-2   Embedded GNU C Library: Developmen

Versions of packages gcc-4.4 suggests:
ii  gcc-4.4-doc                  4.4.4.nf1-1 documentation for the GNU compiler
pn  gcc-4.4-locales              <none>      (no description available)
ii  gcc-4.4-multilib             4.4.4-8     The GNU C compiler (multilib files
pn  libcloog-ppl0                <none>      (no description available)
pn  libgcc1-dbg                  <none>      (no description available)
pn  libgomp1-dbg                 <none>      (no description available)
pn  libmudflap0-4.4-dev          <none>      (no description available)
pn  libmudflap0-dbg              <none>      (no description available)
pn  libppl-c2                    <none>      (no description available)
pn  libppl7                      <none>      (no description available)

-- no debconf information



Reply to: