]> git.sur5r.net Git - openldap/commitdiff
Use ber_flatten2 to avoid excess mallocs
authorHoward Chu <hyc@openldap.org>
Wed, 18 Dec 2002 19:16:31 +0000 (19:16 +0000)
committerHoward Chu <hyc@openldap.org>
Wed, 18 Dec 2002 19:16:31 +0000 (19:16 +0000)
clients/tools/ldappasswd.c
clients/tools/ldapsearch.c
libraries/libldap/controls.c
libraries/libldap/ftest.c
libraries/libldap/passwd.c

index e3f9c187e113868d7ef934bcb9563f10cf001a0a..63bae54d04ef39f171d53d23b6d23708b80cdf28 100644 (file)
@@ -126,7 +126,8 @@ main( int argc, char *argv[] )
        char    *user = NULL;
 
        LDAP           *ld = NULL;
-       struct berval *bv = NULL;
+       struct berval bv = {0};
+       BerElement  *ber = NULL;
 
        int id, code = LDAP_OTHER;
        LDAPMessage *res;
@@ -134,7 +135,7 @@ main( int argc, char *argv[] )
        char    *retoid = NULL;
        struct berval *retdata = NULL;
 
-    prog = lutil_progname( "ldappasswd", argc, argv );
+       prog = lutil_progname( "ldappasswd", argc, argv );
 
        /* LDAPv3 only */
        version = LDAP_VERSION3;
@@ -192,7 +193,7 @@ main( int argc, char *argv[] )
 
        if( user != NULL || oldpw != NULL || newpw != NULL ) {
                /* build change password control */
-               BerElement *ber = ber_alloc_t( LBER_USE_DER );
+               ber = ber_alloc_t( LBER_USE_DER );
 
                if( ber == NULL ) {
                        perror( "ber_alloc_t" );
@@ -222,15 +223,13 @@ main( int argc, char *argv[] )
 
                ber_printf( ber, /*{*/ "N}" );
 
-               rc = ber_flatten( ber, &bv );
+               rc = ber_flatten2( ber, &bv, 0 );
 
                if( rc < 0 ) {
-                       perror( "ber_flatten" );
+                       perror( "ber_flatten2" );
                        ldap_unbind( ld );
                        return EXIT_FAILURE;
                }
-
-               ber_free( ber, 1 );
        }
 
        if ( not ) {
@@ -239,10 +238,10 @@ main( int argc, char *argv[] )
        }
 
        rc = ldap_extended_operation( ld,
-               LDAP_EXOP_MODIFY_PASSWD, bv, 
+               LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL
                NULL, NULL, &id );
 
-       ber_bvfree( bv );
+       ber_free( ber, 1 );
 
        if( rc != LDAP_SUCCESS ) {
                ldap_perror( ld, "ldap_extended_operation" );
@@ -273,7 +272,7 @@ main( int argc, char *argv[] )
        if( retdata != NULL ) {
                ber_tag_t tag;
                char *s;
-               BerElement *ber = ber_init( retdata );
+               ber = ber_init( retdata );
 
                if( ber == NULL ) {
                        perror( "ber_init" );
index a6893c572193462a6f1435bdb92402b15edc1bc9..7d89585a215349404c6c7606bcb06750f7a2e1e4 100644 (file)
@@ -361,9 +361,7 @@ main( int argc, char **argv )
        FILE            *fp = NULL;
        int                     rc, i, first;
        LDAP            *ld = NULL;
-       BerElement      *ber = NULL;
-       struct berval   *sebvalp = NULL, *vrbvalp = NULL;
-       struct berval   *prbvalp = NULL;
+       BerElement      *seber = NULL, *vrber = NULL, *prber = NULL;
 
        npagedresponses = npagedentries = npagedreferences =
                npagedextended = npagedpartial = 0;
@@ -466,74 +464,67 @@ getNextPage:
 
 #ifdef LDAP_CONTROL_SUBENTRIES
                if ( subentries ) {
-               if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
+               if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
                                return EXIT_FAILURE;
                        }
 
-                       err = ber_printf( ber, "{b}", abs(subentries) == 1 ? 0 : 1 );
+                       err = ber_printf( seber, "{b}", abs(subentries) == 1 ? 0 : 1 );
                if ( err == LBER_ERROR ) {
-                               ber_free( ber, 1 );
+                               ber_free( seber, 1 );
                                fprintf( stderr, "Subentries control encoding error!\n" );
                                return EXIT_FAILURE;
                        }
 
-                       if ( ber_flatten( ber, &sebvalp ) == LBER_ERROR ) {
+                       if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == LBER_ERROR ) {
                                return EXIT_FAILURE;
                        }
 
                        c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
-                       c[i].ldctl_value=(*sebvalp);
                        c[i].ldctl_iscritical = subentries < 1;
                        i++;
                }
 #endif
 
                if ( valuesReturnFilter ) {
-               if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
+               if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
                                return EXIT_FAILURE;
                        }
 
-               if ( ( err = ldap_put_vrFilter( ber, vrFilter ) ) == -1 ) {
-                               ber_free( ber, 1 );
+               if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
+                               ber_free( vrber, 1 );
                                fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
                                return EXIT_FAILURE;
                        }
 
-                       if ( ber_flatten( ber, &vrbvalp ) == LBER_ERROR ) {
+                       if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == LBER_ERROR ) {
                                return EXIT_FAILURE;
                        }
 
-                       ber_free( ber, 1 );
-
                        c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
-                       c[i].ldctl_value=(*vrbvalp);
                        c[i].ldctl_iscritical = valuesReturnFilter > 1;
                        i++;
                }
 
                if ( pagedResults ) {
-                       if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
+                       if (( prber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
                                return EXIT_FAILURE;
                        }
 
-                       ber_printf( ber, "{iO}", pageSize, &cookie );
-                       if ( ber_flatten( ber, &prbvalp ) == LBER_ERROR ) {
+                       ber_printf( prber, "{iO}", pageSize, &cookie );
+                       if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == LBER_ERROR ) {
                                return EXIT_FAILURE;
                        }
                        
-                       ber_free( ber, 1 );
-
                        c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
-                       c[i].ldctl_value=(*prbvalp);
                        c[i].ldctl_iscritical = pagedResults > 1;
                        i++;
                }
 
                tool_server_controls( ld, c, i );
 
-               ber_bvfree( sebvalp );
-               ber_bvfree( vrbvalp );
-               ber_bvfree( prbvalp );
+               ber_free( seber, 1 );
+               ber_free( vrber, 1 );
+               ber_free( prber, 1 );
        }
        
        if ( verbose ) {
index 6701defb9bd8a45abbe88e10a4494aa90af01e5a..a49a76f8b4c6d49c85d5feb6b5c2de9d0cfe7d67 100644 (file)
@@ -410,7 +410,6 @@ ldap_create_control(
        LDAPControl **ctrlp )
 {
        LDAPControl *ctrl;
-       struct berval *bvalp;
 
        assert( requestOID != NULL );
        assert( ber != NULL );
@@ -421,14 +420,11 @@ ldap_create_control(
                return LDAP_NO_MEMORY;
        }
 
-       if ( ber_flatten( ber, &bvalp ) == -1 ) {
+       if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
                LDAP_FREE( ctrl );
                return LDAP_NO_MEMORY;
        }
 
-       ctrl->ldctl_value = *bvalp;
-       ber_memfree( bvalp );
-
        ctrl->ldctl_oid = LDAP_STRDUP( requestOID );
        ctrl->ldctl_iscritical = iscritical;
 
index f61298992ed4e7b372d38f2e0aa0ade4dc8a2e83..695fb2919338d285e20e24419c5079ebcc90b654 100644 (file)
@@ -76,7 +76,7 @@ main( int argc, char *argv[] )
 static int filter2ber( char *filter )
 {
        int rc;
-       struct berval *bv = NULL;
+       struct berval bv = {0};
        BerElement *ber;
 
        printf( "Filter: %s\n", filter );
@@ -93,17 +93,16 @@ static int filter2ber( char *filter )
                return EXIT_FAILURE;
        }
 
-       rc = ber_flatten( ber, &bv );
+       rc = ber_flatten2( ber, &bv, 0 );
        if( rc < 0 ) {
-               perror( "ber_flatten" );
+               perror( "ber_flatten2" );
                return EXIT_FAILURE;
        }
 
-       printf( "BER encoding (len=%ld):\n", (long) bv->bv_len );
-       ber_bprint( bv->bv_val, bv->bv_len );
+       printf( "BER encoding (len=%ld):\n", (long) bv.bv_len );
+       ber_bprint( bv.bv_val, bv.bv_len );
 
-       ber_free( ber, 0 );
-       ber_bvfree( bv );
+       ber_free( ber, 1 );
 
        return EXIT_SUCCESS;
 }
index a186abe0402c6c35c385c057b1a73fd1d12a6f75..458e0efdb619ddfe8917df082c096b3124b0464d 100644 (file)
@@ -71,7 +71,8 @@ ldap_passwd( LDAP *ld,
        int                             *msgidp )
 {
        int rc;
-       struct berval *bv = NULL;
+       struct berval bv = {0};
+       BerElement *ber = NULL;
 
        assert( ld != NULL );
        assert( LDAP_VALID( ld ) );
@@ -79,7 +80,7 @@ ldap_passwd( LDAP *ld,
 
        if( user != NULL || oldpw != NULL || newpw != NULL ) {
                /* build change password control */
-               BerElement *ber = ber_alloc_t( LBER_USE_DER );
+               ber = ber_alloc_t( LBER_USE_DER );
 
                if( ber == NULL ) {
                        ld->ld_errno = LDAP_NO_MEMORY;
@@ -105,9 +106,7 @@ ldap_passwd( LDAP *ld,
 
                ber_printf( ber, /*{*/ "N}" );
 
-               rc = ber_flatten( ber, &bv );
-
-               ber_free( ber, 1 );
+               rc = ber_flatten2( ber, &bv, 0 );
 
                if( rc < 0 ) {
                        ld->ld_errno = LDAP_ENCODING_ERROR;
@@ -117,7 +116,9 @@ ldap_passwd( LDAP *ld,
        }
        
        rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD,
-               bv, sctrls, cctrls, msgidp );
+               bv.bv_val ? &bv : NULL, sctrls, cctrls, msgidp );
+
+       ber_free( ber, 1 );
 
        return rc;
 }