Home > database >  What is the reason for "stack smashing detected"?
What is the reason for "stack smashing detected"?

Time:02-22

I am new to programming and am currently studying about address typecasting. I don't seem to understand why I am getting this : *** stack smashing detected ***: terminated Aborted (core dumped) when I run the following code??

#include<iostream>
using namespace std;

void updateValue(int *p){
    *p = 610 % 255;
}

int main(){
    char ch = 'A';
    updateValue((int*)&ch);
    cout << ch; 
}

Here's what I understand about the code:

The address of ch is typecasted to int* and passed into the function updateValue(). Now, inside the updateValue() stack, an integer pointer p is created which points to ch. When p is dereferenced, it interprets ch as an int and reads 4(or 8) bytes of contiguous memory instead of 1. So, 'A'(65) along with some garbage value gets assigned to 610%5 i.e. 20.

But I don't understand, what and where things are going wrong?

CodePudding user response:

But I don't understand, what and where things are going wrong?

In this statement

*p = 610 % 255;

the memory that does not belong to the object ch that has the type char is overwritten. That is instead of one byte occupied by the object ch there are overwritten 4 bytes that correspond to the allocated memory for an object of the type int.

CodePudding user response:

when p is dereferenced, it interprets ...

When you indirect through the reinterpreted p and access an object of the wrong type, the behaviour of the program is undefined.

what and where things are going wrong?

Things started going wrong when you reinterpreted a pointer to one type as a pointer to an unrelated type.

Some rules of thumb:

  • Don't use reinterpret casts until you know what it does. They are difficult to get right, and are rarely useful.
  • Don't use reinterpret casts when it would result in undefined behaviour.
  • Don't use C-style casts at all.
  • If you think that you need to reinterpret cast, then take a few steps back, and consider why you think that you need it.

CodePudding user response:

The problem is that you're typecasting a char* to an int* and then dereferencing p which leads to undefined behavior.

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior. The program may just crash.

So the output that you're seeing(maybe seeing) is a result of undefined behavior. And as i said don't rely on the output of a program that has UB. The program may just crash which happens in your case.

For example, here the program crashes, but here it doesn't crash.

So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.


1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

  • Related