kkh
kkh

Reputation: 4869

Keep getting stack smashing error

I have an buffer of size 101

 char buffer[101]

I am trying to copy an address to the array

 int i;
 for(i=0;i<sizeof(buffer);i+=4)
     *(long*)&buffer[i] = address

in which address is of type long.

However I met with a stack smashing detection when I am running it. Any idea why?

Upvotes: 0

Views: 73

Answers (1)

Daniel Fischer
Daniel Fischer

Reputation: 183873

Alignment issues aside,

 for(i=0;i<sizeof(buffer);i+=4)
     *(long*)&buffer[i] = address

when i == 100 you write past the allocated buffer. You should stop when i > sizeof buffer - 4.

Upvotes: 6

Related Questions