This vulnerability occurs when a programmer incorrectly accounts for pointer arithmetic in C or C++, causing the program to access unintended memory locations. The core issue is forgetting that adding an integer to a pointer automatically scales that integer by the size of the data type it points to.
In C and C++, pointer arithmetic is not byte-based by default. When you add an integer `n` to a pointer, the compiler implicitly multiplies `n` by the `sizeof` the pointed-to type to get the correct byte offset. A common mistake is writing code that assumes a simple, unscaled addition, which leads to calculating an address far beyond or short of the intended target. This often happens when treating a pointer as a generic byte pointer (`void*` or `char*`) in some places but as a typed pointer (like `int*`) in others without proper casting. To prevent this, always be explicit about pointer types and scaling. When performing manual memory navigation, consistently use `char*` for byte-wise arithmetic or carefully cast pointers, ensuring you understand the compiler's implicit multiplications. Using container classes, iterators, or modern C++ smart pointers can often eliminate the need for raw pointer arithmetic altogether, sidestepping this entire class of errors.
Impact: Read MemoryModify Memory
Incorrect pointer scaling will often result in buffer overflow conditions. Confidentiality can be compromised if the weakness is in the context of a buffer over-read or under-read.
cMedium