Main Content

CERT C: Rec. DCL15-C

Declare file-scope objects or functions that do not need external linkage as static

Description

This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).

Rule Definition

Declare file-scope objects or functions that do not need external linkage as static.1

Polyspace Implementation

The rule checker checks for Function or object with external linkage referenced in only one translation unit.

Examples

expand all

Issue

The rule checker flags:

  • Objects that are defined at file scope without the static specifier but used only in one file.

  • Functions that are defined without the static specifier but called only in one file.

If you intend to use the object or function in one file only, declare it static.

Objects that are defined at file scope without the static specifier but used only in one file.

Functions that are defined without the static specifier but called only in one file.

Risk

Compliance with this rule avoids confusion between your identifier and an identical identifier in another translation unit or library. If you restrict or reduce the visibility of an object by giving it internal linkage or no linkage, you or someone else is less likely to access the object inadvertently.

Example - Variable with External Linkage Used in One File

Header file:

/* file.h */
extern int var;

First source file:

/* file1.c */
#include "file.h"

int var;    /* Compliant */
int var2;   /* Non compliant */
static int var3; /* Compliant */

void reset(void);

void reset(void) {
    var = 0;
    var2 = 0;
    var3 = 0;
}

Second source file:

/* file2.c */
#include "file.h"

void increment(int var2);

void increment(int var2) {
    var++;
    var2++;
}

In this example:

  • The declaration of var is compliant because var is declared with external linkage and used in multiple files.

  • The declaration of var2 is noncompliant because var2 is declared with external linkage but used in one file only.

    It might appear that var2 is defined in both files. However, in the second file, var2 is a parameter with no linkage and is not the same as the var2 in the first file.

  • The declaration of var3 is compliant because var3 is declared with internal linkage (with the static specifier) and used in one file only.

Example - Function with External Linkage Used in One File

Header file:

/* file.h */
extern int var;
extern void increment1 (void);

First source file:

/* file1.c */
#include "file.h"

int var;

void increment2(void);
static void increment3(void);
void func(void);

void increment2(void) { /* Non compliant */
    var+=2;
}

static void increment3(void) { /* Compliant */
    var+=3;
}

void func(void) {
    increment1();
    increment2();
    increment3();
}

Second source file:

/* file2.c */
#include "file.h"

void increment1(void) { /* Compliant */
    var++;
}

In this example:

  • The definition of increment1 is compliant because increment1 is defined with external linkage and called in a different file.

  • The declaration of increment2 is noncompliant because increment2 is defined with external linkage but called in the same file and nowhere else.

  • The declaration of increment3 is compliant because increment3 is defined with internal linkage (with the static specifier) and called in the same file and nowhere else.

Check Information

Group: Rec. 02. Declarations and Initialization (DCL)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.