Use visually distinct identifiers with meaningful names to eliminate errors resulting from misreading 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 appear visually similar or even identical:
Character | Similar Characters |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 and to help prevent errors resulting from nonunique identifiers. (See DCL23-C. Guarantee that mutually visible identifiers are unique.)
In addition, the larger the scope of an identifier, the more descriptive its name should be. It may be perfectly appropriate to name a loop control variable i
, but the same name would likely be confusing if it named a file scope object or a variable local to a function more than a few lines long. See also DCL01-C. Do not reuse variable names in subscopes and DCL19-C. Use as minimal a scope as possible for all variables and functions.
DCL02-C implicitly assumes global scope, which can be confused with scope within the same file. Although it may not generate any errors, a possible violation of the rule may occur, as in the following example. Note this example does not violate DCL23-C. Guarantee that mutually visible identifiers are unique.
In file foo.h
:
int id_O; /* (Capital letter O) */ |
In file bar.h
:
int id_0; /* (Numeric digit zero) */ |
If a file foobar.c
includes both foo.h
and bar.h
, then both id_0
and id_0
come in the same scope, violating this rule.
In a compliant solution, use of visually similar identifiers should be avoided in the same project scope.
In file foo.h
:
int id_a; |
In file bar.h
:
int id_b; |
Failing to use visually distinct identifiers can result in referencing the wrong object or function, causing unintended program behavior.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL02-C | Low | Unlikely | Medium | P2 | L3 |
Tool | Version | Checker | Description |
---|---|---|---|
Axivion Bauhaus Suite | CertC-DCL02 | ||
CodeSonar | LANG.ID.AMBIG | Typographically ambiguous identifiers | |
Compass/ROSE | |||
CC2.DCL02 | Fully implemented | ||
LDRA tool suite | 67 X | Fully implemented | |
Parasoft C/C++test | CERT_C-DCL02-a | Use visually distinct identifiers | |
PC-lint Plus | 9046 | Partially supported: does not report ‘Q’ or ‘D’ vs ‘0’ or ‘O’ | |
Polyspace Bug Finder | CERT C: Rec. DCL02-C | Checks for use of typographically ambiguous identifiers (rec. fully covered) |
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
SEI CERT C++ Coding Standard | VOID DCL02-CPP. Use visually distinct identifiers |
ISO/IEC TR 24772:2013 | Choice of Clear Names [NAI] |
MISRA C:2012 | Directive 4.5 (advisory) |