Main Content

CERT C: Rule DCL36-C

Do not declare an identifier with conflicting linkage classifications

Description

Rule Definition

Do not declare an identifier with conflicting linkage classifications.1

Polyspace Implementation

The rule checker checks for Inconsistent use of static and extern in object declarations.

Examples

expand all

Issue

The issue occurs when you do not use the static storage class specifier consistently in all declarations of object and functions that have internal linkage.

The rule checker detects situations where:

  • The same object is declared multiple times with different storage specifiers.

  • The same function is declared and defined with different storage specifiers.

Risk

If you do not use the static specifier consistently in all declarations of objects with internal linkage, you might declare the same object with external and internal linkage.

In this situation, the linkage follows the earlier specification that is visible (C99 Standard, Section 6.2.2). For instance, if the earlier specification indicates internal linkage, the object has internal linkage even though the latter specification indicates external linkage. If you notice the latter specification alone, you might expect otherwise.

Example - Linkage Conflict Between Variable Declarations
static int foo = 0;
extern int foo;         /* Non-compliant */

extern int hhh;
static int hhh;         /* Non-compliant */

In this example, the first line defines foo with internal linkage. The first line is compliant because the example uses the static keyword. The second line does not use static in the declaration, so the declaration is noncompliant. By comparison, the third line declares hhh with an extern keyword creating external linkage. The fourth line declares hhh with internal linkage, but this declaration conflicts with the first declaration of hhh.

Correction — Consistent static and extern Use

One possible correction is to use static and extern consistently:

static int foo = 0;
static int foo;

extern int hhh;
extern int hhh;
Example - Linkage Conflict Between Function Declaration and Definition
static int fee(void);  /* Compliant - declaration: internal linkage */
int fee(void){         /* Non-compliant */
  return 1;
}

static int ggg(void);  /* Compliant - declaration: internal linkage */
extern int ggg(void){  /* Non-compliant */
  return 1;
}

This example shows two internal linkage violations. Because fee and ggg have internal linkage, you must use a static class specifier to be compliant with MISRA™.

Check Information

Group: Rule 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.