Main Content

CERT C: Rec. PRE08-C

Guarantee that header file names are unique

Since R2024a

Description

Guarantee that header file names are unique.1

Polyspace Implementation

The rule checker checks for Nonunique header file names.

Examples

expand all

Issue

Nonunique header file names occur when your source file includes two or more distinct header files whose names satisfy the following conditions, ignoring distinctions of alphabetical case:

  • First eight characters in the file names are identical.

  • Either all these file names have no extensions, or all of them have the same single character-extension following a period in the name.

Notes about the checker's behavior:

  • The checker reports violations per translation unit. Therefore, the checker does not detect conflicting header file names between different translation units.

  • In a translation unit, for a set of #include statements that include conflicting file names, the checker reports a violation at the first #include statement. All subsequent #include statements in this set appear as a sequence of events attached to this violation.

  • The checker does not report a violation if you include the same file more than once.

  • If an #include statement refers to the absolute or relative path of a header file, the checker only uses the base file name when checking for violations of this recommendation.

  • The checker does not report violations on GCC #include_next statements.

Risk

If all your header file names are not unique, you might inadvertently include an older version of a header file in your source code. This older version might include incorrect macro definitions or obsolete function prototypes, or cause other run-time errors that the compiler might fail to detect. In addition, nonunique header file names might also cause portability issues.

Fix

If necessary, rename your header files so that they can be distinguished based on either the first eight characters in their names or their single-character extensions. Remember that your compiler might ignore distinctions of alphabetical case.

Example — Nonunique File Names
#include "Library.h"            /* Non-compliant */
#include <stdio.h>
#include <stdlib.h>
#include "library.h"
 
#include "utilities_math.h"     /* Non-compliant */
#include "utilities_physics.h"
 
#include "my_library.h"         

void foo()
{
}

In this example, the checker reports two violations of this recommendation because:

  • The file names Library.h and library.h both have seven characters which are identical, ignoring distinctions of alphabetical case. They also have the same file extension.

  • The first eight characters of the file names utilities_math.h and utilities_physics.h are identical. They also have the same file extension.

Correction — Rename Files to Make Them Unique

To fix these violations, make sure that the header files have unique names. For example, you can rename the files as follows:

#include "Lib_main.h"
#include <stdio.h>
#include <stdlib.h>
#include "lib_2.h"
 
#include "util_math.h"
#include "util_physics.h"
 
#include "my_library.h"

void foo()
{
}

Result Information

Group: Rec. 08. Preprocessor (PRE)

Version History

Introduced in R2024a


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.