Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c - clang enum 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
", 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 de?nes the value of an enumeration constant shall be an integer constant expression that has a value representable as an int; .3, "The identi?ers 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...