Re: buffer overflow
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Thu, 09 Nov 2006 09:45:08 -0500
Roman Mashak wrote:
can't realize what's happening in this code snippet:
int main(int argc, char *argv[])
{
char buf[256];
strcpy(buf, argv[1]);
...
}
Debugger shows argv[1] as NULL and as a result I get 'segmentation
fault' on 'strcpy' call. I can't figure out why NULL is not a proper
in this case, standard doesn't prohibit it in string functions (at
least I have not found it).
However the standard does require a proper string to copy into
buf. A string is a sequence of bytes, possibly empty, followed by
a '\0' byte. A NULL pointer doesn't point to anything, so there is
no place for that '\0'. So you need a statement such as:
if (argv[1]) strcpy(buf, argv[1]);
else buf[0] = '\0';
Of course argv[1] may not even exist, so you should also guard by:
if (argc > 1) ...
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
.
- Follow-Ups:
- Re: buffer overflow
- From: Ian Malone
- Re: buffer overflow
- References:
- buffer overflow
- From: Roman Mashak
- buffer overflow
- Prev by Date: Re: different struct sizes
- Next by Date: Re: display the attchment list
- Previous by thread: Re: buffer overflow
- Next by thread: Re: buffer overflow
- Index(es):
Relevant Pages
|