Re: why this program is not crashing

manoj1978_at_gmail.com
Date: 02/11/05


Date: 11 Feb 2005 02:39:43 -0800


ghyott@yahoo.com wrote:
> hello,
> In my opinion the following code should crash when run with
> *(argv+1)="1234567890" and *(argv+2)="1234567890" .

It crashed when i ran this with microsoft visual c 7
>
> int main(int argc,char **argv)
> {
> char buf1[5];
> char buf2[5];
> char buf3[5];
> strncpy(buf2,*(argv+1),sizeof(buf2));
> strncpy(buf3,*(argv+2),sizeof(buf3));
> sprintf(buf1,"%s",buf2);
> return 0;
> }
>
> The last element of buf2 is not NULL.Therefore sprintf should copy
> "1234512345"
> to buf1 and which should result in segmentation fault.However,this is
> not the case and the program is running normally.Can anybody please
> pin-point my error.
>
> Thanks
buf1,buf2 and buf3 are stored in stack,so incrementing pointer to buf2
goes towards buf1 not buf3.if they were global then it will be as you
say.

buf1,buf2 buf3 have padding to make them start in addresses divisible
by 4.so most possibly 3 bytes padding.one of them may be zero by chance
that may be why it didn't crash in your system.



Relevant Pages

  • why this program is not crashing
    ... In my opinion the following code should crash when run with ... The last element of buf2 is not NULL.Therefore sprintf should copy ... to buf1 and which should result in segmentation fault.However,this is ...
    (comp.lang.c)
  • Re: why this program is not crashing
    ... > In my opinion the following code should crash when run with ... > The last element of buf2 is not NULL.Therefore sprintf should copy ...
    (comp.lang.c)