From f4e007515431372cac83800054ec74a14d51d668 Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Sat, 15 Nov 2003 10:02:44 +0000 Subject: [PATCH] more memleaks fixed; some cleanup --- libraries/librewrite/config.c | 6 +++++ libraries/librewrite/context.c | 5 ++--- libraries/librewrite/info.c | 3 +++ libraries/librewrite/map.c | 1 + libraries/librewrite/params.c | 12 +++++++--- libraries/librewrite/rewrite.c | 2 +- libraries/librewrite/rule.c | 41 ++++++++++++++++++++++++++++++++++ libraries/librewrite/subst.c | 39 +++++++++++++++++++++----------- libraries/librewrite/xmap.c | 1 + 9 files changed, 90 insertions(+), 20 deletions(-) diff --git a/libraries/librewrite/config.c b/libraries/librewrite/config.c index baf88d40a7..514b16c415 100644 --- a/libraries/librewrite/config.c +++ b/libraries/librewrite/config.c @@ -75,6 +75,7 @@ rewrite_parse( "[%s:%d] rewriteEngine needs 'state'\n%s", fname, lineno, "" ); return -1; + } else if ( argc > 2 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] extra fields in rewriteEngine" @@ -84,8 +85,10 @@ rewrite_parse( if ( strcasecmp( argv[ 1 ], "on" ) == 0 ) { info->li_state = REWRITE_ON; + } else if ( strcasecmp( argv[ 1 ], "off" ) == 0 ) { info->li_state = REWRITE_OFF; + } else { Debug( LDAP_DEBUG_ANY, "[%s:%d] unknown 'state' in rewriteEngine;" @@ -151,6 +154,7 @@ rewrite_parse( " 'alias'\n%s", fname, lineno, "" ); return -1; + } else if ( argc > 4 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] extra fields in" @@ -175,6 +179,7 @@ rewrite_parse( rewrite_int_curr_context->lc_alias = aliased; rewrite_int_curr_context = aliased; + } else { Debug( LDAP_DEBUG_ANY, "[%s:%d] extra fields" @@ -195,6 +200,7 @@ rewrite_parse( " 'subst' ['flags']\n%s", fname, lineno, "" ); return -1; + } else if ( argc > 4 ) { Debug( LDAP_DEBUG_ANY, "[%s:%d] extra fields in rewriteRule" diff --git a/libraries/librewrite/context.c b/libraries/librewrite/context.c index 4758c8fff7..e77b485165 100644 --- a/libraries/librewrite/context.c +++ b/libraries/librewrite/context.c @@ -251,7 +251,7 @@ rewrite_context_apply( case REWRITE_REGEXEC_ERR: Debug( LDAP_DEBUG_ANY, "==> rewrite_context_apply" - " error ...\n%s%s%s", "", "", ""); + " error ...\n", 0, 0, 0); /* * Checks for special actions to be taken @@ -273,8 +273,7 @@ rewrite_context_apply( case REWRITE_ACTION_IGNORE_ERR: Debug( LDAP_DEBUG_ANY, "==> rewrite_context_apply" - " ignoring error ...\n%s%s%s", - "", "", "" ); + " ignoring error ...\n", 0, 0, 0 ); do_continue = 1; break; diff --git a/libraries/librewrite/info.c b/libraries/librewrite/info.c index 723cdbc1ec..fbc2fc67ba 100644 --- a/libraries/librewrite/info.c +++ b/libraries/librewrite/info.c @@ -210,13 +210,16 @@ rewrite_session( case REWRITE_MODE_ERR: rc = REWRITE_REGEXEC_ERR; goto rc_return; + case REWRITE_MODE_OK: rc = REWRITE_REGEXEC_OK; goto rc_return; + case REWRITE_MODE_COPY_INPUT: *result = strdup( string ); rc = REWRITE_REGEXEC_OK; goto rc_return; + case REWRITE_MODE_USE_DEFAULT: context = rewrite_context_find( info, REWRITE_DEFAULT_CONTEXT ); diff --git a/libraries/librewrite/map.c b/libraries/librewrite/map.c index cf563f079c..d17d814629 100644 --- a/libraries/librewrite/map.c +++ b/libraries/librewrite/map.c @@ -455,6 +455,7 @@ rewrite_builtin_map_free( break; } + free( map->lb_name ); free( map ); } diff --git a/libraries/librewrite/params.c b/libraries/librewrite/params.c index d86f3c32a2..3f5488d69e 100644 --- a/libraries/librewrite/params.c +++ b/libraries/librewrite/params.c @@ -60,7 +60,7 @@ rewrite_param_set( #endif /* USE_REWRITE_LDAP_PVT_THREADS */ return REWRITE_ERR; } - } + } #ifdef USE_REWRITE_LDAP_PVT_THREADS ldap_pvt_thread_rdwr_wunlock( &info->li_params_mutex ); @@ -117,9 +117,15 @@ rewrite_param_free( void *tmp ) { - char *value = ( char * )tmp; + struct rewrite_var *var = ( struct rewrite_var * )tmp; + assert( var != NULL ); + + assert( var->lv_name != NULL ); + assert( var->lv_value.bv_val != NULL ); - free( value ); + free( var->lv_name ); + free( var->lv_value.bv_val ); + free( var ); } /* diff --git a/libraries/librewrite/rewrite.c b/libraries/librewrite/rewrite.c index b40ca26ce8..f02f6983cf 100644 --- a/libraries/librewrite/rewrite.c +++ b/libraries/librewrite/rewrite.c @@ -132,7 +132,7 @@ main( int argc, char *argv[] ) exit( EXIT_SUCCESS ); case 'r': - rewriteContext = strdup( optarg ); + rewriteContext = optarg; break; } } diff --git a/libraries/librewrite/rule.c b/libraries/librewrite/rule.c index c812cf3fa9..c025000183 100644 --- a/libraries/librewrite/rule.c +++ b/libraries/librewrite/rule.c @@ -70,6 +70,39 @@ append_action( return REWRITE_SUCCESS; } +static int +destroy_action( + struct rewrite_action **paction +) +{ + struct rewrite_action *action; + + assert( paction ); + assert( *paction ); + + action = *paction; + + /* do something */ + switch ( action->la_type ) { + case REWRITE_FLAG_GOTO: { + int *pi = (int *)action->la_args; + + if ( pi ) { + free( pi ); + } + break; + } + + default: + break; + } + + free( action ); + *paction = NULL; + + return 0; +} + /* * In case of error it returns NULL and does not free all the memory * it allocated; as this is a once only phase, and an error at this stage @@ -402,6 +435,7 @@ rewrite_rule_destroy( ) { struct rewrite_rule *rule; + struct rewrite_action *action; assert( prule ); assert( *prule ); @@ -429,6 +463,13 @@ rewrite_rule_destroy( regfree( &rule->lr_regex ); + for ( action = rule->lr_action; action; ) { + struct rewrite_action *curraction = action; + + action = action->la_next; + destroy_action( &curraction ); + } + free( rule ); *prule = NULL; diff --git a/libraries/librewrite/subst.c b/libraries/librewrite/subst.c index 531b09900b..9c7421aedb 100644 --- a/libraries/librewrite/subst.c +++ b/libraries/librewrite/subst.c @@ -51,7 +51,7 @@ rewrite_subst_compile( * Take care of substitution string */ for ( p = begin = result, subs_len = 0; p[ 0 ] != '\0'; p++ ) { - + /* * Keep only single escapes '%' */ @@ -65,7 +65,7 @@ rewrite_subst_compile( continue; } - tmps = (struct berval *)realloc( subs, + tmps = ( struct berval * )realloc( subs, sizeof( struct berval )*( nsub + 1 ) ); if ( tmps == NULL ) { /* FIXME: cleanup */ @@ -96,14 +96,15 @@ rewrite_subst_compile( * Substitution pattern */ if ( isdigit( (unsigned char) p[ 1 ] ) ) { - int d = p[ 1 ] - '0'; struct rewrite_submatch *tmpsm; + int d = p[ 1 ] - '0'; /* * Add a new value substitution scheme */ - tmpsm = realloc( submatch, - sizeof( struct rewrite_submatch )*( nsub + 1 ) ); + + tmpsm = ( struct rewrite_submatch * )realloc( submatch, + sizeof( struct rewrite_submatch )*( nsub + 1 ) ); if ( tmpsm == NULL ) { /* cleanup */ return NULL; @@ -118,7 +119,9 @@ rewrite_subst_compile( if ( p[ 2 ] != '{' ) { submatch[ nsub ].ls_type = REWRITE_SUBMATCH_ASIS; + submatch[ nsub ].ls_map = NULL; begin = ++p + 1; + } else { struct rewrite_map *map; @@ -131,9 +134,8 @@ rewrite_subst_compile( /* cleanup */ return NULL; } - p = begin - 1; - submatch[ nsub ].ls_map = map; + p = begin - 1; } /* @@ -153,7 +155,7 @@ rewrite_subst_compile( /* * Add a new value substitution scheme */ - tmpsm = realloc( submatch, + tmpsm = ( struct rewrite_submatch * )realloc( submatch, sizeof( struct rewrite_submatch )*( nsub + 1 ) ); if ( tmpsm == NULL ) { /* cleanup */ @@ -162,7 +164,6 @@ rewrite_subst_compile( submatch = tmpsm; submatch[ nsub ].ls_type = REWRITE_SUBMATCH_MAP_W_ARG; - submatch[ nsub ].ls_map = map; } @@ -172,12 +173,12 @@ rewrite_subst_compile( /* * Last part of string */ - tmps = realloc( subs, sizeof( struct berval )*( nsub + 1 ) ); + tmps = (struct berval * )realloc( subs, sizeof( struct berval )*( nsub + 1 ) ); if ( tmps == NULL ) { /* * XXX need to free the value subst stuff! */ - free( submatch ); + free( subs ); return NULL; } subs = tmps; @@ -235,7 +236,7 @@ submatch_copy( l = match[ c ].rm_eo - match[ c ].rm_so; val->bv_len = l; - val->bv_val = calloc( sizeof( char ), l + 1 ); + val->bv_val = malloc( l + 1 ); if ( val->bv_val == NULL ) { return REWRITE_ERR; } @@ -354,12 +355,16 @@ rewrite_subst_apply( rc = rewrite_xmap_apply( info, op, subst->lt_submatch[ n ].ls_map, &key, &submatch[ n ] ); + free( key.bv_val ); + key.bv_val = NULL; break; case REWRITE_SUBMATCH_MAP_W_ARG: rc = rewrite_map_apply( info, op, subst->lt_submatch[ n ].ls_map, &key, &submatch[ n ] ); + free( key.bv_val ); + key.bv_val = NULL; break; default: @@ -388,7 +393,7 @@ rewrite_subst_apply( * of the subst pattern and initialize it */ l += subst->lt_subs_len; - res = calloc( sizeof( char ), l + 1 ); + res = malloc( l + 1 ); if ( res == NULL ) { rc = REWRITE_REGEXEC_ERR; goto cleanup; @@ -410,7 +415,9 @@ rewrite_subst_apply( if ( subst->lt_subs[ n ].bv_val != NULL ) { AC_MEMCPY( res + cl, subst->lt_subs[ n ].bv_val, subst->lt_subs[ n ].bv_len ); + cl += subst->lt_subs[ n ].bv_len; } + res[ cl ] = '\0'; val->bv_val = res; val->bv_len = l; @@ -467,12 +474,18 @@ rewrite_subst_destroy( } } + free( subst->lt_submatch ); + subst->lt_submatch = NULL; + /* last one */ if ( subst->lt_subs[ n ].bv_val ) { free( subst->lt_subs[ n ].bv_val ); subst->lt_subs[ n ].bv_val = NULL; } + free( subst->lt_subs ); + subst->lt_subs = NULL; + free( subst ); *psubst = NULL; diff --git a/libraries/librewrite/xmap.c b/libraries/librewrite/xmap.c index 195bee0be7..3ec9873b2d 100644 --- a/libraries/librewrite/xmap.c +++ b/libraries/librewrite/xmap.c @@ -494,6 +494,7 @@ rewrite_xmap_destroy( } + free( map->lm_name ); free( map ); *pmap = NULL; -- 2.39.5