---
title: Invalid object size
framework: xcode
role: article
role_heading: Article
path: xcode/invalid-object-size
---

# Invalid object size

Detects invalid pointer casts due to differences in the sizes of types.

## Overview

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. note: This UBSan check may not trigger at low optimization levels. 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 pointer](xcode/misaligned-pointer.md)
- [Invalid Boolean value](xcode/invalid-boolean.md)
- [Out-of-bounds array access](xcode/out-of-bounds-array-access.md)
- [Invalid enumeration value](xcode/invalid-enumeration-value.md)
- [Reaching of unreachable point](xcode/reaching-of-unreachable-point.md)
- [Dynamic type violation](xcode/dynamic-type-violation.md)
- [Invalid float cast](xcode/invalid-float-cast.md)
- [Division by zero](xcode/division-by-zero.md)
- [Nonnull argument violation](xcode/nonnull-argument-violation.md)
- [Nonnull return value violation](xcode/nonnull-return-value-violation.md)
- [Nonnull variable assignment violation](xcode/nonnull-variable-assignment-violation.md)
- [Null reference creation and null pointer dereference](xcode/null-reference-creation-and-null-pointer-dereference.md)
- [Invalid shift](xcode/invalid-shift.md)
- [Integer overflow](xcode/integer-overflow.md)
- [Invalid variable-length array](xcode/invalid-variable-length-array.md)
