Hard-coded buffer size
Size of memory buffer is a numerical value instead of symbolic constant
Description
This defect occurs when you use a numerical value instead of a symbolic constant when declaring a memory buffer such as an array.
Risk
Hard-coded buffer size causes the following issues:
Hard-coded buffer size increases the likelihood of mistakes and therefore maintenance costs. If a policy change requires developers to change the buffer size, they must change every occurrence of the buffer size in the code.
Hard-constant constants can be exposed to attack if the code is disclosed.
Fix
Use a symbolic name instead of a hard-coded constant for buffer
size. Symbolic names include const
-qualified variables, enum
constants,
or macros.
enum
constants are recommended.
Macros are replaced by their constant values after preprocessing. Therefore, they can expose the loop boundary.
enum
constants are known at compilation time. Therefore, compilers can optimize the loops more efficiently.const
-qualified variables are usually known at run time.
Examples
Result Information
Group: Good practice |
Language: C | C++ |
Default: Off |
Command-Line Syntax: HARD_CODED_BUFFER_SIZE |
Impact: Low |
Version History
Introduced in R2015b
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)