Re: problem with vict being uninitialized/ crashing

From: Welcor (welcor@dune.net)
Date: 01/26/02


----- Original Message -----
From: "Josh Harris" <jharris2@ACSU.BUFFALO.EDU>
> It compiled fine, but when I actually tried to use the bail command it
> crashed my mud.  Here is my bail command:
>
A couple of things need to be adressed in this piece of code;
- remember to skip out of the function when a check fails (return!)
- remember to initialise vict before checking on it. You crashed due
  to dereferencing a NULL pointer.
- be consistent in your code; either use 'if () {} else if () {} else
  if () {} else {}' chains or (as in my example here) 'if () {}'
  'if () {}' 'if () {}' seperate if statements. The combination will
  often result in loss of overview.

ACMD(do_bail)
{
  struct char_data *vict;
  char arg[MAX_INPUT_LENGTH];

  one_argument(argument, arg);

  skip_spaces(&arg);
  delete_doubledollar(arg);

  if(!*arg) {
    send_to_char("Were you going to try and bail someone out?\r\n", ch);
    return; /* important ! */
  }

  if (!(vict = get_char_vis(ch, arg, FIND_CHAR_WORLD))) {
     send_to_char("Player isn't playing, try again...\r\n", ch);
    return; /* important too! */
  }

  if(ch==vict) {
    send_to_char("You can't bail yourself out of this one...\r\n", ch);
    return; /* still important ! */
  }

  if (GET_LEVEL(ch)<=GET_LEVEL(vict)) {
    send_to_char("You aren't high enough level to bail them out!\r\n", ch);
    return; /* if you don't return bad things will happen */
  }

  if (!PLR_FLAGGED(vict, PLR_CRIMINAL)) {
    send_to_char("There not in jail, their just visiting!\r\n", ch);
    return;
  }

  if (GET_GOLD(ch)<GET_BAIL(vict)) {
    send_to_char("You can't afford to get them out of jail!\r\n",ch);
    return; /* chose to do it this way for consistency */
  }
  /* we know now that everything is as it's supposed to */
  GET_GOLD(ch) = GET_GOLD(ch)-GET_BAIL(vict);
  GET_BAIL(vict)=0; /* only do this _after_ the bail is substracted */
  send_to_char("The door on the cell begins to slowly open, someone has"
               " bailed you out!\r\n", vict);
  send_to_char("You bail your friend out of jail!\r\n", ch);
  REMOVE_BIT(PLR_FLAGS(vict), PLR_CRIMINAL);
}

--
   +---------------------------------------------------------------+
   | 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