Incorrect use of offsetof in C++
Incorrect arguments to offsetof
macro causes undefined
behavior
Description
This defect occurs when you pass
arguments to the offsetof
macro for which the behavior of the macro is not
defined.
The offsetof
macro:
offsetof(classType, aMember)
aMember
from the beginning of an object of type
classType
. For use in offsetof
,
classType
and aMember
have certain restrictions:
classType
must be a standard layout class.For instance, it must not have
virtual
member functions. For more information on the requirements for a standard layout class, see C++ named requirements: StandardLayoutType.aMember
must not be static.aMember
must not be a member function.
The checker flags uses of the offsetof
macro where the
arguments violate one or more of these restrictions.
Risk
Violating the restrictions on the arguments of the offsetof
macro
leads to undefined behavior.
Fix
Use the offsetof
macro only on nonstatic data members of a standard
layout class.
The result details state which restriction on the offsetof
macro is
violated. Fix the violation.
Examples
Result Information
Group: Programming |
Language: C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax:
OFFSETOF_MISUSE |
Impact: Medium |
Version History
Introduced in R2019a
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)