If one definition affects another, embody a relation exists between constants, you should encode the relationship in the definition; do definitions. Do not give two independent definitions, because a maintainer may fail to preserve that relationship when modifying the code.
...
As a corollary, do not encode an impermanent or false relationship between constants, because future modifications may result in an incorrect definition for the dependent constant.
Noncompliant Code Example
In this non-compliant codingnoncompliant code example, the definition for XX2 OUT_STR_LEN must always be two greater than the definition of XX IN_STR_LEN. The following definitions fail to embody this relationship:
| Code Block | |||
|---|---|---|---|
| |||
#define XX 5
#define XX2 7 /* misleading, no indication of relationship */
| |||
| |||
enum { IN_STR_LEN=18, OUT_STR_LEN=20 };
|
A Consequently, a programmer performing maintenance on this program would need to identify the relationship and modify both definitions accordingly. While Although this sort of error appears relatively benign, it could can easily lead to serious security vulnerabilities, such as buffer overflows.
Compliant Solution
This pair of definitions The declaration in this compliant solution embodies the relationship between the two definitions. :
| Code Block | ||||
|---|---|---|---|---|
| ||||
enum { IN_STR_LEN=18, OUT_STR_LEN=IN_STR_LEN+2 }; #define XX 5 #define XX2 (XX + 2) |
As a result, a programmer could can reliably modify the program by changing the definition of XX.
Risk Assessment
IN_STR_LEN.
Noncompliant Code Example
In this noncompliant code example, a relationship is established between two constants where none exists:
| Code Block | ||||
|---|---|---|---|---|
| ||||
enum { ADULT_AGE=18 };
/* Misleading; relationship established when none exists */
enum { ALCOHOL_AGE=ADULT_AGE+3 };
|
A programmer performing maintenance on this program may modify the definition for ADULT_AGE but fail to recognize that the definition for ALCOHOL_AGE has also been changed as a consequence.
Compliant Solution
This compliant solution does not assume a relationship where none exists:
| Code Block | ||||
|---|---|---|---|---|
| ||||
enum { ADULT_AGE=18 };
enum { ALCOHOL_AGE=21 };
|
Risk Assessment
Failing to properly encode relationships in constant definitions may lead to the introduction of defects during maintenance. These defects could potentially result in vulnerabilities, for example, if the affected constants were used for allocating or accessing memory.
Recommendation |
|---|
Severity | Likelihood |
|---|
Detectable | Repairable | Priority | Level |
|---|
DCL08- |
1 (low)
1 (unlikely)
2 (medium)
P2
L3
C | Low | Unlikely | No | No | P1 | L3 |
Automated Detection
Tool | Version | Checker | Description |
|---|
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Related Guidelines
Bibliography
| [Plum 1985] | Rule 1-4 |
...
\[[Plum 85|AA. C References#Plum 85]\] Rule 1-4
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.10, "Preprocessing directives," and Section 5.1.1, "Translation environment"Wiki Markup