分配的数字太大而无法在位字段中表示未定义的行为

Is assigning a number too big to be represented in a bit field undefined behaviour

我有以下代码:

#include <stdio.h>
#include <stdint.h>

enum nums {
    ONE,
    TWO,
    TWENTY = 20
};

struct field {

    uint32_t something : 4;
    uint32_t rest : 28;
};

int main(void) {

    struct field f;
    f.something = TWENTY;
    return 0;
}

在 powerpc 8241 运行 RTEMS 4.9.1 上,使用 minGW GCC 3.4.5(我知道它很旧)编译,此代码将导致段错误。我确定的原因是我们将一个数字设置为大,由一个位域表示到相关位域。因为我们有 4 位,所以它应该只能表示 0 -> 15,实际上当我们用这些数字设置它时它工作正常。上面的任何东西都会崩溃。我无法重现此行为 here,所以我的问题是:

这是未定义的行为吗?如果是这样,c 标准中是否有涵盖它的参考?

或者它更有可能只是一个错误,因为我们的非常旧的编译器?

这看起来像一个错误,至少在 C99 中这是定义明确的行为,来自草案 c99 标准部分 6.2.6 类型的表示:

Values stored in unsigned bit-fields and objects of type unsigned char shall be represented using a pure binary notation.40

及后面的第 6.2.5 节类型:

A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.