From: Kurt Zeilenga Date: Mon, 7 Jun 1999 18:48:22 +0000 (+0000) Subject: Hide assert(p == NULL) behind LDAP_MEMORY_DEBUG. X-Git-Tag: OPENLDAP_REL_ENG_2_BP~376 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=012f4e29b0e4cc019ab7f890580d646d021469b8;p=openldap Hide assert(p == NULL) behind LDAP_MEMORY_DEBUG. Add detection for duplicate free in LDAP_MEMORY_DEBUG code. --- diff --git a/libraries/liblber/io.c b/libraries/liblber/io.c index b785a67bd3..607bb753b5 100644 --- a/libraries/liblber/io.c +++ b/libraries/liblber/io.c @@ -178,7 +178,14 @@ ber_realloc( BerElement *ber, unsigned long len ) void ber_free( BerElement *ber, int freebuf ) { +#ifdef LDAP_MEMORY_DEBUG assert( ber != NULL ); +#endif + + if( ber == NULL ) { + return; + } + assert( BER_VALID( ber ) ); if ( freebuf && ber->ber_buf != NULL ) diff --git a/libraries/liblber/lber-int.h b/libraries/liblber/lber-int.h index 40d61109ec..cbe28423eb 100644 --- a/libraries/liblber/lber-int.h +++ b/libraries/liblber/lber-int.h @@ -201,7 +201,7 @@ extern BerMemoryFunctions* ber_int_memory_fns; #define LBER_INT_FREE(p) ber_memfree((p)) #define LBER_INT_VFREE(v) ber_memvfree((void**)(v)) #define LBER_INT_STRDUP(s) ber_strdup((s)) - + #define LBER_MALLOC(s) ber_memalloc((s)) #define LBER_CALLOC(n,s) ber_memcalloc((n),(s)) #define LBER_REALLOC(p,s) ber_memrealloc((p),(s)) diff --git a/libraries/liblber/memory.c b/libraries/liblber/memory.c index 5dcc1ec388..f40969ee2c 100644 --- a/libraries/liblber/memory.c +++ b/libraries/liblber/memory.c @@ -12,28 +12,47 @@ #include "lber-int.h" -#ifdef LDAP_MEMORY_DEBUG +#if LDAP_MEMORY_DEBUG struct ber_mem_hdr { union bmu_align_u { size_t bmu_size_t; void * bmu_voidp; double bmu_double; long bmu_long; + char bmu_char[4]; } ber_align; #define bm_junk ber_align.bmu_size_t +#define bm_data ber_align.bmu_char[1] }; #define BER_MEM_JUNK 0xddeeddeeU +static const struct ber_mem_hdr ber_int_mem_hdr = { BER_MEM_JUNK }; +#define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data) #endif BerMemoryFunctions *ber_int_memory_fns = NULL; +#if 0 && defined( LDAP_MEMORY_DEBUG ) +void +ber_int_memfree( void **p ) +{ + assert( p != NULL ); + assert( *p != BER_MEM_BADADDR ); + + ber_memfree( p ); + + *p = BER_MEM_BADADDR; +} +#endif + void ber_memfree( void *p ) { ber_int_options.lbo_valid = LBER_INITIALIZED; +#ifdef LDAP_MEMORY_DEBUG /* catch p == NULL when debugging */ assert( p != NULL ); +#endif /* ignore p == NULL when not debugging */ if( p == NULL ) { @@ -44,7 +63,9 @@ ber_memfree( void *p ) #ifdef LDAP_MEMORY_DEBUG struct ber_mem_hdr *mh = (struct ber_mem_hdr *) ((char *)p - sizeof(struct ber_mem_hdr)); - assert( mh->bm_junk == BER_MEM_JUNK ); + + assert( mh->bm_junk == BER_MEM_JUNK ); + mh->bm_junk = ~BER_MEM_JUNK; free( mh ); #else free( p ); @@ -64,7 +85,13 @@ ber_memvfree( void **vec ) { int i; +#ifdef LDAP_MEMORY_DEBUG assert(vec != NULL); /* vec damn better point to something */ +#endif + + if( vec == NULL ) { + return; + } for ( i = 0; vec[i] != NULL; i++ ) { LBER_FREE( vec[i] ); @@ -79,8 +106,10 @@ ber_memalloc( size_t s ) { ber_int_options.lbo_valid = LBER_INITIALIZED; +#ifdef LDAP_MEMORY_DEBUG /* catch s == 0 when debugging */ assert( s ); +#endif /* ignore s == 0 when not debugging */ if( s == 0 ) { @@ -112,8 +141,10 @@ ber_memcalloc( size_t n, size_t s ) { ber_int_options.lbo_valid = LBER_INITIALIZED; +#ifdef LDAP_MEMORY_DEBUG /* catch s,n == 0 when debugging */ assert( n && s ); +#endif /* ignore s,n == 0 when not debugging */ if( n == 0 || s == 0 ) { @@ -166,6 +197,8 @@ ber_memrealloc( void* p, size_t s ) mh = p; + assert( mh->bm_junk == BER_MEM_JUNK ); + return &mh[1]; #else return realloc( p, s ); @@ -181,7 +214,13 @@ ber_memrealloc( void* p, size_t s ) void ber_bvfree( struct berval *bv ) { +#ifdef LDAP_MEMORY_DEBUG assert(bv != NULL); /* bv damn better point to something */ +#endif + + if( bv == NULL ) { + return; + } ber_int_options.lbo_valid = LBER_INITIALIZED; @@ -197,7 +236,13 @@ ber_bvecfree( struct berval **bv ) { int i; +#ifdef LDAP_MEMORY_DEBUG assert(bv != NULL); /* bv damn better point to something */ +#endif + + if( bv == NULL ) { + return; + } ber_int_options.lbo_valid = LBER_INITIALIZED; @@ -214,7 +259,9 @@ ber_bvdup( { struct berval *new; - assert( bv != NULL ); +#ifdef LDAP_MEMORY_DEBUG + assert(bv != NULL); /* bv damn better point to something */ +#endif ber_int_options.lbo_valid = LBER_INITIALIZED; @@ -222,7 +269,6 @@ ber_bvdup( return NULL; } - if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) { return NULL; } @@ -246,10 +292,20 @@ ber_bvdup( } char * -(ber_strdup)( LDAP_CONST char *s ) +ber_strdup( LDAP_CONST char *s ) { char *p; - size_t len = strlen( s ) + 1; + size_t len; + +#ifdef LDAP_MEMORY_DEBUG + assert(s != NULL); /* bv damn better point to something */ +#endif + + if( s == NULL ) { + return( NULL ); + } + + len = strlen( s ) + 1; if ( (p = (char *) LBER_MALLOC( len )) == NULL ) { return( NULL );