Versions Compared

Key

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

If a file-scope object or a function does not need to be visible outside of the file, it should be hidden by being declared as static. This practice creates more modular code and limits pollution of the global name space.

Section Subclause 6.2.2 of the C standard Standard [ISO/IEC 9899:2011] states:

If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

...

This noncompliant code example includes a helper() function that is implicitly declared to have external linkage.:

Code Block
bgColor#ffcccc
langc
enum { MAX = 100 };

int helper(int i) {
  /* performPerform some computation based on i */
}

int main(void) {
  size_t i;
  int out[MAX];

  for (i = 0; i < MAX; i++) {
    out[i] = helper(i);
  }

  /* ... */

}

...

This compliant solution declares helper() to have internal linkage, thereby preventing external functions from using it.:

Code Block
bgColor#ccccff
langc
enum {MAX = 100};

static int helper(int i) {
  /* performPerform some computation based on i */
}

int main(void) {
  size_t i;
  int out[MAX];

  for (i = 0; i < MAX; i++) {
    out[i] = helper(i);
  }

  /* ... */

}

...

Allowing too many objects to have external linkage can use up descriptive identifiers, leading to more complicated identifiers, violations of abstraction models, and possible name conflicts with libraries. If the compilation unit implements a data abstraction, it may also expose invocations of private functions from outside the abstraction.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

DCL15-C

low

Low

unlikely

Unlikely

low

Low

P3

L3

Automated Detection

Tool

Version

Checker

Description

Splint

Include PageSplint_VSplint_V

 

 
Astrée
Include Page
Astrée_V
Astrée_V
global-object-scopeFully checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-DCL15
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.STRUCT.SCOPE.FILEScope could be file static

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.DCL15

Fully implemented

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C1504, C1531
LDRA tool suite
Include Page
LDRA_V
LDRA_V

27 D
61 D
553 S

Fully implemented

Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-DCL15-a

Objects or functions with external linkage shall be declared in a header file

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

765

Fully supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. DCL15-C


Checks for situations where function or object with external linkage is referenced in only one translation unit (rec. fully covered)

RuleChecker
Include Page
RuleChecker_V
RuleChecker_V
global-object-scopeFully checked
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

...

...

Bibliography

...

Subclause 6.2.2, "Linkages of

...

Identifiers"

...


...

Image Modified Image Modified Image Modified