Fix “stack smashing detected” Abort in C
GCC and Clang insert a guard canary word between local stack buffers and the saved return address when `-fstack-protector` is active. If out-of-bounds writes corrupt that canary, `__stack_chk_fail()` aborts the process with SIGABRT on function return.
Quick Fix Summary
Bound all string writes with snprintf(dst, sizeof dst, ...) or bounded widths (scanf("%15s", buf) / fgets(buf, sizeof buf, stdin)), and test with -fsanitize=address.
Why This Error Happens
How stack canaries stop silent return-address corruption
When a local buffer overflows on the stack, adjacent locals and the saved frame pointer or return address are overwritten before the CPU crashes cleanly.
Pinpointing the exact line before return
Because __stack_chk_fail fires only when the function epilogue executes, compile with -fsanitize=address in our ASan + UBSan profile to trap on the exact overflowing store instruction.
Top Root Causes & Specific Code Fixes
Copying an oversized string with strcpy or unfiltered scanf
Unbounded copies write past the N-byte stack array onto the canary.
char name[8];
strcpy(name, "onlinecompiler");char name[8];
snprintf(name, sizeof(name), "%s", "online");Off-by-one loop condition (<= size instead of < size)
Writing arr[N] touches N bytes beyond the end of int arr[N].
int arr[4];
for (int i = 0; i <= 4; i++) arr[i] = i;int arr[4];
for (int i = 0; i < 4; i++) arr[i] = i;Complete Broken and Corrected Programs
Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.
Read the broken program
#include <stdio.h>
#include <string.h>
int main(void) {
char tag[4];
strcpy(tag, "OVERFLOW");
printf("%s\n", tag);
return 0;
}Open broken program in compilerRead the corrected program
#include <stdio.h>
int main(void) {
char tag[16];
snprintf(tag, sizeof(tag), "%s", "SAFE_TAG");
printf("%s\n", tag);
return 0;
}Open corrected program in compilerExpected output from the corrected program
Reference output, not a live execution result.
SAFE_TAGCompiler & Sanitizer Diagnostic Matrix
| Compiler / Runtime Symptom | Root Cause | Diagnostic Flag / Check | Corrected Pattern |
|---|---|---|---|
| *** stack smashing detected *** (SIGABRT) | Buffer write overwrote saved stack canary | -fstack-protector-strong + ASan | Bound copies with `snprintf(dst, sizeof dst, ...)` |
| off-by-one `<= N` loop write | Loop wrote `arr[N]` past end of stack frame | -fsanitize=address | Iterate `0 <= i < N` strictly |
Debugging Techniques and Commands
gcc -std=c17 -g -O1 -fsanitize=address main.c -o app
Halts immediately at the first out-of-bounds byte write and prints stack redzones.
gcc -std=c17 -fstack-protector-strong -Wall -Wextra main.c -o app
Protects functions containing stack arrays and address-taken locals.
Frequently Asked Questions
Why does the crash happen at return instead of the strcpy line?
The stack canary is verified right before leaving the stack frame; use AddressSanitizer to trap on the write instruction itself.
Should I disable -fno-stack-protector to hide the error?
Never. Turning off stack protection leaves stack memory corruption silent and exploitable.
Why use snprintf instead of strncpy?
`snprintf(dst, sizeof dst, "%s", src)` always null-terminates the destination buffer when size > 0, whereas `strncpy` can leave buffers unterminated.