You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 69 Next »

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.

Compliant Solution

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 guard:

#ifndef HEADER_H
#define HEADER_H

/* ... contents of <header.h> ... */

#endif /* HEADER_H */

Consequently, the first time header.h is #include'd, all of its contents are included. If the header file is subsequently #include'd again, its contents are bypassed.

Because solutions such as this one make it possible to create a header file that can be included more than once, the C Standard guarantees that the standard headers are safe for multiple inclusion.

Note that it is a common mistake to choose a reserved name for the name of the macro used in the inclusion guard. See DCL37-C. Do not declare or define a reserved identifier.

Risk Assessment

Failure to include header files in an inclusion guard can result in unexpected behavior.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

PRE06-C

low

unlikely

low

P3

L3

Automated Detection

ToolVersionCheckerDescription

ECLAIR

1.2

inclgard

Fully implemented

LDRA tool suite

9.7.1

243 S

Fully implemented
PRQA QA-C
Unable to render {include} The included page could not be found.
0883Fully implemented

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Bibliography

[Plum 1985]Rule 1-14

 

  • No labels