]> git.sur5r.net Git - openldap/blobdiff - libraries/librewrite/subst.c
Fix up abandon merge. Hallvard will holler if I get this wrong. :-)
[openldap] / libraries / librewrite / subst.c
index 2f0645f6d4c790c2b17f3fa1b7275f48d7761865..4c0bc411e0fa627c0d77084e2a116da33c635f06 100644 (file)
@@ -36,7 +36,7 @@ rewrite_subst_compile(
 )
 {
        size_t subs_len;
-       struct berval **subs = NULL;
+       struct berval **subs = NULL, **tmps;
        struct rewrite_submatch **submatch = NULL;
 
        struct rewrite_subst *s = NULL;
@@ -53,22 +53,25 @@ rewrite_subst_compile(
        for ( p = begin = result, subs_len = 0; p[ 0 ] != '\0'; p++ ) {
                
                /*
-                * Keep only single escapes '\'
+                * Keep only single escapes '%'
                 */
                if ( p[ 0 ] != REWRITE_SUBMATCH_ESCAPE ) {
                        continue;
-               } else if ( p[ 1 ] == REWRITE_SUBMATCH_ESCAPE ) {
+               } 
+               if ( p[ 1 ] == REWRITE_SUBMATCH_ESCAPE ) {
+                       AC_MEMCPY((char *)p, p + 1, strlen( p ) );
                        continue;
                }
 
                nsub++;
                
-               subs = (struct berval **)realloc( subs,
+               tmps = (struct berval **)realloc( subs,
                                sizeof( struct berval * )*( nsub + 1 ) );
-               if ( subs == NULL ) {
+               if ( tmps == NULL ) {
                        /* cleanup */
                        return NULL;
                }
+               subs = tmps;
                subs[ nsub ] = NULL;
                
                /*
@@ -89,7 +92,7 @@ rewrite_subst_compile(
                        if ( subs[ nsub - 1 ]->bv_val == NULL ) {
                                return NULL;
                        }
-                       strncpy( subs[ nsub - 1 ]->bv_val, begin, l );
+                       AC_MEMCPY( subs[ nsub - 1 ]->bv_val, begin, l );
                        subs[ nsub - 1 ]->bv_val[ l ] = '\0';
                } else {
                        subs[ nsub - 1 ] = NULL;
@@ -98,19 +101,22 @@ rewrite_subst_compile(
                /*
                 * Substitution pattern
                 */
-               if ( isdigit( p[ 1 ] ) ) {
+               if ( isdigit( (unsigned char) p[ 1 ] ) ) {
                        int d = p[ 1 ] - '0';
+                       struct rewrite_submatch **tmpsm;
 
                        /*
                         * Add a new value substitution scheme
                         */
-                       submatch = realloc( submatch, 
+                       tmpsm = realloc( submatch, 
        sizeof( struct rewrite_submatch * )*( nsub + 1 ) );
-                       if ( submatch == NULL ) {
+                       if ( tmpsm == NULL ) {
                                /* cleanup */
                                return NULL;
                        }
+                       submatch = tmpsm;
                        submatch[ nsub ] = NULL;
+                       
                        submatch[ nsub - 1 ] = 
        calloc( sizeof(  struct rewrite_submatch ), 1 );
                        if ( submatch[ nsub - 1 ] == NULL ) {
@@ -149,6 +155,7 @@ rewrite_subst_compile(
                 */
                } else if ( p[ 1 ] == '{' ) {
                        struct rewrite_map *map;
+                       struct rewrite_submatch **tmpsm;
 
                        map = rewrite_map_parse( info, p + 2, &begin );
                        if ( map == NULL ) {
@@ -160,12 +167,13 @@ rewrite_subst_compile(
                        /*
                         * Add a new value substitution scheme
                         */
-                       submatch = realloc( submatch,
+                       tmpsm = realloc( submatch,
                                        sizeof( struct rewrite_submatch * )*( nsub + 1 ) );
-                       if ( submatch == NULL ) {
+                       if ( tmpsm == NULL ) {
                                /* cleanup */
                                return NULL;
                        }
+                       submatch = tmpsm;
                        submatch[ nsub ] = NULL;
                        submatch[ nsub - 1 ] =
                                calloc( sizeof(  struct rewrite_submatch ), 1 );
@@ -184,15 +192,16 @@ rewrite_subst_compile(
        /*
         * Last part of string
         */
-       subs = realloc( subs, sizeof( struct berval *)*( nsub + 2 ) );
-       if ( subs == NULL ) {
+       tmps = realloc( subs, sizeof( struct berval *)*( nsub + 2 ) );
+       if ( tmps == NULL ) {
                /*
                 * XXX need to free the value subst stuff!
                 */
                free( submatch );
                return NULL;
        }
-       
+
+       subs = tmps;
        subs[ nsub + 1 ] = NULL;
        l = p - begin;
        if ( l > 0 ) {
@@ -200,7 +209,7 @@ rewrite_subst_compile(
                subs_len += l;
                subs[ nsub ]->bv_len = l;
                subs[ nsub ]->bv_val = malloc( l + 1 );
-               strncpy( subs[ nsub ]->bv_val, begin, l );
+               AC_MEMCPY( subs[ nsub ]->bv_val, begin, l );
                subs[ nsub ]->bv_val[ l ] = '\0';
        } else {
                subs[ nsub ] = NULL;
@@ -253,7 +262,7 @@ submatch_copy(
                return REWRITE_ERR;
        }
        
-       strncpy( val->bv_val, s, l );
+       AC_MEMCPY( val->bv_val, s, l );
        val->bv_val[ l ] = '\0';
        
        return REWRITE_SUCCESS;
@@ -408,15 +417,18 @@ rewrite_subst_apply(
         */
         for ( n = 0, cl = 0; n < subst->lt_num_submatch; n++ ) {
                if ( subst->lt_subs[ n ] != NULL ) {
-                       strcpy( res + cl, subst->lt_subs[ n ]->bv_val);
+                       AC_MEMCPY( res + cl, subst->lt_subs[ n ]->bv_val,
+                                       subst->lt_subs[ n ]->bv_len );
                        cl += subst->lt_subs[ n ]->bv_len;
                }
-               strcpy( res + cl, submatch[ n ].bv_val );
+               AC_MEMCPY( res + cl, submatch[ n ].bv_val, 
+                               submatch[ n ].bv_len );
                cl += submatch[ n ].bv_len;
                free( submatch[ n ].bv_val );
        }
        if ( subst->lt_subs[ n ] != NULL ) {
-               strcpy( res + cl, subst->lt_subs[ n ]->bv_val );
+               AC_MEMCPY( res + cl, subst->lt_subs[ n ]->bv_val,
+                               subst->lt_subs[ n ]->bv_len );
        }
 
        val->bv_val = res;