Stack corruption due to array bound overwrite - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Stack corruption due to array bound overwrite

deathdeath hellPosts: 1Member

Hi,

I am trying to write a program where because of overwriting an array beyond its limit, the next stack memory gets modified. 

include <stdio.h>

int
main(void)
{

 char str[5];
 unsigned int a =10000;
 unsigned int ipaddr2 = 3774939393;
 unsigned int ipaddr1[2] = {3774939393, 5678};

 memcpy(&str, &ipaddr2, sizeof(int));
 printf("%d\n", a);
 a = a + 1;
 memcpy(&str, &ipaddr1, 2 * sizeof(int));
 printf("%d\n", a);

}

I was expecting that "memcpy(&str, &ipaddr1, 2 * sizeof(int))" writes 8 bytes and therefor corrupts "int a". However the second printf shows correct value. Why it is not crashing or why not showing corrupt value for a?

./a.out

10000
10001

Thanks

Comments

  • deathdeath hellPosts: 1Member

    Why not? str[5] takes 5 bytes. And 'a' comes after that. So via memcpy if I write 8 bytes of data on str then that should overwrite 'a' value also. right?

Sign In or Register to comment.