...
For instance, it is acceptable to call std::memcpy() on an object containing a bit-field, as in the following example, because the read and write of the padding bits cannot be observed. However, the code still must comply with OOP57-CPP. Prefer special member functions and overloaded operators to C Standard Library functions.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <cstring>
struct S {
int i : 10;
int j;
};
void f(const S &s1) {
S s2;
std::memcpy(&s2, &s1, sizeof(S));
} |
Code that complies with this exception must still comply with OOP57-CPP. Prefer special member functions and overloaded operators to C Standard Library functions.
Risk Assessment
The effects of accessing bits of an object representation that are not part of the object's value representation can range from implementation-defined behavior (such as assuming the layout of fields with differing access controls) to code execution vulnerabilities (such as overwriting the vtable pointer).
...