Standard-layout types can be used to communicate with code written in other programming languages, as the layout of the type is strictly specified. The C++ Standard, [class], paragraph 7 [ISO/IEC 14882-2014], defines a standard-layout classes class as a class that
- Does does not have virtual functions,
- Has has the same access control for all nonstatic data members,
- Has has no base classes of the same type as the first nonstatic data member,
- Has has nonstatic data members declared in only one class within the class hierarchy, and
- Recursivelyrecursively, does not have nonstatic data members of nonstandard-layout type.
...
This noncompliant code example assumes that there is a library whose header is library.h
, and an application (represented by application.cpp
), and that the library and application are not ABI-compatible. Therefore, the contents of library.h
constitute an execution boundary. A nonstandard-layout type object S
is passed across this execution boundary. The application creates an instance of an object of this type, then passes a reference to the object to a function defined by the library, crossing the execution boundary. Because the layout is not guaranteed to be compatible across the boundary, this results in unexpected behavior.
...
The effects of passing objects of nonstandard-layout type across execution boundaries depends on what operations are performed on the object within the callee as well as what subsequent operations are performed on the object from the caller. The effects can range from correct or benign behavior to undefined behavior.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP60-CPP | High | Probable | Medium | P12 | L1 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
| -Wdynamic-class-memaccess | Catches instances where the vtable pointer will be overwritten | |||||||
Helix QAC |
| DF4741, DF4742, DF4743 | |||||||
Klocwork |
| CERT.EXPR.PASS_NON_STD_LAYOUT | |||||||
Parasoft C/C++test |
| CERT_CPP-EXP60-a | Do not pass a nonstandard-layout type object across execution boundaries | ||||||
Polyspace Bug Finder |
| CERT C++: EXP60-CPP | Checks for non-standard layout objects passed across execution boundaries (rule fully covered). |
Related Vulnerabilities
Search for other vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
Bibliography
...