Writes must stay within an object’s bounds
A buffer overflow writes outside the bounds of the intended buffer and can damage neighboring state or allocator information. It can involve a stack array or heap storage. Exhausting stack capacity through deep recursion is a different mechanism, despite overlapping terminology. The function that eventually crashes may only consume corruption introduced earlier. Start from the allocated capacity, write length, index range and terminator requirements rather than enlarging whichever structure appears near the final symptom. The relevant boundary is the object’s permitted region, not merely the operating system’s page boundary. A write staying inside a mapped page can still violate the language’s object bounds.
Count terminators and use consistent units
For a hypothetical eight-byte character buffer requiring a trailing zero, copying eight data bytes and then a terminator requires nine bytes. Either permit at most seven data bytes or allocate the required capacity under a clear contract. This is arithmetic explaining a boundary, not an exploit or measured incident. Character count and byte count can differ under multibyte encodings. Multiplying element count by element size, or adding a header length, also requires overflow-aware arithmetic before allocation. Correct total length is insufficient if the starting offset is wrong or the code checks the original capacity instead of the remaining capacity. State each quantity’s unit explicitly.
Find the earliest invalid write
During investigation, connect the earliest invalid-write report with allocation size, access width and the allocation stack. AddressSanitizer is one way to identify violations, but findings depend on exercised inputs and the instrumented components. Preserve optimization and symbol settings, then minimize a reproducing input. Giving a later consumer a larger object can hide damage while leaving the incorrect length calculation intact. Distinguish an out-of-bounds read from an invalid write and from a lifetime error. Review both the reported access and the interface that established the object’s capacity. This identifies where a missing invariant belongs instead of distributing defensive guesses around every downstream crash.
Use interfaces with explicit size contracts
Prefer containers, spans or explicit capacity-carrying interfaces and validate required-size arithmetic. Caller and callee must agree on units, terminators and whether truncation is allowed. Functions with size-limited names still differ in return values and termination behavior, so their contracts must be checked individually. Silently truncating an identifier or structured record can change its meaning; rejection and truncation are product decisions rather than interchangeable safety patches. Check arithmetic before allocating and handle allocation failure. A memory-safe implementation can remove many such defects, but foreign-function interfaces and unsafe blocks still require correct length and ownership relationships at their boundaries.
Separate mitigation from correctness
Retest empty input, below-capacity input, exact boundaries, one-beyond-boundary lengths, multibyte text and very large size values. Success only just below the limit can miss a one-byte terminator error. Stack canaries and address randomization are defense layers, not transformations of an invalid write into a correct one. The repair should explain how required capacity is established and why an excessive operation is rejected or represented safely. Check the error behavior alongside unchanged handling of legitimate data. A disappearing crash is useful but weaker than a size contract supported by boundary-focused tests. Keep mitigation, detection and prevention as distinct claims when reporting the result.