delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/11/18:56:35

From: Jason Green <news AT jgreen4 DOT fsnet DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: strcat() bug?
Date: Tue, 11 Jan 2000 23:19:42 +0000
Organization: Customer of Planet Online
Lines: 45
Message-ID: <1hen7ssm9h8j95k9kgi54cbsuvgtsbt2j6@4ax.com>
References: <20000111 DOT 154802 DOT -350271 DOT 2 DOT roberts DOT j DOT whitlock AT juno DOT com>
NNTP-Posting-Host: modem-4.actinium.dialup.pol.co.uk
Mime-Version: 1.0
X-Trace: news5.svr.pol.co.uk 947633092 24205 62.136.44.4 (11 Jan 2000 23:24:52 GMT)
NNTP-Posting-Date: 11 Jan 2000 23:24:52 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Newsreader: Forte Agent 1.7/32.534
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Robert S Whitlock <roberts DOT j DOT whitlock AT juno DOT com> wrote:

> For me, this is truly a wtf. Any ideas?

There could be other bugs, but I found this one simply by grepping for
strcat.  This is really all the code you needed to post:

/* ... */

char *sdk_log(char *msg)
{
    if (logfile != (FILE *) NULL)
    {
	fprintf(logfile,strcat(msg," typed a line\n"));
	//fprintf(logfile,msg);
	fflush(logfile);
    }
    return msg;
}

/* ... */

sdk_log("Failure to initialize Allegro");


This won't work. 

You are passing a *constant* string literal to sdk_log, then you
modify that string.  The compiler has made code to assume the string
would not be modified, but it did not catch the error because the
constness is lost when msg is declared as char*.

Preferred solution: declare msg as const char* and change sdk_log() so
it will work without modifying msg.

Or be sure to call sdk_log() only with strings that have enough spare
space for whatever you want to append. 

Casting 'const char*' to 'char*' is legal (for historical reasons) but
is a bad thing because it encourages bugs (like the one you have).

Compiling with -Wwrite-strings might help to point out places where
this is happening.

Hope that helps.

- Raw text -


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