Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Do not use the same variable name in two scopes where one scope is contained in another. For example

  • No other variable should share the name of a global variable if the other variable is in a subscope of the global variable.
  • A block should not declare a variable with the same name as a variable declared in any block that contains it.

Reusing variable names leads to programmer confusion about which variable is being modified. Additionally, if variable names are reused, generally one or both of the variable names are too generic.

Noncompliant Code Example

This noncompliant code example declares the msg identifier at file scope and reuses the same identifier to declare a character array local to the report_error() function. The programmer may unintentionally copy the function argument to the locally declared msg array within the report_error() function. Depending on the programmer's intention, this either fails to initialize the global variable msg, or this allows the local msg buffer to overflow by using the global value msgsize as a bounds for the local buffer.

Code Block
bgColor#FFCCCC
langc
static char msg[100];
static const size_t msgsize = sizeof( msg);

void report_error(const char *str) {
  char msg[80];
  snprintf(msg, msgsize, "Error: %s\n", str);
  /* ... */
}

int main() {
  /* ... */
  report_error("some error");
}

Compliant Solution

This compliant solution uses different, more descriptive variable names.

Code Block
bgColor#ccccff
langc
static char message[100];
static const size_t message_size = sizeof( message);

void report_error(const char *str) {
  char msg[80];
  snprintf(msg, sizeof( msg), "Error: %s\n", str);
  /* ... */
}

int main() {
  /* ... */
  report_error("some error");
}

When the block is small, the danger of reusing variable names is mitigated by the visibility of the immediate declaration. Even in this case, however, variable name reuse is not desirable. In general, the larger the declarative region of an identifier the more descriptive and verbose should be the name of the identifier.

By using different variable names globally and locally, the compiler forces the developer to be more precise and descriptive with variable names.

Exceptions

DCL01-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
bgColor#ccccff
langc
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
}

Risk Assessment

Reusing a variable name in a subscope can lead to unintentionally referencing an incorrect variable.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

DCL01-C

low

unlikely

medium

P2

L3

Automated Detection

Tool

Version

Checker

Description

Section

LDRA tool suite

Include Page
c:LDRA_V
c:LDRA_V
Section

131 S

Section

Fully Implemented

Section

Splint

Include Page
c:Splint_V
c:Splint_V

 

 

Section

Compass/ROSE

 

 

 

Section

Klocwork

Include Page
c:Klocwork_V
c:Klocwork_V
Section

IF_MULTI_DECL
IF_MULTI_DEF
IF_MULTI_KIND

 

Section

ECLAIR

Include Page
c:ECLAIR_V
c:ECLAIR_V
Section

declhidn

Section

Fully Implemented

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

CERT C++ Secure Coding Standard: DCL01-CPP. Do not reuse variable names in subscopes

ISO/IEC 9899:1999 Section 5.2.4.1, "Translation limits"

MISRA Rule 5.2

Bibliography


      02. Declarations and Initialization (DCL)