CertC-EXP11¶
Do not make assumptions regarding the layout of structures with bit-fields
Required inputs: IR
The internal representations of bit-field structures have several properties (such as internal padding) that are implementation-defined . Additionally, bit-field structures have several implementation-defined constraints:
- The alignment of bit-fields in the storage unit (for example, the bit-fields may be allocated from the high end or the low end of the storage unit)
- Whether or not bit-fields can overlap a storage unit boundary
Consequently, it is impossible to write portable safe code that makes assumptions regarding the layout of bit-field structure members.
Noncompliant Code Example (Bit-Field Alignment)
Bit-fields can be used to allow flags or other integer values with small ranges to be packed together to save storage space. Bit-fields can improve the storage efficiency of structures. Compilers typically allocate consecutive bit-field structure members into the same int -sized storage, as long as they fit completely into that storage unit. However, the order of allocation within a storage unit is implementation-defined. Some implementations are right-to-left : the first member occupies the low-order position of the storage unit. Others are left-to-right : the first member occupies the high-order position of the storage unit. Calculations that depend on the order of bits within a storage unit may produce different results on different implementations.
Consider the following structure made up of four 8-bit bit-field members:
struct bf {
unsigned int m1 : 8;
unsigned int m2 : 8;
unsigned int m3 : 8;
unsigned int m4 : 8;
}; /* 32 bits total */
Right-to-left implementations will allocate struct bf as one storage unit with this format:
m4 m3 m2 m1
Conversely, left-to-right implementations will allocate struct bf as one storage unit with this format:
m1 m2 m3 m4
The following code behaves differently depending on whether the implementation is left-to-right or right-to-left:
struct bf {
unsigned int m1 : 8;
unsigned int m2 : 8;
unsigned int m3 : 8;
unsigned int m4 : 8;
}; /* 32 bits total */
void function() {
struct bf data;
unsigned char *ptr;
data.m1 = 0;
data.m2 = 0;
data.m3 = 0;
data.m4 = 0;
ptr = (unsigned char *)&data;
(*ptr)++; /* Can increment data.m1 or data.m4 */
}
|
Compliant Solution (Bit-Field Alignment)
This compliant solution is explicit in which fields it modifies:
struct bf {
unsigned int m1 : 8;
unsigned int m2 : 8;
unsigned int m3 : 8;
unsigned int m4 : 8;
}; /* 32 bits total */
void function() {
struct bf data;
data.m1 = 0;
data.m2 = 0;
data.m3 = 0;
data.m4 = 0;
data.m1++;
}
|
Noncompliant Code Example (Bit-Field Overlap)
In this noncompliant code example, assuming 8 bits to a byte, if bit-fields of 6 and 4 bits are declared, is each bit-field contained within a byte, or are the bit-fields split across multiple bytes?
struct bf {
unsigned int m1 : 6;
unsigned int m2 : 4;
};
void function() {
unsigned char *ptr;
struct bf data;
data.m1 = 0;
data.m2 = 0;
ptr = (unsigned char *)&data;
ptr++;
*ptr += 1; /* What does this increment? */
}
|
If each bit-field lives within its own byte, then m2 (or m1 , depending on alignment) is incremented by 1. If the bit-fields are indeed packed across 8-bit bytes, then m2 might be incremented by 4.
Compliant Solution (Bit-Field Overlap)
This compliant solution is explicit in which fields it modifies:
struct bf {
unsigned int m1 : 6;
unsigned int m2 : 4;
};
void function() {
struct bf data;
data.m1 = 0;
data.m2 = 0;
data.m2 += 1;
}
|
Risk Assessment
Making invalid assumptions about the type of type-cast data, especially bit-fields, can result in unexpected data values.
| Recommendation | Severity | Likelihood | Detectable | Repairable | Priority | Level |
|---|---|---|---|---|---|---|
| EXP11-C | Medium | Probable | No | No | P4 | L3 |
Related Guidelines
| SEI CERT C++ Coding Standard | VOID EXP11-CPP. Do not apply operators expecting one type to data of an incompatible type |
| ISO/IEC TR 24772:2013 | Bit Representations [STR] |
| MISRA C:2012 | Directive 1.1 (required) |
Bibliography
| [ Plum 1985 ] | Rule 6-5: In portable code, do not depend upon the allocation order of bit-fields within a word |
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
cast_changes_type_inside_category |
Cast exposes the implementation-defined layout of the bit-fields in ‘{}’ |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
check_explicit_casts¶
check_explicit_casts : bool = True
check_implicit_casts¶
check_implicit_casts : bool = True
look_through_casts¶
look_through_casts : bool = False
only_complex_expressions¶
only_complex_expressions : bool = False
show_operand_in_entity¶
show_operand_in_entity : bool = False
type_category¶
type_category
Selection of type categories to consider.Type: set[TypeCategory]
Default:
{'object_pointer_types'}
type_system¶
type_system : bauhaus.ir.common.types.type_systems.TypeSystem = <bauhaus.ir.common.types.type_systems.CompilerTypeSystem object at 0x7f3bf21364e0>
Option Types¶
These types are used by options listed above:
TypeCategory¶
Base class for the different type categories.signed_types
unsigned_types
float_types
char_types
plain char.bool_types
bool, _Bool and special expressions.enum_types
void_types
void_pointer_types
incomplete_pointer_types
function_pointer_types
object_pointer_types
null_pointer_types
other_types
Those not covered by other categories.