stack smashing detected C/C++ Origin: Application & API Last updated 15 Oct 2026

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.

Exact message you may see *** stack smashing detected ***stack smashing detected: terminated*** stack smashing detected ***: terminatedbuffer overflow detected*** buffer overflow detected ***
Seen on: C/C++ Linux GCC

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, gets or scanf("%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

  1. Use bounded functions: snprintf, strncpy (carefully), fgets, or std::string
  2. Compile with -fsanitize=address -g to see the exact overflowing write
  3. Check every length argument

Detailed fix by platform

GCC / Clang

  1. g++ -g -fsanitize=address main.cpp -o app — AddressSanitizer names the line with "stack-buffer-overflow"
  2. Use -Wall -Wformat-overflow -Wstringop-overflow for compile-time hints

Code examples

Bounded copy

cpp
#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

  1. ASan — Which line does stack-buffer-overflow name?
  2. Buffers — Fixed-size local arrays written to?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

AddressSanitizer: heap-buffer-overflow — C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N (C/C++)AddressSanitizer: hea…AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…Bus error — C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file (C/C++)Bus errorC4996 — MSVC: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead (C/C++)C4996double free or corruption — C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped) (C/C++)double free or corrup…Floating point exception — C/C++: Floating point exception (core dumped) — SIGFPE, usually integer division by zero (C/C++)Floating point except…format expects argument of type — C: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=] (C/C++)format expects argume…Segmentation fault — C/C++: Segmentation fault (core dumped) — SIGSEGV from a null, dangling or out-of-bounds pointer (C/C++)Segmentation faultstack sma…

Explore the map of this error Full error map →

🧠 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.