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.
All these complications disappeared with the discovery of a simple technique: each header should #define
a symbol that means "I have already been included." The entire header is then enclosed in an inclusion include guard:
Code Block |
---|
|
#ifndef HEADER_H
#define HEADER_H
/* ... contentsContents of <header.h> ... */
#endif /* HEADER_H */
|
...
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 inclusion include guard. See DCL37-C. Do not declare or define a reserved identifier for more information.
Failure to include header files in an inclusion include guard can result in unexpected behavior.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|
PRE06-C |
lowunlikelylowECLAIRECLAIRECLAIRinclgard implementedPRQA QA-C | Include Page |
---|
PRQA_V | PRQA_V | 0883 | supported |
Polyspace Bug Finder | Include Page |
---|
| Polyspace Bug Finder_V |
---|
| Polyspace Bug Finder_V |
---|
|
| CERT C: Rec. PRE06-C | Checks for content of header file not guarded from multiple inclusions (rec. fully covered)
|
RuleChecker | Include Page |
---|
| RuleChecker_V |
---|
| RuleChecker_V |
---|
|
| include-guard-missing include-guard-pragma-once | Fully checked |
Fully implemented Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Secure inclusion -Rule 195 : Macros shall not be #define
’d or #undef
’d within a block ...
...
Image Modified Image Modified Image Modified