On Tue, 14 Jan 2003, Michael Barton wrote:

> I don't see anywhere that you're allocating dynamic memory..
> You shouldn't really be using any perms here, much less losing them.
> Make sure in the version of strip_color on your mud, you're not calling
> str_dup (which was in the function I originally posted).
>
> One thing I noticed, buf should be defined as a 'static char' in strip_color
> since you're returning a pointer to it.  Basically, buf right now is freed
> when the function returns and could be overwritten by who knows what.
> Making it static means the memory stays allocated between calls to the
> function.

Also, once you make it static (which you must do or bad things can happen),
you need to be sure to remember that the buffer will be overwritten
the next time you call the function.

Since you're calling it multiple times in this function, you'll want
to strcpy() the result of the function call into a local buffer
instead of just keeping a pointer to the result.

In this particular function, you can get by with doing this with
just string.  You can use just a pointer for list, since the next
time strip_color is called, you're replacing list anyways.


Dennis


> > I am having a slight problem though, or at least I think it's a problem.
> > I am noticing a big increase in perms since I added this code ... the
> > only place I use this code is in the is_name function which I know is
> > called a ton of places.  Here is my is_name function with the
> > strip_color calls in it.
> >
> > bool is_name(char *str, char *namelist)
> > {
> >      char name[MAX_INPUT_LENGTH], part[MAX_INPUT_LENGTH];
> >      char *list, *string;
> >
> >     /* fix crash on NULL namelist */
> >      if (namelist == NULL || namelist[0] == '\0')
> >           return FALSE;
> >
> >     /* fixed to prevent is_name on "" returning TRUE */
> >      if (str[0] == '\0')
> >           return FALSE;
> >
> >      string = strip_color(str);
> >     /* we need ALL parts of string to match part of namelist */
> >      for (;;)
> >      {
> >           /* start parsing string */
> >           str = one_argument(str, part);
> >
> >           if (part[0] == '\0')
> >                return TRUE;
> >
> >     /* check to see if this is part of namelist */
> >           list = strip_color(namelist);
> >           for (;;)
> >           {
> >                /* start parsing namelist */
> >                list = one_argument(list, name);
> >                if (name[0] == '\0') /* this name was not found */
> >                     return FALSE;
> >
> >                if (!str_prefix(string, name))
> >                     return TRUE;    /* full pattern match */
> >
> >                if (!str_prefix(part, name))
> >                     break;
> >           }
> >      }
> > }


Reply via email to