Re: NEWBIE CODE: Segmentation Fault

From: Patrick Dughi (dughi@imaxx.net)
Date: 08/15/00


> I'm using circle bpl17 with ascii player files and I have made a code to
> show the players that had more deaths. But sometimes (not always,
> sometimes), I used the kgdb to read the core file and I found Segmentation
> Fault error. This is the code (I'll to show the line where the error
> occurs):

>  char buft1[1000];
>  char jogador[20];
>  int jogadores[10] = {};

.
.
> ==>         strcpy(jogador, ler->player.name);  ====================> this
> is the line where the error occurs
>

        I would try using 'strncpy(jogador, ler->player.name,20);'

        I would also put some sort of string length check on what you add
to buft1.  If you overwrite buft1, it may not crash, but it could walk
down the stack and crash all sorts of other things.  Depending how your
compiler allocates things, there's a good chance that it simply allocates
memory in a simple sequential, back to back, order.. looking at the order
up there, jogador (and jogadores) immediately follow buft1.  They'd be
likely to be corrupted by overruns first.

                                                        PjD


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



This archive was generated by hypermail 2b30 : 04/11/01 PDT