GDB - I don't quite understand

Posted by Nick Cash on Sun 14 Dec 2003 07:19 AM — 14 posts, 28,133 views.

USA #0
Alright, I was using gdb on my core file and got this after a backtrace:


#0  0x420807b6 in strcpy () from /lib/i686/libc.so.6
#1  0x080589b7 in profanity_filter (
    arg=0x8153200 "You're home planet is a little hard to get to right now.",
    out=0x8153200 "You're home planet is a little hard to get to right now.") at act_comm.c:446
#2  0x08059b7e in do_say (ch=0x8287858,
    argument=0x8153200 "You're home planet is a little hard to get to right now.")
    at act_comm.c:1056
#3  0x080deba9 in spec_newbie_pilot (ch=0x8287858) at special.c:160
#4  0x080ec279 in mobile_update () at update.c:689
#5  0x080eeaf5 in update_handler () at update.c:2227
#6  0x080922ff in game_loop () at comm.c:573
#7  0x08091b2c in main (argc=7, argv=0xbffffac4) at comm.c:236
#8  0x42017589 in __libc_start_main () from /lib/i686/libc.so.6


There are only two things in there that tell me anything I'm understanding. One, it has an issue with my profanity filter, or two (or both I guess) it has a problem with spec_newbie_pilot. What would you suggest?
USA #1
Most likely the filter. The backtrace simply shows you what happened before the crash.
Australia Forum Administrator #2
Can you paste the code in the profanity filter? Some versions just comment that out anyway, sounds like it wasn't done properly.
USA #3


/*
 * Profanity Filter
 */
void profanity_filter( char * arg, char * out )
{
   char *nbuf, *tbuf, *p;
   bool change=TRUE;
   int x, v;

   nbuf = malloc(strlen(arg)+1);
   tbuf = malloc(strlen(arg)+1);

   strcpy(nbuf, strlower(arg));
   strcpy(tbuf, arg);

   while (change)
   {
      change = FALSE;

      for (x = 0; str_cmp(ignore_table[x], "$"); x++)
      {
	  if ( ( p = strstr(nbuf, ignore_table[x]) ) )
	  {
	    change = TRUE;
	    for (v = 0; v < strlen(ignore_table[x]); v++)
		*(p+v) = '.';
	  }
      }
   }

   change = TRUE;
   while (change)
   {
	change = FALSE;

	for (x = 0; strcmp(curse_table[x], "$"); x++)
	{
	   if ( ( p = strstr(nbuf, curse_table[x]) ) )
  	   {
		   change = TRUE;
		   for (v = 0; v < strlen(curse_table[x]); v++)
		     *(p+v) = '*';
           }
	}
   }

/*
 * Clonse *'s over to the real string
 */
for( x = 0; x < strlen( nbuf ); x++ )
{
	if ( nbuf[x] == '*' ) tbuf[x] = '*';
}

strcpy( out, tbuf );

free(nbuf);
free(tbuf);

return;

}


Perhaps its that its using free instead of another function? (DISPOSE perhaps?) I'm not really sure.
Australia Forum Administrator #4
Free goes with malloc, so that part is OK. There are a number of strcpy calls in that function. I would put a breakpoint on it and step through and see which line is crashing.
USA #5
It doesnt crash every time though. Its kind of strage, but I'm sure its a memory problem. I'll post back in a while with any news I get.
USA #6
Maybe it's called incorrectly, that is, with bad parameters.

What bothers me from your code is the last time strcpy is used, on the "out" pointer. Chances are you're not calling it correctly at some point, and from the backtrace it appears that's in do_say.
USA #7
Maybe its encountering problems with the quote(s)..?
Amended on Tue 16 Dec 2003 01:13 AM by Zeno
USA #8
The meat of the problem lies in the filter itself:

Quote:

void profanity_filter( char * arg, char * out )


Notice it's being passed these arguments from elsewhere, and you have no way of knowing how large the memory space for them is. The other strcpys the function uses are also bothersome looking to me, but I'm not sure they'd be a problem. It's the one where the contents of 'tbuf' are copied back into 'out' that I suspect is why it crashes.

It's most likely overflowing the memory boundary for *out and corrupting whatever is stored beyond it.
USA #9
How would you reccomend I fix it?
USA #10
Quoting myself:
Quote:
Chances are you're not calling it correctly at some point, and from the backtrace it appears that's in do_say.


So go look at the backtrace, see the line that's calling it, and look for a bad argument. As Samson and I have pointed out that "out" argument is being allocated elsewhere, and quite possibly it's not being allocated correctly just one time. Your backtrace will show you where it was called, and that point will at least give hints as to where the error is.
Australia Forum Administrator #11
Quote:

How would you reccomend I fix it?


Forget the profanity filter. There are plenty of ways of getting around them, and get your intent across. eg. a space between each letter.
USA #12
Heh, my thoughts exactly. I had just taken the damn thing out before I read this post. Thanks for the ideas anyways!
USA #13
I was actually looking over stuff in a non-updated version of my code that still contained this and thought of something. Perhaps the reason it was messing up was because it was being called using argument twice, like this:

profanity_filter( argument, argument );

Now to me that seems faulty. Just a thought, not like I'm going to put it back in my code anyways, unless things get out of hand.

It would appear you were right Kysilyan.