MISRAC2004-12.12_a
In this section:
Synopsis
(Required) The underlying bit representations of floating-point values shall not be used. To reinterpret bit patterns deliberately, use an explicit cast.
Enabled by default
Yes
Severity/Certainty
Medium/High

Full description
Found a read access to a field of a union following a write access to a different field, which effectively re-interprets the bit pattern with a different type. This check is identical to UNION-type-punning.
Coding standards
- CERT EXP39-C
Do not access a variable through a pointer of an incompatible type
- CWE 188
Reliance on Data/Memory Layout
- MISRA C:2004 12.12
(Required) The underlying bit representations of floating-point values shall not be used.
Code examples
The following code example fails the check and will give a warning:
union name {
int int_field;
float float_field;
};
void example(void) {
union name u;
u.int_field = 10;
float f = u.float_field;
}
The following code example passes the check and will not give a warning about this issue:
union name {
int int_field;
float float_field;
};
void example(void) {
union name u;
u.int_field = 10;
float f = u.int_field;
}