Re: Strcpy? Help?

From: Jason Wilkins (fenix@IO.COM)
Date: 09/24/98


Nick Stout wrote:

> I was told by one of my good friends that when i was having large code
> problems to write to you.
>
> Anyway, somewhere in my code there is a corrupted strcpy statement or
> buffer overload. I have a good idea where, because whenever it gets to
> "Blah is mortally wounded, and will die soon if not aided" it dies. Not
> the person, but the actual mud.

First off, comment out the code there, and add a log message of
somethingthat says it executed.  If the mud doesn't crash then thats your
problem
for sure.

Trace through the code with a debugger and see exactly where it crashes.
If you don't have a good debugger then add log messages around the
functions that you think the error is at:

log("trying suspect()";
suspect();
log("suspect succeeded()");

If you crash before returning from the function, trace it to the exact
statement.

If the statement takes a pointer then make sure that that pointer is not
NULL,
if its not null, then if its a string print it out.


> The whole thing goes down.
> It gives me a "Error in strcpy ()" error, which means it could be
> anywhere.

There are two things that cause exceptions while using strcpy, passing aNULL
pointer or passing something that does not have a terminating '\0'
character.

This is why you need to find the exact place thats causing the error, and
why you need to print out the value of its arguments when you get there.

An easy way, may be to write a macro like this:

#define strcpy(dst, src)  log("strcpy in file " _FILE_ " line " _LINE_),
strcpy(dst, src)

This will log every use of strcpy, and print out the filename and line
number.

crash your mud and look at the log.


> One person attempted to help, using strncpy. I dont know, but
> i couldn't get it to work, partially cause i'm too used to strcpy.

strncpy requires that you be aware of how much of your buffer is left,so it
is a little more involved, but safer.

> Is there anything i could do that might at all fix the problem? I have
> been trying to fix it, and i think i have just about tried everything
> now. Any ideas that a really good coder could give?

I don't know how often Circle uses strcpy, but the above hack shouldlog
every use of it if you make sure that the macro goes into every
file that uses strcpy.  make an include file, maybe called strcpy_test.h
and put that macro in every file that shows up when you do

grep strcpy *.c

> I realize you know code quite a bit better than I do. I can get along,
> but when huge problems like this arise, I can only sit back and hope for
> the best. I need an answer. Is there any other way i can fix this at
> all?

First, find exactly where the error is.  Then we can talk further.

> Thank you very much for your listening, and my note about this is also
> on the Circlemud list, if you need to know what I thought the problem
> was.
>
> -CyberMan

--
           Phoenix -- President of The Artistic Intuition Company
       Caelius * Mirror Reflex * Runica * X-Domain * Infinite Realms
                          http://www.io.com/~fenix


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST