Trusted answers to developer questions

What is the "stack smashing detected" error?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

Usually, the compiler generates the stack smashing detected error in response to its defense mechanism against buffer overflows.

A buffer​ overflow occurs when the user input exceeds the buffer capacity. The following C code can cause the buffer to overflow if the user enters more than ten characters. In such a ​case, the compiler will throw the stack smashing detected error.

Press + to interact
#include<stdio.h>
void get_name(){
char buffer[10];
gets(buffer);
}
int main() {
printf("Please enter your name.");
get_name();
return 0;
}

A buffer overflow

The following illustration explains the concept of a buffer overflow:

1 of 5

Using canaries

A buffer overflow is dangerous, so the compiler uses various protection mechanisms to guard against it. One such mechanism is a canary. A canary is a randomly generated value, ​and in the case of a buffer overflow, the canary is overwritten; when, upon comparison with the known value, the compiler detects that the stack was compromised it throws the stack smashing detected error.

The following demonstrates how canaries are used:

1 of 2

RELATED TAGS

stack smashing
errors
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?