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

Date: Tue, 11 Jan 2000 16:06:44 -0600
From: Weiqi Gao <weiqi DOT gao AT edwardjones DOT com>
Subject: Re: strcat() bug?
To: djgpp AT delorie DOT com
Message-id: <387BA974.54124C27@edwardjones.com>
Organization: Edward Jones
MIME-version: 1.0
X-Mailer: Mozilla 4.51 [en]C-CCK-MCD (WinNT; U)
X-Accept-Language: en
References: <20000111 DOT 154802 DOT -350271 DOT 2 DOT roberts DOT j DOT whitlock AT juno DOT com>
Reply-To: djgpp AT delorie DOT com

Robert S Whitlock wrote:
> 
> 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:

When you use the strcat function, it is your responsibility to make sure
that the destination buffer is big enough to hold the combined string. 
Therefore 'strcat(msg, " typed a line\n")' where msg is initialized as
"Processing with GUI" will mess up other variables (first on the line is
the variable that's declared after the string.

The idiom
  char buf[1024];
  strcat(buf, "Some text");
  strcat(buf, "Some more text.");
is usually used to build up a short dynamic string.

--
Weiqi Gao
weiqigao AT a DOT crl DOT com

- Raw text -


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