Buffer Safety and Process Memory
Process memory layout
| Region | Common uses |
|---|---|
| Text | Program machine instructions. |
| Data and BSS | Objects with static storage duration. Nonzero initializers usually go in the data segment, and zero-initialized objects can go in BSS. |
| Heap | Memory allocated and freed while the program runs. |
| Stack | Temporary call state, including some local variables. Architecture and compiler settings affect the actual layout. |
Lengths and indices
A buffer has a starting address, capacity, and data format. An array of length n has valid indices from 0 through n−1. When allocating memory or checking a copy length, address negative values and signed-to-unsigned conversions before comparing against the available capacity.
Out-of-bounds writes
A write beyond the buffer can corrupt adjacent data and cause crashes. Verify the allocated destination capacity and the source length. Strings also need space for the terminator. Test empty, exact-fit, and one-unit-too-large inputs.