ang子溢出 [英] clang enum overflow

查看:251
本文介绍了ang子溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用-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返回

  demo.c:9:3:警告:枚举值中的溢出
out_1
^

如您所见,编译器不会对out_2溢出提示警告,编译时他的值是未知的?

解决方案

在第一个实例中,编译器本身正在尝试选择一个导致溢出的整数,同样也是警告你。它可能生成 INT_MIN 。该标准允许 signed int 中的任何值作为枚举常数(见底部)。



在第二,在分配给 out_2 之前,计算表达式(INT_MAX + 1)。这里的表达式中的溢出正在产生允许的结果,但这是未定义的行为。有效的结果然后存储在枚举中,这就是为什么第一个错误不会生成。



clang(3.2)也不会警告这个,这是有效的相同:

  int a = INT_MAX + 1; 

在这方面,clang不符合C标准,因为这是未定义的。 p>

相对于gcc的输出,差异完全清楚:

 函数'main':
9:9:错误:枚举值中的溢出
13:25:警告:表达式中的整数溢出[-Woverflow]
pre>

Intel编译器忽略枚举溢出,但警告整数溢出:

  enum.c(13):warning#61:整数运算结果超出范围
out_2 = INT_MAX + 1
^
pre>



为了参考,从C99标准6.7.7.2.2中,定义枚举常数值的表达式应为一个整型常量表达式,其值可表示为 int ; .3,枚举器列表中的标识符被声明为具有类型<> c $ c> int的常量,并且可能会出现在允许的任何地方。即枚举常量可以是任何 int 值,并且具有 int 类型。定义的枚举变量的结果类型可以是 char int unsigned int ,只要它允许枚举中的所有可能的常量。因此,示例中的枚举未定义,因为它们都需要整数溢出。第一个明确是非法的。


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?

解决方案

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.

这篇关于ang子溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆