Hard-coded object size used to manipulate memory
Memory manipulation with hard-coded size instead of sizeof
Description
This defect occurs when you use hard-coded constants as memory size arguments for these memory functions:
Dynamic memory allocation function such as
malloc
orcalloc
.Memory manipulation functions such as
memcpy
,memmove
,memcmp
, ormemset
.
When performing memory operations with a string literal, Polyspace® does not report a defect if you hard code the memory size.
Risk
If you hard code object size, your code is not portable to architectures with different type sizes. If the constant value is not the same as the object size, the buffer might or might not overflow.
Fix
For the size argument of memory functions, use sizeof(
.object
)
Examples
Result Information
Group: Good Practice |
Language: C | C++ |
Default: Off |
Command-Line Syntax: HARD_CODED_MEM_SIZE |
Impact: Low |
Version History
Introduced in R2016bSee 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)