Re: why this program is not crashing
From: Flash Gordon (spam_at_flash-gordon.me.uk)
Date: 02/15/05
- Previous message: sangeetha: "Re: transpose of a nx1 matrix"
- In reply to: manoj1978_at_gmail.com: "Re: why this program is not crashing"
- Next in thread: Prawit Chaivong: "Re: why this program is not crashing"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 15 Feb 2005 13:55:29 +0000
manoj1978@gmail.com wrote:
> 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.
Maybe on your implementation, but in general it is whatever the compiler
feels like doing. The compiler could see that main is not called
recursively and therefore implement buf1, buf2 and buf3 as if they were
static, it could reorder them, possibly on the basis of when they are
used, it could add in sentinel values between them to allow for the
detection of buffer overflows, or anything else it likes.
> buf1,buf2 buf3 have padding to make them start in addresses divisible
> by 4.so most possibly 3 bytes padding.
Again, that may apply to your system but it is not true in general.
> one of them may be zero by chance
> that may be why it didn't crash in your system.
Speculating why someone else's system does not crash on buffer overflow
(or any other undefined behaviour) on the basis of you implementation is
generally pointless.
-- Flash Gordon Living in interesting times. Although my email address says spam, it is real and I read it.
- Previous message: sangeetha: "Re: transpose of a nx1 matrix"
- In reply to: manoj1978_at_gmail.com: "Re: why this program is not crashing"
- Next in thread: Prawit Chaivong: "Re: why this program is not crashing"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|