...
| Code Block | ||||
|---|---|---|---|---|
| ||||
void f(void) {
for (int i = 0; i < 10; i++) {
long j;
/* ... */
}
} |
Exceptions
DCL01-C-EX1: A function argument in a function declaration may clash with a variable in a containing scope provided that when the function is defined, the argument has a name that clashes with no variables in any containing scopes.
| Code Block | ||||
|---|---|---|---|---|
| ||||
extern int name;
void f(char *name); /* Declaration: no problem here */
/* ... */
void f(char *arg) { /* Definition: no problem; arg doesn't hide name */
/* Use arg */
}
|
DCL01-C-EX2: A temporary variable within a new scope inside of a macro can override a surrounding identifieroverride an identifier in a containing scope. However,this exception does not apply to to the arguments of the macro itself.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#define SWAP(type, a, b) do { type tmp = a; a = b; b = tmp; } while(0)
void func(void) {
int tmp = 100;
int a = 10, b = 20;
SWAP(int, a, b); /* Hidden redeclaration of tmp is acceptable */
SWAP(int, tmp, b); /* NONCOMPLIANT: Hidden redeclaration of tmp clashes with argument */
} |
Risk Assessment
Reusing a variable name in a subscope can lead to unintentionally referencing an incorrect variable.
Recommendation | Severity | Likelihood | Detectable |
|---|
Repairable | Priority | Level |
|---|---|---|
DCL01-C | Low | Unlikely |
Yes | Yes |
P3 | L3 |
Automated Detection
Tool | Version | Checker | Description |
|---|---|---|---|
| Astrée |
| Supported indirectly via MISRA C:2012 Rule 5.3. | ||||||||
| Axivion Bauhaus Suite |
| CertC-DCL01 | |||||||
| CodeSonar |
| LANG.ID.ND.NEST | Non-distinct identifiers: nested scope | ||||||
| Compass/ROSE |
| CC2.DCL01 | Fully implemented |
| Helix QAC |
|
|
|
IF_MULTI_DECL
IF_MULTI_DEF
IF_MULTI_KIND
C0795, C0796, C2547, C3334 | ||||||||
| Klocwork |
| MISRA.VAR.HIDDEN |
| LDRA tool suite |
| 131 S | Fully implemented |
2547
3334
| Parasoft C/C++test |
| CERT_C-DCL01-a | Identifier declared in a local or function prototype scope shall not hide an identifier declared in a global or namespace scope | ||||||
| PC-lint Plus |
| 578 | Fully supported | ||||||
| Polyspace Bug Finder |
| Checks for variable shadowing (rule fully covered) | |||||||
| PVS-Studio |
| V561, V688, V703, V711, V2015 | |||||||
| RuleChecker |
| Supported indirectly via MISRA C:2012 Rule 5.3. | |||||||
| Security Reviewer - Static Reviewer | 6.02 | C126 C127 | Fully Implemented |
| Splint |
|
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
| SEI CERT C++ Coding Standard | VOID DCL01-CPP. Do not reuse variable names in subscopes |
| MISRA C:2012 | Rule 5.3 (required) |
...
...