Alignment changed after memory reallocation
Memory reallocation changes the originally stricter alignment of an object
Description
This defect occurs when you
use realloc()
to modify the size of objects with strict memory alignment
requirements.
Risk
The pointer returned by realloc()
can be suitably assigned to objects
with less strict alignment requirements. A misaligned memory allocation can lead to buffer
underflow or overflow, an illegally dereferenced pointer, or access to arbitrary memory
locations. In processors that support misaligned memory, the allocation impacts the
performance of the system.
Fix
To reallocate memory:
Resize the memory block.
In Windows®, use
_aligned_realloc()
with the alignment argument used in_aligned_malloc()
to allocate the original memory block.In UNIX/Linux, use the same function with the same alignment argument used to allocate the original memory block.
Copy the original content to the new memory block.
Free the original memory block.
Note
This fix has implementation-defined behavior. The implementation might not support the requested memory alignment and can have additional constraints for the size of the new memory.
Examples
Result Information
Group: Dynamic memory |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax:
ALIGNMENT_CHANGE
|
Impact: Low |
Version History
Introduced in R2017b
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)