
...
For an enumeration whose underlying type is fixed, the values of the enumeration are the values of the underlying type. Otherwise, the values of the enumeration are the values representable by a hypothetical integer type with minimal width M such that all enumerators can be represented. The width of the smallest bit-field large enough to hold all the values of the enumeration type is M. It is possible to define an enumeration that has values not defined by any of its enumerators. If the enumerator-list is empty, the values of the enumeration are as if the enumeration had a single enumerator with value 0.
...
A value of integral or enumeration type can be explicitly converted to a complete enumeration type. If the enumeration type has a fixed underlying type, the value is first converted to that type by integral conversion, if necessary, and then to the enumeration type. If the enumeration type does not have a fixed underlying type, the value is unchanged if the original value is within the range of the enumeration values (9.7.1), and otherwise, the behavior is undefined. A value of floating-point type can also be explicitly converted to an enumeration type. The resulting value is the same as converting the original value to the underlying type of the enumeration (7.3.10), and subsequently to the enumeration type.
...
It is possible for unspecified values to result in a buffer overflow, leading to the execution of arbitrary code by an attacker. However, because enumerators are rarely used for indexing into arrays or other forms of pointer arithmetic, it is more likely that this scenario will result in data integrity violations rather than arbitrary code execution.
Rule | Severity | Likelihood | Detectable | RepairableRemediation Cost | Priority | Level |
---|---|---|---|---|---|---|
INT50-CPP | Medium | Unlikely | Yes | NoMedium | P4 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| cast-integer-to-enum | Partially checked | ||||||
Axivion Bauhaus Suite |
| CertC++-INT50 | |||||||
CodeSonar |
| LANG.CAST.COERCE LANG.CAST.VALUE | Coercion Alters Value Cast Alters Value | ||||||
Helix QAC |
| C++3013 | |||||||
Parasoft C/C++test |
| CERT_CPP-INT50-a | An expression with enum underlying type shall only have values corresponding to the enumerators of the enumeration | ||||||
PVS-Studio |
| V1016 | |||||||
RuleChecker |
| cast-integer-to-enum | Partially checked | ||||||
Polyspace Bug Finder |
| CERT C++: INT50-CPP | Checks for casting to out-of-range enumeration value (rule fully covered) |
...