Runtime (SIGSEGV / Signal)Memory Safety7 min read

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.

Broken Pattern
char name[8];
strcpy(name, "onlinecompiler");
Corrected Pattern
char name[8];
snprintf(name, sizeof(name), "%s", "online");
Illustrative diagnostic (not executed):*** stack smashing detected ***: terminated (Aborted / Exit 134)

Off-by-one loop condition (<= size instead of < size)

Writing arr[N] touches N bytes beyond the end of int arr[N].

Broken Pattern
int arr[4];
for (int i = 0; i <= 4; i++) arr[i] = i;
Corrected Pattern
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 compiler
Read 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 compiler

Expected output from the corrected program

Reference output, not a live execution result.

SAFE_TAG

Compiler & Sanitizer Diagnostic Matrix

Compiler / Runtime SymptomRoot CauseDiagnostic Flag / CheckCorrected Pattern
*** stack smashing detected *** (SIGABRT)Buffer write overwrote saved stack canary-fstack-protector-strong + ASanBound copies with `snprintf(dst, sizeof dst, ...)`
off-by-one `<= N` loop writeLoop wrote `arr[N]` past end of stack frame-fsanitize=addressIterate `0 <= i < N` strictly

▶ Load failing reproducer in Online C Compiler

Debugging Techniques and Commands

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

Strong Stack Protector
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.