Incorrect pointer scaling
Implicit scaling in pointer arithmetic might be ignored
Description
This defect occurs when Polyspace® Bug Finder™ considers that you are ignoring the implicit scaling in pointer arithmetic.
For instance, the defect can occur in the following situations.
Situation | Risk | Possible Fix |
---|---|---|
You use the sizeof operator in arithmetic
operations on a pointer. | The Pointer arithmetic
is already implicitly scaled by the size of the data type of the pointed
variable. Therefore, the use of | Do not use sizeof operator in pointer arithmetic. |
You perform arithmetic operations on a pointer, and then apply a cast. | Pointer arithmetic is implicitly scaled. If you do not consider this implicit scaling, casting the result of a pointer arithmetic produces unintended results. | Apply the cast before the pointer arithmetic. |
Fix
The fix depends on the root cause of the defect. See fixes in the table above and code examples with 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: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax: BAD_PTR_SCALING |
Impact: Medium |
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)