delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/06/21:18:27

From: Daniel Barker <sokal AT holyrood DOT ed DOT ac DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Is that bug ?
Date: Sun, 7 Feb 1999 01:36:17 +0000
Organization: Edinburgh University
Lines: 45
Message-ID: <Pine.GSO.3.95.990207011324.12932A-100000@holyrood.ed.ac.uk>
References: <36BC27D0 DOT 41C67EA6 AT uic DOT rnd DOT runnet DOT ru>
NNTP-Posting-Host: holyrood.ed.ac.uk
Mime-Version: 1.0
X-Trace: scotsman.ed.ac.uk 918351249 14372 129.215.38.17 (7 Feb 1999 01:34:09 GMT)
X-Complaints-To: usenet AT scotsman DOT ed DOT ac DOT uk
NNTP-Posting-Date: 7 Feb 1999 01:34:09 GMT
In-Reply-To: <36BC27D0.41C67EA6@uic.rnd.runnet.ru>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sat, 6 Feb 1999, George Lugovoy wrote:

> I has a program:
> 
> //////////////////////////////////////////////////////////////////////
> #include <stdlib.h>
> 
> int main( void )
> {
>   char buffer[1024];
>   memcpy( buffer, NULL, sizeof( buffer ) );
>   return 0;
> }
> //////////////////////////////////////////////////////////////////////
> 
> Under DOS, Unix that program is crashed, BUT
> under Windows that program won't crash.
> What is it ? Is that bug ?

The program has undefined effect. It's trying to read 1024 bytes of memory
starting at the address NULL, which is an invalid address. Such bugs
typically have different effects on different systems. As I understand it,
the system is allowed (in the definition of the C language) to respond
however it likes. However, I think that running the above program through
the debugger GDB will reveal the error, on any system.

Another problem is that memcpy() is not declared in <stdlib.h>. memcpy() 
is declared in <string.h>. This could lead to trouble on some systems, due
to accidental casting of pointers to integers.

Although NULL is defined in DJGPP's header <stdlib.h>, Kernighan & Ritchie
(2nd edition) says it is defined in <stdio.h>. So, to be portable, you
should probably include both <string.h> and <stdio.h>, in addition to
fixing the main bug in the program.


Daniel Barker,
Institute of Cell and Molecular Biology,
Swann Building,
King's Buildings,
Mayfield Road,
Edinburgh
EH9 3JR
UK

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019