Wrong allocated object size for cast
Allocated memory does not match destination pointer
Description
This defect occurs during pointer conversion when the pointer’s address is misaligned. If a pointer is converted to a different pointer type, the size of the allocated memory must be a multiple of the size of the destination pointer.
Risk
Dereferencing a misaligned pointer has undefined behavior and can cause your program to crash.
Fix
Suppose you convert a pointer ptr1
to ptr2
.
If ptr1
points to a buffer of N
bytes and
ptr2
is a
pointer where
type
*sizeof(
is
type
)n
bytes, make sure that N
is an integer
multiple of n
.
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
Examples
Result Information
Group: Static Memory |
Language: C | C++ |
Default: Off |
Command-Line Syntax: OBJECT_SIZE_MISMATCH |
Impact: High |
Version History
Introduced in R2013b
See Also
Find defects (-checkers)
| Unreliable cast of pointer
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)