Versions Compared

Key

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

An identifier declared in different scopes or multiple times within the same scope can be made to refer to the same object or function by linkage. An identifier can be classified as externally linked, internally linked, or not linked. These three kinds of linkage have the following characteristics [Kirch-Prinz 2002]:

  • External linkage.: An identifier with external linkage represents the same object or function throughout the entire program, that is, in all compilation units and libraries belonging to the program. The identifier is available to the linker. When a second declaration of the same identifier with external linkage occurs, the linker associates the identifier with the same object or function.
  • Internal linkage.: An identifier with internal linkage represents the same object or function within a given translation unit. The linker has no information about identifiers with internal linkage. Consequently, these identifiers are internal to the translation unit.
  • No linkage.: If an identifier has no linkage, then any further declaration using the identifier declares something new, such as a new variable or a new type.

...

In this noncompliant code example, i2 and i5 are defined as having both internal and external linkage. Future use of either identifier results in undefined behavior.

Code Block
bgColor#FFCCCC
langc
int i1 = 10;         /* Definition, external linkage */
static int i2 = 20;  /* Definition, internal linkage */
extern int i3 = 30;  /* Definition, external linkage */
int i4;              /* Tentative definition, external linkage */
static int i5;       /* Tentative definition, internal linkage */

int i1;  /* Valid tentative definition */
int i2;  /* Not valid, linkage disagreement with previous */
int i3;  /* Valid tentative definition */
int i4;  /* Valid tentative definition */
int i5;  /* Not valid, linkage disagreement with previous */

int main(void) {
  /* ... */
  return 0;
}

...

Use of an identifier classified as both internally and externally linked causes undefined behavior.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

DCL36-C

Medium

Probable

Medium

P8

L2

...

Related Vulnerabilities

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

...

 

...

Image Modified