Every program needs somewhere to put things while it works. A typed name, a line read from a file, a message arriving over a network: each has to sit in memory for a moment before the program can use it. The space set aside for that data is called a buffer, and it has a fixed size.
A buffer overflow happens when a program writes more data into a buffer than it can hold, and the extra data spills into the memory next door. Nothing about the data needs to be strange or hostile. An ordinary input that is simply longer than expected is enough, and the neighbouring memory gets overwritten.
What a buffer is
A buffer is a reserved stretch of memory with a known size. When a programmer sets one up, they decide how much it should hold: room for fifty characters, say, or a thousand bytes of data. The program is supposed to treat the buffer as a container with walls, and stop writing when it reaches them.
Buffers are everywhere because programs constantly move data around. Reading a file means copying its contents into a buffer a piece at a time. Receiving a message means placing it in a buffer until the program is ready to read it. They are the ordinary shelves a program puts things on, and the size is fixed because memory has to be planned.
How the spill happens
Imagine a shelf built to hold ten books. If someone tries to put twelve books on it, the last two do not vanish. They land somewhere else. A buffer works the same way. A program reads twelve units into a buffer built for ten, and the last two are written into the memory that follows it.
Why would a program do that? Usually because nobody checked the length first. The code that copies the data assumes the input will fit, or trusts the source to keep it short. A file may be longer than the programmer expected, or a user may paste a longer name than the form allowed. The copy goes ahead anyway.
The spill is silent. There is no alarm when data crosses the end of a buffer, because it is just writing to memory, one unit after another. It does not know that space belongs to something else. The first sign of trouble comes later, when the program uses the overwritten memory and finds something unexpected there.

Why it matters: crashes
The mildest outcome is a crash. Memory next to a buffer holds information the program needs to keep running: a number it is counting with, or the address of the next instruction it should follow. When overflowed data overwrites one of those values, the program can no longer make sense of itself.
Sometimes the program stops at once. Sometimes it carries on with corrupted values, producing wrong results before it fails. The cause can be hard to trace, because the crash happens somewhere other than where the extra data was written. The overflow is the cause, but the symptom shows up later, when the damaged memory is used.
Why it matters: security
A crash is not the worst outcome. An overflow can also be a way in. If an attacker can control the data that overflows a buffer, they can choose what gets written into the neighbouring memory. That can mean replacing the address of the next instruction, so the program runs code the attacker supplied.
This is not a theoretical worry. Some of the most discussed security flaws in software history have been buffer overflows, in programs that handled messages, images, or documents from untrusted sources. A single unchecked copy in a program that reads data from outside was enough to hand control of a machine to whoever sent the data.
Keep this separate from other attacks. A SQL injection is a different kind of problem: hostile code hidden inside a database query, rather than data spilling past the end of a block of memory. Both involve untrusted input, but they attack different parts of a system, and they are fixed in different ways.

What it is not
A buffer overflow is easy to confuse with other memory problems. A memory leak is a slow drain: a program claims memory and forgets to return it, so free memory shrinks a little at a time. An overflow is not a drain. It is a single write that goes too far, and the damage is done at once.
It is also not a shortage of memory. A program can run out of memory, and that is a different failure. An overflow can happen on a machine with memory to spare, because the problem is not how much memory exists. The kernel manages the shared pool of memory, but it cannot know how large a buffer was meant to be.
How teams prevent one
The direct defence is a bounds check: before copying data into a buffer, the program measures the data and refuses to write more than the buffer can hold. If the input is too long, the program takes only the part that fits or rejects it. It works, but it has to be applied everywhere data enters a buffer.
Safer functions help with that. Many languages provide copying functions that take the size of the destination buffer and stop at its edge, instead of older functions that copy until the source runs out. Teams that use the safer versions remove a class of mistakes, because the check is built into the tool rather than left to every programmer to remember.
Modern systems add further layers of protection behind the code. Operating systems can mark regions of memory as non-executable, so that even if an attacker places their own code in a buffer, the machine will refuse to run it. Other protections shuffle the layout of memory each time a program starts, so an attacker cannot know where anything sits.
None of these layers is a substitute for the first one. A bounds check stops the overflow from happening at all; the other protections limit the damage when a check is missing. Teams treat them as a stack: write the checks, use the safer functions, and let the system protections catch whatever slips through. They also test with long inputs on purpose.

A small mistake with a long reach
A buffer overflow begins with something unremarkable: a container built to one size, and data that turns out to be larger. The program simply writes past the end of the space it was given, and the extra data lands on whatever sits next door.
That simplicity is why the problem has lasted so long. Every program uses buffers, and every buffer has a fixed size, so the chance to write past the end is present. The fix is simple — check the length, every time — and the teams that take it seriously are the ones whose programs keep running when the long input arrives.
