In C and C++, enumeration types map to integers. The normal expectation is that each enumeration type member is distinct.
Non-Compliant Code Example
However, if required, members can be assigned explicit values, as in:
enum {red=4, orange, yellow, green, blue, indigo=6, violet};
It may not be obvious to the programmer (though it is fully specified in the language) that yellow and indigo have been declared to be identical values (6), as are green and violet (7).
Compliant Solution
Enumeration type declarations must either
- provide no explicit integer assignments, for example:
enum {red, orange, yellow, green, blue, indigo, violet};
- assign a value to the first member only (the rest are then sequential), for example:
enum {red=4, orange, yellow, green, blue, indigo, violet};
- assign a value to all members, so any equivalence is explicit, for example:
enum {red=1, orange=2, yellow=3, green=4, blue=5, indigo=6, violet=7};
Risk Assessment
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
|---|---|---|---|---|---|
INT09-A |
1 (low) |
1 (unlikely) |
3 (low) |
P3 |
L3 |
References
- ISO/IEC 9899-1999 Section 6.7.2.2, "Enumeration specifiers"
- MISRA 04 Rule 9.3