CWE-469 — Use of Pointer Subtraction to Determine Size
CWE-469: Use of Pointer Subtraction to Determine Size
MITRE CWE weakness
| Kind | Weakness |
| Abstraction | Base |
| Status | Draft |
| Likelihood of exploit | Medium |
Description
The product subtracts one pointer from another in order to determine size, but this calculation can be incorrect if the pointers do not exist in the same memory chunk.
Common consequences
- Access Control, Integrity, Confidentiality, Availability: Modify Memory, Read Memory, Execute Unauthorized Code or Commands, Gain Privileges or Assume Identity
Mitigations
Implementation — Save an index variable. This is the recommended solution. Rather than subtract pointers from one another, use an index variable of the same size as the pointers in question. Use this variable to "walk" from one pointer to the other and calculate the difference. Always validate this number.
References
- CWE page: https://cwe.mitre.org/data/definitions/469.html
- CWE list: https://cwe.mitre.org/data/index.html