Re: Redesign of set_title causes problems

From: Kras Kresh (kras_kresh@hotmail.com)
Date: 04/23/02


>From: Christian Ejlertsen <chr.ejlertsen@has.dk>
>Subject: [CIRCLE] Redesign of set_title causes problems
>Date: Tue, 23 Apr 2002 20:59:24 +0200
>
>Worked on a flexible title command so that you can have pretitles.
>Made it so you would set your title like this
>title Sir * the Valiant Knight
>This will resolve into Sir Playername the Valiant Knight.
>It works sweetly actually, but then i wanted to add a check that
>insured that the asteriks was in the title somewhere.
>When the asteriks is found no problems, when no asterisk it goes
>through the if and sets it. It even prompts me that it has the set the
>title, but then it's chrashes. it hangs like some sort of endless loop.
>Gdb output is.
>Program received signal SIGSEGV, segmentation fault.
>0x400cf177 in malloc() from /lib/libc.so.6
>
>What wrong ???
>
>The set_title snippet is supplied below.
>
>## START SNIPPET ##
>
>void set_title(struct char_data * ch, char *title)
>{
>   const char *i = NULL;
>   char lbuf[MAX_STRING_LENGTH], *buf, *retitle;
>   bool found = FALSE;
>
>   buf = lbuf;
>
>   if (title == NULL && !PLR_FLAGGED(ch, PLR_ROLLED)) {
>     if (GET_SEX(ch) == SEX_FEMALE)
>       title = title_female(GET_RACE(ch), GET_LEVEL(ch));
>     else
>       title = title_male(GET_RACE(ch), GET_LEVEL(ch));
>   }
>
>   if (strlen(title) > MAX_TITLE_LENGTH)
>     title[MAX_TITLE_LENGTH] = '\0';
>
>   if (GET_TITLE(ch) != NULL)
>     free(GET_TITLE(ch));
>
>   for (;;) {
>     if (*title == '*') {
>         found = TRUE;
>         i = GET_NAME(ch);
>        while ((*buf = *(i++)))
>               buf++;
>        title++;
>     }
>     else if (!(*(buf++) = *(title++)))
>       break;
>   }
>
>   *(--buf) = '\0';
>
>        /* if no asteriks (*) found reset title */
>        if (!found) {
>           if (GET_SEX(ch) == SEX_FEMALE) {
>               retitle = title_female(GET_RACE(ch), GET_LEVEL(ch));
>           }
>           else {
>               retitle = title_male(GET_RACE(ch), GET_LEVEL(ch));
>           }
>        send_to_char("Title has to contain the (*) asterisk sign to be
>valid!\r\n"
>                            "Title reset to default.\r\n", ch);
>         set_title(ch, retitle);
>         return;
>       }
>
>   GET_TITLE(ch) = str_dup(lbuf);
>
>   sprintf(buf2, "Okay, you're now %s.\r\n", GET_TITLE(ch));
>   send_to_char(buf2, ch);
>}
>
>## STOP SNIPPET ##


I'm assuming that everything else works and the function there is the only
problem. You might want to add a 'char buf2[MAX_STRING_LENGTH];' at the
beginning of that function since you do use it inside of it. Next, look at
this "free(GET_TITLE(ch));" add in a "GET_TITLE(ch) = NULL;" right after
that so that you get:

  if (GET_TITLE(ch) != NULL) {
    free(GET_TITLE(ch));
    GET_TITLE(ch) = NULL;
  }

That should be your problem. In you loop there, you call on the function
itself so its a recursion. But remember that your GET_TITLE(ch) still
pointed to something. It would attempt to free it again, causing your seg
fault. I hope that is the problem, cuz thats the easiest thing to see.

_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT