Main Content

MISRA C:2012 Rule 6.3

A bit field shall not be declared as a member of a union

Since R2024a

Description

Rule Definition

A bit field shall not be declared as a member of a union.

This rule comes from MISRA C™: 2012 Amendment 3.

Rationale

The C standard does not specify the bitwise position of a bit field within a type. For example, in this code, the bit field oneByte can be the first eight bits or the last eight bits of the int32_t storage unit.

int32_t oneByte:8;
The exact bitwise position of the bit field depends on the processor endianness, the compiler, and other implementation-dependent factors. If you use bit fields within a union for type punning, the storage of the bit field is unspecified and implementation-dependent. In this code, it is unspecified how the bitfield oneByte is overlaid on the 32-bit number:
union myUnion {
	int32_t number;
	int oneByte: 8;
};

void foo() {
	union myUnion U1;
	U1.number = 0xDEADBEEF;
}
The value of U1.oneByte can be either 0xDE or 0xEF, depending on the implementation.

Using bit fields within unions makes the behavior of your code implementation-dependent. Avoid using bit fields within unions.

Polyspace Implementation

Polyspace® reports a violation of this rule if a bit field is declared as a member of a union. Declaring a bit field as a subobject within union members is not a violation of this rule. For example, if you have a bitfield within a struct and declare a union containing the struct, this rule is not violated.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, a bit field is declared in the union myUnion1. Because the bitwise position of the bit field is unspecified in the C standard, the return value of the function foo() is implementation-dependent. Polyspace reports a violation of this rule.

#include<stdint.h>
union myUnion1 {
	int32_t number;
	int oneByte: 8 //Noncompliant
};
union myUnion2 {
	int32_t number;
	int8_t oneByte; //Compliant
};
struct date {
	int day : 5;
	int m : 4;
	int y;
};

union myUnion3 { //Compliant
	int64_t numDate;
	struct date sDate;
};

int foo() {
	union myUnion1 U1;
	U1.number = 0xDEADBEEF;
	if(U1.oneByte == 0xDE) {
		return 1;
	} else {
		return -1;
	}

}

Instead of a bit field, consider using an 8 bit integer, as shown in myUnion2. The bitwise position of oneByte in myUnion2 is specified. Using this union for type-punning purposes does not make the code implementation-defined. This union is compliant with this rule. Keeping bit fields as a subobject of a union is compliant with this rule, as shown in myUnion3

Check Information

Group: Types
Category: Required
AGC Category: Required

Version History

Introduced in R2024a