Invalid object size
Detects invalid pointer casts due to differences in the sizes of types.
Overview
Use this check to detect pointer casts when the size of the source type is less than the size of the destination type. Using the result of such a cast to access out-of-bounds data has undefined behavior. Available in Xcode 9 and later.
Downcast from type with insufficient space in C++
In the following example, the cast from Base * to Derived * is suspect because Base isn’t large enough to contain an instance of Derived:
struct Base {
int pad1;
};
struct Derived : Base {
int pad2;
};
Derived *getDerived() {
return static_cast<Derived *>(new Base); // Error: invalid downcast
}The optimizer may remove an expression, such as getDerived()->pad2, because getDerived() returns a pointer to an object that isn’t large enough to contain a pad2 field.
Solution
One way to fix this issue is to avoid the downcast, such as by using instances of the Derived object wherever you need them.
See Also
Undefined Behavior Sanitizer
Misaligned pointerInvalid Boolean valueOut-of-bounds array accessInvalid enumeration valueReaching of unreachable pointDynamic type violationInvalid float castDivision by zeroNonnull argument violationNonnull return value violationNonnull variable assignment violationNull reference creation and null pointer dereferenceInvalid shiftInteger overflowInvalid variable-length array