delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/13/17:09:01

From: "David C. Hoos, Sr." <david DOT c DOT hoos DOT sr AT ada95 DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: strcat() bug?
Date: Tue, 11 Jan 2000 16:58:25 -0600
Organization: CRC: A wholly owned subsidiary of Thermo Electron
Lines: 66
Message-ID: <85gcle$go2$1@hobbes2.crc.com>
References: <20000111 DOT 154802 DOT -350271 DOT 2 DOT roberts DOT j DOT whitlock AT juno DOT com>
NNTP-Posting-Host: 198.175.145.56
X-Trace: hobbes2.crc.com 947631598 17154 198.175.145.56 (11 Jan 2000 22:59:58 GMT)
X-Complaints-To: abuse AT crc DOT com
NNTP-Posting-Date: 11 Jan 2000 22:59:58 GMT
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
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 in message
news:20000111 DOT 154802 DOT -350271 DOT 2 DOT roberts DOT j DOT whitlock AT juno DOT com...
> Hi, I was writing my program like we all do, and I came across this bug.
> My log file wasn't working right, not putting messages into the file, and
> leaving extra newlines in it, too. Well, I'm pretty sure the strcat()
> function is the one to blame. When I use it, the problem is there. When I
> don't, the problem disappears. However, what really gets me is that
> strcat() messes up some of the other strings as well. If you run the
> program when you use the logfile, one of the messages in a completely
> different part of the program gets messed up. You can see that on one of
> the alerts when the program startrs up. If you don't use the logfile, it
> comes out just fine. WTF?!?!? Since the bug was obviously not contained
> to one spot, I thought I should show you the whole program together. But,
> I didn't want to post it all, so:
Your program is at fault -- strcat() does not have a bug.

This kind of error, is but one of many reasons why I prefer to program in
Ada,
instead of C.

Here's the problem:

The left-most argument of strcat is the destination string, onto which the
string pointed to by the right-most argument is to be concatenated.

So... now, the question becomes -- "onto what did you specify to
concatenate?

In the case of sdk_log, you said to concatenate onto the end of the string
to which the msg parameter points.  In the case of the call to sdk_log from
sdk_exit, you are concatenating onto the end of the static string literal
"Shutting down.....etc."  There is no telling what the compiler placed after
that
static string -- but whatever it is  sure to get overwritten when you
concatenate on the end of the literal.

What you need to do, is to declare a static string locally in sdk_log, copy
msg to
it, then concatenate onto that -- like so:

char *sdk_log(char *msg)
{
static char temp [256]; // be sure this is lone enough for the worst case
    if (logfile != (FILE *) NULL)
    {
strcpy (temp, msg);
 fprintf(logfile,strcat(temp," typed a line\n"));
 file://fprintf(logfile,temp);
 fflush(logfile);
    }
    return temp;
}

I have no idea why you would want to return a pointer to the message
you've just written to the log file, but the only reason for making temp
static,
is so that temp is not on the stack, and will still be there when the return
value from sdk_log is referenced by the function that called sdk_log.

Ada simply will not let programmers make this kind of mistake.





- Raw text -


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