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, Bing, Yandex

Members: 0
Guests: 5
Stats
Files
Topics
Posts
Members
Newest Member
481
3,734
19,366
618
Micheal64X
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » Bugfix Lists » SmaugFUSS Bugfix List » [Bug] Memory leak in polymorp...
Forum Rules | Mark all | Recent Posts

[Bug] Memory leak in polymorph loading code
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Aug 26, 2005, 4:38 am
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,643
JoinedJan 1, 2002

Bug: Memory leak in polymorph loading code
Danger: Medium - Leaks temporary strings
Found by: Remcon
Fixed by: Remcon/Samson

---

db.c

Below the str_dup function, add the following new function:
/* Read a string from file and return it */
char *fread_flagstring( FILE *fp )
{
   static char flagstring[MAX_STRING_LENGTH];
   char *plast;
   char c;
   int ln;

   plast = flagstring;
   flagstring[0] = '\0';
   ln = 0;
   /* Skip blanks. Read first char. */
   do
   {
      if( feof( fp ) )
      {
         bug( "%s: EOF encountered on read.", __FUNCTION__ );
         if( fBootDb )
            exit( 1 );
         return "";
      }
      c = getc(fp);
   }
   while( isspace( c ) );
      if( ( *plast++ = c ) == '~' )
         return "";

   for( ; ; )
   {
      if( ln >= ( MAX_STRING_LENGTH - 1 ) )
      {
         bug( "%s: string too long", __FUNCTION__ );
        *plast = '\0';
        return flagstring;
      }
      switch( *plast = getc( fp ) )
      {
         default:
            plast++;
            ln++;
            break;

         case EOF:
            bug( "%s: EOF", __FUNCTION__ );
            if( fBootDb )
               exit( 1 );
            *plast = '\0';
            return flagstring;
            break;

         case '\n':
            plast++;
            ln++;
            *plast++ = '\r';
            ln++;
            break;

         case '\r':
            break;

         case '~':
           *plast = '\0';
           return flagstring;
      }
   }
}


mud.h

Locate:
char *fread_string args( ( FILE * fp ) );


Below that, add:
char *fread_flagstring( FILE * fp );


polymorph.c, fread_morph

Locate:
               arg = fread_string( fp );


Replace with:
               arg = fread_flagstring( fp );


Locate:
               arg = fread_string_nohash( fp );
               arg = one_argument( arg, temp );
               while( temp[0] != '\0' )
               {
                  for( i = 0; i < MAX_PC_RACE; i++ )
                     if( !str_cmp( temp, race_table[i]->race_name ) )
                     {
                        SET_BIT( morph->race, ( 1 << i ) );
                        break;
                     }
                  arg = one_argument( arg, temp );
               }


Replace with:
               arg = fread_flagstring( fp );
               while( arg[0] != '\0' )
               {
                  arg = one_argument( arg, temp );
                  for( i = 0; i < MAX_PC_RACE; ++i )
                     if( !str_cmp( temp, race_table[i]->race_name ) )
                     {
                        SET_BIT( morph->race, ( 1 << i ) );
                        break;
                     }
               }


The use of temporary arguments like these to read things from files is very common throughout Smaug. Because fread_string and fread_string_nohash allocate to pointer memory, they tie up small amounts of memory each time this happens since you cannot later free the same memory. Usually trying to fix it to do so will simply refuse to compile. This left a paradoxical kind of problem. So fread_flagstring was conceived some time ago to address these problems in AFKMud and in Remcon's code. The same function can now be used to solve these problems in FUSS. This polymorph fix is only one of several that exist throughout the code and as they are found, fixes for them will be added to the list.
       
Pages:<< prev 1 next >>