
...
Note that it is a common mistake to choose a reserved name (such as _HEADER_H_
or __HEADER_H__
) for the name of the macro used in the include guard. See DCL37-C. Do not declare or define a reserved identifier for more information.
Until the early 1980s, large software development projects had a continual problem with the inclusion of headers. One group might have produced a graphics.h
, for example, which started by including io.h
. Another group might have produced keyboard.h
, which also included io.h
. If io.h
could not safely be included several times, arguments would break out about which header should include it. Sometimes an agreement was reached that each header should include no other headers, and as a result, some application programs started with dozens of #include
lines, and sometimes they got the ordering wrong or forgot a required header.
Exceptions
PRE06-C-EX0: It is acceptable to include a header file containing an X-Macro [ Myers 2010 ] multiple times, and each time needs no inclusion guard. Such header files should have an extension of .def.
Risk Assessment
Failure to include header files in an include guard can result in unexpected behavior.
Recommendation | Severity | Likelihood | Detectable | RepairableRemediation Cost | Priority | Level |
---|---|---|---|---|---|---|
PRE06-C | Low | Unlikely | Yes | YesLow | P3 | L3 |
Automated Detection
Tool | Version | Checker | Description | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Astrée |
| multiple include-guard-missing -guard-pragma-once | Fully Partially checked | |||||||||||
Axivion Bauhaus Suite |
| CertC-PRE06 | ||||||||||||
| CC2.PRE06 | Fully implemented | ||||||||||||
Helix QAC |
| C0883 | ||||||||||||
Klocwork |
| MISRA.INCGUARD | ||||||||||||
LDRA tool suite |
| 243 S | Fully implemented | |||||||||||
Parasoft C/C++test |
| CERT_C-PRE06-a | Use multiple include guards | |||||||||||
PC-lint Plus |
| 967 | Fully supported | |||||||||||
Polyspace Bug Finder |
| CERT C: Rec. PRE06-C | Checks for content of header file not guarded from multiple inclusions (rec. fully covered) | PRQA QA-C | ||||||||||
Include Page | PRQA QA-C_v | PRQA QA-C_v | ||||||||||||
0883 | Fully implemented | RuleChecker |
| multiple include-guard-missing -guard-pragma-once | Fully Partially checked |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
SEI CERT C++ Coding Standard | VOID PRE06-CPP. Enclose header files in an include guard |
MISRA C:2012 | Directive 4.10 (required) |
Bibliography
[Plum 1985] | Rule 1-14 |
Myers 2004 | |
Myers 2010 |
...