Versions Compared

Key

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

...

When a larger scope than necessary is used, code becomes less readable, harder to maintain, and more likely to reference unintended variables . (See see DCL01-C. Do not reuse variable names in subscopes).)

Noncompliant Code Example

...

In this compliant solution, the function f() is declared as externalwith internal linkage. This practice limits the scope of the function declaration to the current compilation unit and prevents the function from being included in the external symbol table. It also limits cluttering in the global name space and prevents the function from being accidentally or intentionally invoked from another compilation unit. See DCL15-C. Declare file-scope objects or functions that do not need external linkage as static for more information.

Code Block
bgColor#ccccff
langc
static int f(int i) {
  /* Function definition */
}

int g(int i) {
  int j = f(i);
  /* ... */
} 

...

Failure to minimize scope could result in less reliable, readable, and reusable code.

Recommendation

Severity

Likelihood

Detectable

Remediation Cost

Repairable

Priority

Level

DCL19-C

Low

Unlikely

Medium

Yes

Yes

P2

P3

L3

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

local-object-scope

global-object-scope

Partially checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-DCL19
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.STRUCT.SCOPE.FILE
LANG.STRUCT.SCOPE.LOCAL

Scope

Could Be File StaticScope Could Be Local Static

could be file static
Scope could be local static

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.DCL19

Fully implemented

PRQA QA-C
Helix QAC

Include Page

PRQA QA-C_vPRQA QA-C_v1504,1505,1531,1532,3210,3218 

Helix QAC_V
Helix QAC_V

C1504, C1505, C1531, C1532, C3210, C3218


Klocwork
Include Page
Klocwork_V
Klocwork_V

MISRA.VAR.MIN.VIS
CXX.ID_VIS.GLOBAL_VARIABLE.EXTERN
CXX.ID_VIS.GLOBAL_VARIABLE.STATIC
CERT.TU.UNUSED.GLOBAL.DECL
CERT.STATIC.SINGLE.USE


LDRA tool suite
 
Include Page
LDRA_V
LDRA_V
25 D, 61 D, 40 SFully implemented
Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-DCL19-a

Declare variables as locally as possible

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

765, 9003

Partially supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. DCL19-C


Checks for:

  • Function or object declared without static specifier and referenced in only one file
  • Object defined beyond necessary scope

Rec. partially covered.

PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V821
RuleChecker
Include Page
RuleChecker_V
RuleChecker_V

local-object-scope

global-object-scope

Partially checked

Related Vulnerabilities

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

Related Guidelines

...


...

Image Modified Image Modified Image Modified