Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang enum overflow

Tags:

c

enums

overflow

Using -Wall -pedantic

#include <limits.h>
#include <stdio.h>

int main(void)
{
    enum x {
        a,
        max = INT_MAX,
        out_1
    };
    enum y {
        b,
        out_2 = INT_MAX + 1
    };


    printf("%d %d\n", out_1, out_2);
    return 0;
}

clang returns

demo.c:9:3: warning: overflow in enumeration value
                out_1
                ^

As you can see, compiler does not warn about out_2 overflow, his value is unknown at compile time?

like image 323
David Ranieri Avatar asked Mar 08 '13 21:03

David Ranieri


1 Answers

In the first instance, the compiler itself is trying to pick an integer that is causing an overflow, and so is warning you. It is likely producing INT_MIN. The standard allows for any value in a signed int to be an enum constant (see bottom).

In the second, the expression (INT_MAX + 1) is calculated before it is assigned to out_2. An overflow in the expression here is producing a result that is allowed, but this is undefined behaviour. The valid result is then stored in the enum, which is why the first error isn't produced.

clang (3.2) will also not warn about this, which is effectively identical:

int a = INT_MAX + 1;

In this respect, clang is not behaving according to the C standard, as this is undefined.

The output from gcc in comparison makes the difference completely clear:

In function ‘main’:
9:9: error: overflow in enumeration values
13:25: warning: integer overflow in expression [-Woverflow]

The Intel compiler ignores the enum overflow, but warns about the integer overflow:

enum.c(13): warning #61: integer operation result is out of range
      out_2 = INT_MAX + 1
                      ^


For reference, from the C99 standard 6.7.7.2.2, "The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int; .3, "The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted." i.e. an enum constant may be any int value, and has an int type. The resulting type of a defined enum variable can be char, int or unsigned int, as long as it allows for all the possible constants in the enum. As such both enums in the example are undefined, as they both require an integer overflow. The first is explicitly illegal.
like image 103
teppic Avatar answered Nov 10 '22 05:11

teppic