Use visually distinct identifiers to eliminate errors resulting from misrecognizing the spelling of an identifier during the development and review of code. An identifier can denote an object; a function; a tag or a member of a structure, union, or enumeration; a typedef name; a label name; a macro name; or a macro parameter.
Depending on the fonts used, certain characters are visually similar or even identical:
0 (zero) |
O (capital o) |
D (capital d) |
1 (one) |
I (capital i) |
l (lowercase L) |
2 (two) |
Z (capital z) |
|
5 (five) |
S (capital s) |
|
8 (eight) |
B (capital b) |
|
n (lowercase N) |
h (lowercase H) |
|
rn (lowercase R, lowercase N) |
m (lowercase M) |
Do not define multiple identifiers that vary only with respect to one or more visually similar characters.
Make the initial portions of long identifiers unique for easier recognition. This also helps prevent errors resulting from nonunique identifiers (see DCL32-C. Guarantee that mutually visible identifiers are unique).
Failing to use visually distinct identifiers can result in the wrong variable being used, causing unexpected program behavior.
Recommendation |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
|---|---|---|---|---|---|
DCL02-C |
low |
unlikely |
medium |
P2 |
L3 |
Compass/ROSE can detect violations of this recommendation.
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
This rule appears in the C++ Secure Coding Standard as DCL02-CPP. Use visually distinct identifiers.
This rule appears in the Java Secure Coding Standard as DCL00-J. Use visually distinct identifiers.
\[[ISO/IEC 9899:1999|AA. References#ISO/IEC 9899-1999]\] Section 5.2.4.1, "Translation limits" \[[ISO/IEC PDTR 24772|AA. References#ISO/IEC PDTR 24772]\] "AJN Choice of Filenames and other External Identifiers," "BRS Leveraging human experience," and "NAI Choice of Clear Names" \[[MISRA 04|AA. References#MISRA 04]\] Rule 5.6 |
02. Declarations and Initialization (DCL)