Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
tintin++ ogg sound player script for linux
Author: Robert Smith
Submitted by: Vladaar
6Dragons ogg Soundpack
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.4
Author: Vladaar
Submitted by: Vladaar
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
Users Online
CommonCrawl, Google

Members: 0
Guests: 6
Stats
Files
Topics
Posts
Members
Newest Member
481
3,734
19,366
618
Micheal64X
Today's Birthdays
Vintar (28)
Related Links
» SmaugMuds.org » Bugfix Lists » AFKMud Bugfix List » AFKMud Bug Archive » [Bug] Name generator system l...
Forum Rules | Mark all | Recent Posts

[Bug] Name generator system leaks string hash memory
< Newer Topic :: Older Topic > AFKMud 1.76a

Pages:<< prev 1 next >>
Post is unread #1 Dec 25, 2005, 7:56 pm   Last edited Jan 22, 2006, 6:45 pm by Samson
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,643
JoinedJan 1, 2002

Bug: Name generator system leaks string hash memory
Discovered in: AFKMud 1.76a
Danger: Medium - Slight possibility of hash corruption
Found by: Samson
Fixed by: Samson

---

db.c, load_mobiles

Locate:
      char *desc = fread_flagstring( fp );
      if( desc && desc[0] != '\0' && str_cmp( desc, "(null)" ) )
      {
         pMobIndex->chardesc = STRALLOC( desc );
         pMobIndex->chardesc[0] = UPPER( pMobIndex->chardesc[0] );
      }

      if( pMobIndex->long_descr != NULL )
         pMobIndex->long_descr[0] = UPPER( pMobIndex->long_descr[0] );


Change to:
      char *desc = fread_flagstring( fp );
      if( desc && desc[0] != '\0' && str_cmp( desc, "(null)" ) )
      {
         pMobIndex->chardesc = STRALLOC( desc );
         if( str_prefix( "namegen", desc ) )
            pMobIndex->chardesc[0] = UPPER( pMobIndex->chardesc[0] );
      }

      if( pMobIndex->long_descr != NULL && str_prefix( "namegen", pMobIndex->long_descr ) )
         pMobIndex->long_descr[0] = UPPER( pMobIndex->long_descr[0] );


reset.c, reset_room

Locate:
   char namegenCheck[MSL], mob_keywords[MSL];
   char *namegenCheckString;


Change to:
   char mob_keywords[MSL];


Locate:
            /*
             * Added by Tarl 4 Dec 02 so that if a mob is 'flagged' namegen in
             * his name, it will auto assign a random name to it. Similarly,
             * occurances of namegen in the long_descr and description will be
             * replaced with the name.
             */

            namegenCheckString = mob->name;
            namegenCheckString = one_argument( namegenCheckString, namegenCheck );

            /*
             * Modified by Tarl 5 Dec 02 to add extra namegen options. ie, namegen_gr will pick a name
             * suitable for Graecian mobs.
             * 
             * To add more, edit the line below this, and add an if-check similar to the one starting
             * with if( !strcasecmp( namegenCheck, "namegen_gr" ) )
             */
            if( ( !strcasecmp( namegenCheck, "namegen" ) ) || ( !strcasecmp( namegenCheck, "namegen_gr" ) )
                || ( !strcasecmp( namegenCheck, "namegen_ven" ) ) || ( !strcasecmp( namegenCheck, "namegen_orc" ) ) )
            {
               char nameg[MSL];
               bool namePicked = false;
               char *tempStr = NULL, tempStr2[MSL], *tempStr3 = NULL, tempStr4[MSL], temp[MSL];
               unsigned int x = 0;
               char file[256];

               nameg[0] = '\0';
               tempStr2[0] = '\0';
               tempStr4[0] = '\0';
               if( !strcasecmp( namegenCheck, "namegen_gr" ) )
               {
                  namePicked = true;
                  if( mob->sex == SEX_FEMALE )
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_gr_female.txt" );
                  else
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_gr_other.txt" );
               }
               if( !strcasecmp( namegenCheck, "namegen_ven" ) )
               {
                  namePicked = true;
                  if( mob->sex == SEX_FEMALE )
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_ven_female.txt" );
                  else
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_ven_other.txt" );
               }
               if( !strcasecmp( namegenCheck, "namegen_orc" ) )
               {
                  namePicked = true;
                  if( mob->sex == SEX_FEMALE )
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_orc_female.txt" );
                  else
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_orc_other.txt" );
               }
               if( !namePicked )
                  name_generator( nameg );
               else
                  pick_name( nameg, file );

               STRFREE( mob->name );
               STRFREE( mob->short_descr );
               mob_keywords[0] = '\0';
               mudstrlcat( mob_keywords, namegenCheckString, MSL );
               mudstrlcat( mob_keywords, " ", MSL );
               mudstrlcat( mob_keywords, nameg, MSL );
               mob->name = STRALLOC( mob_keywords );
               mob->short_descr = STRALLOC( nameg );
               tempStr = STRALLOC( mob->long_descr );
               STRFREE( mob->long_descr );
               while( tempStr[0] != '\0' )
               {
                  tempStr = one_argument( tempStr, temp );
                  if( !strcasecmp( temp, "namegen" ) )
                     mudstrlcat( tempStr2, nameg, MSL );
                  else
                  {
                     if( !strncasecmp( temp, "namegen", 7 ) )
                     {
                        for( x = 7; x < strlen( temp ); x++ )
                           temp[x - 7] = temp[x];

                        temp[strlen( temp ) - 7] = '\0';
                        mudstrlcat( tempStr2, nameg, MSL );
                        mudstrlcat( tempStr2, temp, MSL );
                     }
                     else
                        mudstrlcat( tempStr2, temp, MSL );
                  }
                  mudstrlcat( tempStr2, " ", MSL );
               }
               mob->long_descr = STRALLOC( tempStr2 );
               if( mob->chardesc )
               {
                  tempStr3 = STRALLOC( mob->chardesc );
                  STRFREE( mob->chardesc );
                  while( tempStr3[0] != '\0' )
                  {
                     tempStr3 = one_argument( tempStr3, temp );
                     if( !strcasecmp( temp, "namegen" ) )
                        mudstrlcat( tempStr4, nameg, MSL );
                     else
                     {
                        if( !strncasecmp( temp, "namegen", 7 ) )
                        {
                           for( x = 7; x < strlen( temp ); x++ )
                              temp[x - 7] = temp[x];

                           temp[strlen( temp ) - 7] = '\0';
                           mudstrlcat( tempStr4, nameg, MSL );
                           mudstrlcat( tempStr4, temp, MSL );
                        }
                        else
                           mudstrlcat( tempStr4, temp, MSL );
                     }
                     mudstrlcat( tempStr4, " ", MSL );
                  }
                  mob->chardesc = STRALLOC( tempStr4 );
               }
            }


Change to:
            /*
             * Added by Tarl 4 Dec 02 so that if a mob is 'flagged' namegen in
             * his name, it will auto assign a random name to it. Similarly,
             * occurances of namegen in the long_descr and description will be
             * replaced with the name.
             *
             * Modified by Tarl 5 Dec 02 to add extra namegen options. ie, namegen_gr will pick a name
             * suitable for Graecian mobs.
             * 
             * To add more, edit the line below this, and add an if-check similar to the one starting
             * with if( strstr( mob->name, "namegen_gr" ) )
             *
             * And then Samson shows up and cleans up the code, kills off the memory leaks, and life was good.
             * Or something like that anyway. Merry Christmas 2005!
             */
            if( strstr( mob->name, "namegen" ) )
            {
               char nameg[MSL];
               char *genstring = "namegen";
               bool namePicked = false;
               char tempStr[MSL], tempStr2[MSL];
               char file[256];

               if( strstr( mob->name, "namegen_gr" ) )
               {
                  genstring = "namegen_gr";
                  namePicked = true;
                  if( mob->sex == SEX_FEMALE )
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_gr_female.txt" );
                  else
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_gr_other.txt" );
               }
               else if( strstr( mob->name, "namegen_ven" ) )
               {
                  genstring = "namegen_ven";
                  namePicked = true;
                  if( mob->sex == SEX_FEMALE )
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_ven_female.txt" );
                  else
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_ven_other.txt" );
               }
               else if( strstr( mob->name, "namegen_orc" ) )
               {
                  genstring = "namegen_orc";
                  namePicked = true;
                  if( mob->sex == SEX_FEMALE )
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_orc_female.txt" );
                  else
                     snprintf( file, 256, "%s%s", SYSTEM_DIR, "namegen_orc_other.txt" );
               }

               nameg[0] = '\0';
               if( !namePicked )
                  name_generator( nameg );
               else
                  pick_name( nameg, file );

               mudstrlcpy( mob_keywords, mob->name, MSL );
               mudstrlcat( mob_keywords, " ", MSL );
               mudstrlcat( mob_keywords, nameg, MSL );
               STRFREE( mob->name );
               mob->name = STRALLOC( mob_keywords );
               STRFREE( mob->short_descr );
               mob->short_descr = STRALLOC( nameg );

               mudstrlcpy( tempStr, strrep( mob->long_descr, genstring, nameg ), MSL );
               STRFREE( mob->long_descr );
               mob->long_descr = STRALLOC( tempStr );

               if( mob->chardesc )
               {
                  mudstrlcpy( tempStr2, strrep( mob->chardesc, genstring, nameg ), MSL );
                  STRFREE( mob->chardesc );
                  mob->chardesc = STRALLOC( tempStr2 );
               }
            }


The name generator has had a memory leak problem from the day it was created. Basically because of how it handled the temporary strings it would cause the original contents of the string hash to get modified and no longer match with what was there, except in the off chance the new name had the same length as the word "namegen". It had been a memory leak we just lived with for the longest time but then it hit me - strrep exists and that makes life good. Namegen was around before strrep was added. The fix for this just sort of hit me while I was cleaning it up in the C++ codebase. Merry Christmas :)
       
Pages:<< prev 1 next >>