Versions Compared

Key

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

...

DCL01-C-EX2: A temporary variable within a new scope inside of a macro can override a surrounding identifier.override an identifier in a containing scope. However,this exception does not apply to to the arguments of the macro itself.

Code Block
bgColor#ccccff
langc
#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

Remediation Cost

Priority

Level

DCL01-C

Low

Unlikely

Medium

P2

L3

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

Supported indirectly via MISRA C:2012 Rule 5.3.
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-DCL01
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.ID.ND.NESTNon-distinct identifiers: nested scope
Compass/ROSE

 

 

 




ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.DCL01

Fully implemented

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C0795, C0796, C2547, C3334


Klocwork
Include Page
Klocwork_V
Klocwork_V

MISRA.VAR.HIDDEN

 

 


LDRA tool suite
Include Page
LDRA_V
LDRA_V

131 S

Fully implemented

Parasoft C/C++test
9.5MISRA2004-5_2_{a,b}Fully implementedPolyspace Bug FinderR2016aVariable shadowing

Variable hides another variable of same name with nested scope

PRQA QA-C Include Page
Include Page
Parasoft_V
Parasoft_V

CERT_C-DCL01-a
CERT_C-DCL01-b

Identifier declared in a local or function prototype scope shall not hide an identifier declared in a global or namespace scope
Identifiers declared in an inner local scope should not hide identifiers declared in an outer local scope

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

578

Fully supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. DCL01-C


Checks for variable shadowing (rule fully covered)

PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V561, V688, V703, V711, V2015
RuleChecker

Include Page
RuleChecker_V
RuleChecker_V


Supported indirectly via MISRA C:2012 Rule 5.3.
PRQA QA-C_vPRQA QA-C_v

2547
3334

Fully implemented
Splint
Include Page
Splint_V
Splint_V

 

 



Related Vulnerabilities

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

Related Guidelines

...


...

Image Modified Image Modified Image Modified