A buffer overflow happens when a program writes more data into a block of memory than it can hold, or writes past the end of it. The result can be corrupted data, a crash, or attacker-chosen code running. The most reliable fix is a memory-safe language, with bounds checks and compiler defenses as backup.
What exactly goes wrong in memory?
OWASP describes the condition as a program trying to put more data in a buffer than it can hold, or putting data in memory past a buffer. MITRE's CWE catalog names the broader weakness out-of-bounds write (CWE-787): the product writes data past the end, or before the beginning, of the intended buffer.
In the classic stack case, the attacker sends data that is stored in an undersized stack buffer. The overflow overwrites information on the call stack, including the function's return pointer. When the function returns, control goes where the overwritten pointer says. CWE-121 covers this stack-based variant, and heap overflows and off-by-one errors are siblings.
What is the impact?
CWE lists three consequences. Integrity: memory corruption, and in some cases modification of control data such as return addresses to execute unexpected code. Availability: an out-of-range access can crash the product. Other: later writes produce undefined behavior.
OWASP rates the severity as very high, with likelihood of exploit from high to very high. It also says overflows are not easy to discover and generally hard to exploit, yet attackers have found them in a staggering range of products. That combination, hard but valuable, explains why they keep appearing.
Where do they hide?
OWASP says the root cause is usually memory manipulation combined with mistaken assumptions about the size or makeup of data. C and C++ functions often do not check bounds, and even bounded functions such as strncpy can cause trouble when used incorrectly. CWE adds a specific trap: if the destination size equals the source size, strncpy may not terminate the string.
Risk concentrates in code that relies on external data to control its behavior, depends on data properties enforced elsewhere, or is too complex to predict. For web applications, OWASP points at web and application servers and at libraries the app calls, such as a graphics library handling uploaded images. Environments built on interpreted languages like Java or Python are largely immune, except for flaws in the interpreter itself, and CWE warns that a language's interface to native code can still overflow.
A constructed example
A function copies a user-supplied display name into a 16-byte local array with an unbounded string copy. A 40-byte name overwrites 24 bytes of adjacent stack memory. The fix is to bound the write to the destination size and handle truncation deliberately:
void set_name(const char *user_input) {
char name[16];
snprintf(name, sizeof(name), "%s", user_input);
/* name is always terminated and never exceeds 16 bytes */
}
Better still is to avoid fixed-size buffers for untrusted data by using a safe string type or a language with bounds checking.
Which mitigations does CWE recommend?
Among the ones CWE lists:
- Language selection: use a language that does not allow the weakness, such as ones that manage their own memory.
- Vetted libraries with safer string handling. CWE notes this is not a complete solution because many overflows are not about strings.
- Compiler and runtime detection, such as stack canaries and range checking. CWE rates these as defense in depth and notes the typical result of a detected overflow is still a crash.
- Address space layout randomization and position-independent executables, which make addresses unpredictable. CWE says exploits often pair an overflow with a memory disclosure bug, and that side channels have bypassed ASLR.
- Data execution protection, using hardware no-execute bits, with the caveat that overwriting nearby variables can still be harmful.
- Replacing unbounded copy functions with length-taking ones, rated moderate because calculation errors remain.
How to check or apply this
- Inventory native code: your own C and C++, plus libraries and image, font or parsing components that your application loads.
- Review code for unbounded copies, size arithmetic and loops that index into buffers. OWASP has a code review guide and a testing guide article on this class.
- Turn on compiler hardening and confirm ASLR, PIE and no-execute are active in your build and on your hosts.
- Fuzz parsers that take untrusted input and run the fuzzer in CI.
- Prefer a memory-safe language for new components that handle untrusted data.
- Track vulnerabilities in third-party native libraries. Safeguard's dependency scanning and reachability analysis show whether an application actually calls a vulnerable function in a library, which helps decide what to patch first.
Quick check
Are Java and Python programs vulnerable to buffer overflows?
Mostly not, per OWASP and CWE, since the runtime manages memory. The exceptions are flaws in the interpreter itself and in native code that the language calls.
Does ASLR fix buffer overflows?
No. CWE treats it as defense in depth. It makes exploitation less reliable, but the overflow remains, and memory disclosure bugs or side channels can undermine it.
Sources
- OWASP, Buffer Overflow
- MITRE CWE-787, Out-of-bounds Write
- MITRE CWE-121, Stack-based Buffer Overflow
Last reviewed: 18 October 2026.