Memory safety keeps operations within valid memory bounds and prevents user-controlled data from overwriting unrelated code or data. Because code and data share memory, an unchecked input can overflow its buffer and potentially redirect execution. section 10.6
Heartbleed-style overread
A heartbeat request declares a long payload but supplies little data. Vulnerable code trusts that length and calls memcpy(bp, pl, payload), reading beyond the heartbeat buffer into other memory and returning its contents. section 10.6
Apply memory-safety defenses operationally:
- Avoid C and C++ functions without length checks, including strcpy(), sprintf(), gets(), and scanf(). section 10.6
- Use strncpy(), snprintf(), fgets(), and sscanf(), and verify explicit buffer bounds when using memcpy(). section 10.6
- Combine careful parsing with extensive testing and fuzzing, while avoiding integer overflows, type-safety problems, and undefined behavior. section 10.9
Figure 10.9 The Heartbleed vulnerability: a malicious client requests a very long heartbeat but provides very little data to echo. The vulnerable server reads beyond the heartbeat buffer into other areas of memory.
Use the Heartbleed diagram to trace how a declared payload length makes a server read beyond its buffer. section 10.6


