Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
Bug in will_fall( )
Oct 23, 2017, 1:35 am
By GatewaySysop
Bug in do_zap( ), do_brandish( )
Oct 18, 2017, 1:52 pm
By GatewaySysop
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Memwatch
Author: Johan Lindh
Submitted by: Vladaar
Users Online
CommonCrawl, DotBot

Members: 0
Guests: 7
Stats
Files
Topics
Posts
Members
Newest Member
477
3,706
19,240
608
LAntorcha
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » Bugfix Lists » SWR FUSS Bugfix List » [Bug] STRFREE and DISPOSE mac...
Forum Rules | Mark all | Recent Posts

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

Pages:<< prev 1 next >>
Post is unread #1 May 7, 2005, 2:21 am
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

Bug: STRFREE and DISPOSE macros made memory safe.
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( in_hash_table( (char*)(point) ) ) \
      {                                     \
         bug( "&RDISPOSE called on STRALLOC pointer: %s, line %d", __FILE__, __LINE__ ); \
         log_string( "Attempting to correct." ); \
         if( str_free( (char*)(point) ) == -1 )  \
            bug( "&RSTRFREEing bad pointer: %s, line %d", __FILE__, __LINE__ ); \
      }                                     \
      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) ) )           \
      {                                         \
         bug( "&RSTRFREE called on str_dup pointer: %s, line %d", __FILE__, __LINE__ ); \
         log_string( "Attempting to correct." ); \
         free( (point) );                       \
      }                                         \
      else if( str_free((point)) == -1 )        \
         bug( "&RSTRFREEing bad pointer: %s, line %d", __FILE__, __LINE__ ); \
      (point) = NULL;                           \
   }                                            \
} while(0)


Locate:
void show_high_hash( int top );


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



hashstr.c

Just below the includes, add:
#if !defined(FALSE)
#define FALSE 0
#endif

#if !defined(TRUE)
#define TRUE 1
#endif

typedef unsigned char bool;


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 will not be supported if not used within a SMAUG derived C codebase.
       
Pages:<< prev 1 next >>