C/C++: *** stack smashing detected ***: terminated — buffer overflow on the stack
GCC's stack protector noticed that a local buffer overflowed and overwrote the function's canary value, so it aborted the program.
*** stack smashing detected ***stack smashing detected: terminated*** stack smashing detected ***: terminatedbuffer overflow detected*** buffer overflow detected *** Meaning
When a function returns, the guard value placed after its local arrays is checked. A write past the end of a char buf[16] (from strcpy, gets, scanf("%s"), sprintf or an off-by-one loop) changes it, producing this abort. The crash is reported at function exit, not at the faulty write.
Common causes
strcpy,sprintf,getsorscanf("%s")into a fixed-size array- Loop with
<=instead of<writing one element past the end - Wrong buffer length passed to
read/memcpy - Format specifier mismatch writing more bytes than expected
⚡ Quick fix
- Use bounded functions:
snprintf,strncpy(carefully),fgets, orstd::string - Compile with
-fsanitize=address -gto see the exact overflowing write - Check every length argument
Detailed fix by platform
GCC / Clang
g++ -g -fsanitize=address main.cpp -o app— AddressSanitizer names the line with "stack-buffer-overflow"- Use
-Wall -Wformat-overflow -Wstringop-overflowfor compile-time hints
Code examples
Bounded copy
#include <cstdio>
#include <cstring>
int main() {
char name[8];
const char* input = "Alexander";
// std::strcpy(name, input); // 10 bytes into 8: stack smashing
std::snprintf(name, sizeof name, "%s", input); // truncates safely to "Alexand"
std::puts(name);
}How to diagnose
- ASan — Which line does stack-buffer-overflow name?
- Buffers — Fixed-size local arrays written to?
- Loops — Any <= bound?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026