]> git.sur5r.net Git - openldap/commitdiff
more memleaks fixed; some cleanup
authorPierangelo Masarati <ando@openldap.org>
Sat, 15 Nov 2003 10:02:44 +0000 (10:02 +0000)
committerPierangelo Masarati <ando@openldap.org>
Sat, 15 Nov 2003 10:02:44 +0000 (10:02 +0000)
libraries/librewrite/config.c
libraries/librewrite/context.c
libraries/librewrite/info.c
libraries/librewrite/map.c
libraries/librewrite/params.c
libraries/librewrite/rewrite.c
libraries/librewrite/rule.c
libraries/librewrite/subst.c
libraries/librewrite/xmap.c

index baf88d40a740726cad766f4a823ae091bc2aa57f..514b16c415c89e84bfa6e7e1f41160894cdb2d90 100644 (file)
@@ -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"
index 4758c8fff7b0ebb07c503a4a473bc1cd3b89a100..e77b485165661996d737b079a6cbcef7bed74753 100644 (file)
@@ -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;
 
index 723cdbc1ecb90f8cf02a4c8bc0da9509f4fc0ff3..fbc2fc67ba2357f08607afa3e1bb563737256dbe 100644 (file)
@@ -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 );
index cf563f079c6542e5b17f0cdd47b66289f195eb9f..d17d8146299bf7e74b1d34454151b3596a164848 100644 (file)
@@ -455,6 +455,7 @@ rewrite_builtin_map_free(
                break;
        }
 
+       free( map->lb_name );
        free( map );
 }
 
index d86f3c32a2b66ba74535edadfb4e03cff657e24e..3f5488d69ed36ad036faab2263d78a284047aa06 100644 (file)
@@ -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 );
 }
 
 /*
index b40ca26ce8fe8cf400b6dd4cf580a578e2912746..f02f6983cf7d7d601fbda5a6b996648fcd2e3adb 100644 (file)
@@ -132,7 +132,7 @@ main( int argc, char *argv[] )
                        exit( EXIT_SUCCESS );
                        
                case 'r':
-                       rewriteContext = strdup( optarg );
+                       rewriteContext = optarg;
                        break;
                }
        }
index c812cf3fa983297e041ae7c2acfcc5e65bf4b81b..c025000183e462da46282bfc2a325027cc001091 100644 (file)
@@ -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;
 
index 531b09900b6dbe05af95711cffcf8c867cc00d3f..9c7421aedb671073f0cdf3a3643e998695690550 100644 (file)
@@ -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;
 
index 195bee0be7ebc085397eb6ea41296a7b0d5e829d..3ec9873b2dbf34450bd3c57fdbd3ef6a65b81047 100644 (file)
@@ -494,6 +494,7 @@ rewrite_xmap_destroy(
 
        }
 
+       free( map->lm_name );
        free( map );
        *pmap = NULL;