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

Members: 0
Guests: 13
Stats
Files
Topics
Posts
Members
Newest Member
481
3,735
19,370
618
Micheal64X
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » Bugfix Lists » AFKMud Bugfix List » AFKMud Bug Archive » [Bug] STRFREE and DISPOSE mac...
Forum Rules | Mark all | Recent Posts

[Bug] STRFREE and DISPOSE macros made memory safe
< Newer Topic :: Older Topic > AFKMud 1.7

Pages:<< prev 1 next >>
Post is unread #1 May 7, 2005, 1:49 am   Last edited Nov 13, 2005, 11:30 am 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: STRFREE and DISPOSE macros made memory safe.
Discovered in: AFKMud 1.7
Danger: None. Adds safety features to both macros.
Found by: Samson
Fixed by: Samson

---

mud.h

Locate:
#define DISPOSE(point)		\
do				\
{				\
   if((point))			\
  {				\
     free((point));		\
     (point) = NULL;		\
  }				\
} while(0)


Replace with:
#define DISPOSE(point)                         \
do                                             \
{                                              \
   if( (point) )                               \
   {                                           \
      if( typeid((point)) == typeid(char*) )   \
      {                                        \
         if( in_hash_table( (char*)(point) ) ) \
         {                                     \
            log_printf( "&RDISPOSE called on STRALLOC pointer: %s, line %d\n", __FILE__, __LINE__ ); \
            log_string( "Attempting to correct." ); \
            if( str_free( (char*)(point) ) == -1 ) \
               log_printf( "&RSTRFREEing bad pointer: %s, line %d\n", __FILE__, __LINE__ ); \
         }                                     \
         else                                  \
            free( (point) );                   \
      }                                        \
      else                                     \
         free( (point) );                      \
      (point) = NULL;                          \
   }                                           \
} while(0)


Locate:
#define STRFREE(point)				\
do						\
{						\
   if((point))					\
  {						\
	if( str_free((point)) == -1 )						\
       log_printf( "STRFREEing bad pointer in %s, line %d\n", __FILE__, __LINE__ ); \
    (point) = NULL;				\
  }						\
} while(0)


Replace with:
#define STRFREE(point)                           \
do                                               \
{                                                \
   if((point))                                   \
   {                                             \
      if( !in_hash_table( (point) ) )            \
      {                                          \
         log_printf( "&RSTRFREE called on str_dup pointer: %s, line %d\n", __FILE__, __LINE__ ); \
         log_string( "Attempting to correct." ); \
         free( (point) );                        \
      }                                          \
      else if( str_free((point)) == -1 )         \
         log_printf( "&RSTRFREEing bad pointer: %s, line %d\n", __FILE__, __LINE__ ); \
      (point) = NULL;                            \
   }                                             \
} while(0)


Locate:
void show_high_hash( int top );


Below that, add:
bool in_hash_table( char *str );



hashstr.c

At the bottom of the file, add:

bool in_hash_table( char *str )
{
   register int len, hash, psize;
   register struct hashstr_data *ptr;

   len = strlen( str );
   psize = sizeof( struct hashstr_data );
   hash = len % STR_HASH_SIZE;
   for( ptr = string_hash[hash]; ptr; ptr = ptr->next )
      if( len == ptr->length && str == ( (char *)ptr + psize ) )
         return true;
   return false;
}


The STRFREE and DISPOSE macros have been notorious for causing all kinds of problems when coders mistakenly use one to deallocate memory intended to be handled by the other and vice versa. These replacement macros will safeguard you against this pitfall and will issue a warning message when the wrong macro is called, and will correct the mistake on the fly. This is not intended to be used as a kludge to force code to behave, it is intended for use as a debugging aid to find problems and fix them properly.

NOTE: These macros are not intended for use in C codebases. This bugfix is intended soley for use on AFKMud and we will not support misuse of these macros in another codebase.
       
Pages:<< prev 1 next >>