Bytewise operations on nontrivial class object
Value representations may be improperly initialized or compared
Description
This defect occurs when you use C Standard library functions to perform bytewise operation on non-trivial or non-standard layout class type objects. For definitions of trivial and standard layout classes, see the C++ Standard (ISO/IEC 14882:2017), [class], paragraphs 6 and 7 respectively.
The checker raises a defect when:
You initialize or copy non-trivial class type objects using these functions:
std::memset
std::memcpy
std::strcpy
std::memmove
To check whether a class type is trivial, use the type-traits library function
std::is_trivial
, for instance:It is not sufficient to check that the object type is trivially copyable (#include <iostream> #include <type_traits> class trivialClass {}; void checkTrivial(){ static_assert(std::is_trivial<trivialClass>::value, "Class is not trivial"); }
std::is_trivially_copyable<>:value
). A trivially copyable object does not guarantee the class invariants hold when you use the object later in your program.You compare non-standard layout class type objects using these functions:
std::memcmp
std::strcmp
Note that an incomplete class can be potentially nontrivial.
The checker does not raise a defect if the bytewise operation is performed through an
alias. For example no defect is raised in the bytewise comparison and copy operations in this
code. The bytewise operations use dptr
and sptr
, the
aliases of non-trivial or non-standard layout class objects d
and
s
.
void func(NonTrivialNonStdLayout *d, const NonTrivialNonStdLayout *s) { void* dptr = (void*)d; const void* sptr = (void*)s; // ... // ... // ... if (!std::memcmp(dptr, sptr, sizeof(NonTrivialNonStdLayout))) { (void)std::memcpy(dptr, sptr, sizeof(NonTrivialNonStdLayout)); // ... } }
Risk
Performing bytewise comparison operations by using C Standard library functions on non-trivial or non-standard layout class type object might result in unexpected values due to implementation details. The object representation depends on the implementation details, such as the order of private and public members, or the use of virtual function pointer tables to represent the object.
Performing bytewise setting operations by using C Standard library functions on non-trivial or non-standard layout class type object can change the implementation details. The operation might result in abnormal program behavior or a code execution vulnerability. For instance, if the address of a member function is overwritten, the call to this function invokes an unexpected function.
Fix
To perform bytewise operations non-trivial or non-standard layout class type object, use these C++ special member functions instead of C Standard library functions.
C Standard Library Functions | C++ Member Functions |
---|---|
| Class constructor |
| Class copy constructor Class move constructor Copy assignment operator Move assignment operator |
|
|
Examples
Result Information
Group: Object Oriented |
Language: C++ |
Default: Off |
Command-Line Syntax:
MEMOP_ON_NONTRIVIAL_OBJ |
Impact: Medium |
Version History
Introduced in R2019b
See Also
Find defects
(-checkers)
| Memory comparison of padding data
| Memory comparison of
strings
| Copy of overlapping memory
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)