Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider $version (sch jbop) (X_X)@==(Q_Q)@

...

If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

...

Noncompliant Code Example

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

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

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

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

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

  /* ... */

}

Compliant Solution

This compliant solution declares helper() to have internal linkage, thereby preventing objects from other scopes from using it.

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

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

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

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

  /* ... */

}

Risk Assessment

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.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

DCL15-A C

low

unlikely

low

P3

L3

Automated Detection

Splint Version 3.1.1 can detect violations of this rule.

Related Vulnerabilities

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

References

Wiki Markup
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.2.2, "Linkages of identifiers"

...