Use ber_flatten2 to avoid excess mallocs

This commit is contained in:
Howard Chu 2002-12-18 19:16:31 +00:00
parent 6a37f1ae80
commit c102809fab
5 changed files with 38 additions and 52 deletions

View File

@ -126,7 +126,8 @@ main( int argc, char *argv[] )
char *user = NULL; char *user = NULL;
LDAP *ld = NULL; LDAP *ld = NULL;
struct berval *bv = NULL; struct berval bv = {0};
BerElement *ber = NULL;
int id, code = LDAP_OTHER; int id, code = LDAP_OTHER;
LDAPMessage *res; LDAPMessage *res;
@ -134,7 +135,7 @@ main( int argc, char *argv[] )
char *retoid = NULL; char *retoid = NULL;
struct berval *retdata = NULL; struct berval *retdata = NULL;
prog = lutil_progname( "ldappasswd", argc, argv ); prog = lutil_progname( "ldappasswd", argc, argv );
/* LDAPv3 only */ /* LDAPv3 only */
version = LDAP_VERSION3; version = LDAP_VERSION3;
@ -192,7 +193,7 @@ main( int argc, char *argv[] )
if( user != NULL || oldpw != NULL || newpw != NULL ) { if( user != NULL || oldpw != NULL || newpw != NULL ) {
/* build change password control */ /* build change password control */
BerElement *ber = ber_alloc_t( LBER_USE_DER ); ber = ber_alloc_t( LBER_USE_DER );
if( ber == NULL ) { if( ber == NULL ) {
perror( "ber_alloc_t" ); perror( "ber_alloc_t" );
@ -222,15 +223,13 @@ main( int argc, char *argv[] )
ber_printf( ber, /*{*/ "N}" ); ber_printf( ber, /*{*/ "N}" );
rc = ber_flatten( ber, &bv ); rc = ber_flatten2( ber, &bv, 0 );
if( rc < 0 ) { if( rc < 0 ) {
perror( "ber_flatten" ); perror( "ber_flatten2" );
ldap_unbind( ld ); ldap_unbind( ld );
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ber_free( ber, 1 );
} }
if ( not ) { if ( not ) {
@ -239,10 +238,10 @@ main( int argc, char *argv[] )
} }
rc = ldap_extended_operation( ld, rc = ldap_extended_operation( ld,
LDAP_EXOP_MODIFY_PASSWD, bv, LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL,
NULL, NULL, &id ); NULL, NULL, &id );
ber_bvfree( bv ); ber_free( ber, 1 );
if( rc != LDAP_SUCCESS ) { if( rc != LDAP_SUCCESS ) {
ldap_perror( ld, "ldap_extended_operation" ); ldap_perror( ld, "ldap_extended_operation" );
@ -273,7 +272,7 @@ main( int argc, char *argv[] )
if( retdata != NULL ) { if( retdata != NULL ) {
ber_tag_t tag; ber_tag_t tag;
char *s; char *s;
BerElement *ber = ber_init( retdata ); ber = ber_init( retdata );
if( ber == NULL ) { if( ber == NULL ) {
perror( "ber_init" ); perror( "ber_init" );

View File

@ -361,9 +361,7 @@ main( int argc, char **argv )
FILE *fp = NULL; FILE *fp = NULL;
int rc, i, first; int rc, i, first;
LDAP *ld = NULL; LDAP *ld = NULL;
BerElement *ber = NULL; BerElement *seber = NULL, *vrber = NULL, *prber = NULL;
struct berval *sebvalp = NULL, *vrbvalp = NULL;
struct berval *prbvalp = NULL;
npagedresponses = npagedentries = npagedreferences = npagedresponses = npagedentries = npagedreferences =
npagedextended = npagedpartial = 0; npagedextended = npagedpartial = 0;
@ -466,74 +464,67 @@ getNextPage:
#ifdef LDAP_CONTROL_SUBENTRIES #ifdef LDAP_CONTROL_SUBENTRIES
if ( subentries ) { if ( subentries ) {
if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) { if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
return EXIT_FAILURE; 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 ) { if ( err == LBER_ERROR ) {
ber_free( ber, 1 ); ber_free( seber, 1 );
fprintf( stderr, "Subentries control encoding error!\n" ); fprintf( stderr, "Subentries control encoding error!\n" );
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if ( ber_flatten( ber, &sebvalp ) == LBER_ERROR ) { if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == LBER_ERROR ) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES; c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
c[i].ldctl_value=(*sebvalp);
c[i].ldctl_iscritical = subentries < 1; c[i].ldctl_iscritical = subentries < 1;
i++; i++;
} }
#endif #endif
if ( valuesReturnFilter ) { if ( valuesReturnFilter ) {
if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) { if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if ( ( err = ldap_put_vrFilter( ber, vrFilter ) ) == -1 ) { if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
ber_free( ber, 1 ); ber_free( vrber, 1 );
fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter ); fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if ( ber_flatten( ber, &vrbvalp ) == LBER_ERROR ) { if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == LBER_ERROR ) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ber_free( ber, 1 );
c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER; c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
c[i].ldctl_value=(*vrbvalp);
c[i].ldctl_iscritical = valuesReturnFilter > 1; c[i].ldctl_iscritical = valuesReturnFilter > 1;
i++; i++;
} }
if ( pagedResults ) { if ( pagedResults ) {
if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) { if (( prber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ber_printf( ber, "{iO}", pageSize, &cookie ); ber_printf( prber, "{iO}", pageSize, &cookie );
if ( ber_flatten( ber, &prbvalp ) == LBER_ERROR ) { if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == LBER_ERROR ) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ber_free( ber, 1 );
c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS; c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
c[i].ldctl_value=(*prbvalp);
c[i].ldctl_iscritical = pagedResults > 1; c[i].ldctl_iscritical = pagedResults > 1;
i++; i++;
} }
tool_server_controls( ld, c, i ); tool_server_controls( ld, c, i );
ber_bvfree( sebvalp ); ber_free( seber, 1 );
ber_bvfree( vrbvalp ); ber_free( vrber, 1 );
ber_bvfree( prbvalp ); ber_free( prber, 1 );
} }
if ( verbose ) { if ( verbose ) {

View File

@ -410,7 +410,6 @@ ldap_create_control(
LDAPControl **ctrlp ) LDAPControl **ctrlp )
{ {
LDAPControl *ctrl; LDAPControl *ctrl;
struct berval *bvalp;
assert( requestOID != NULL ); assert( requestOID != NULL );
assert( ber != NULL ); assert( ber != NULL );
@ -421,14 +420,11 @@ ldap_create_control(
return LDAP_NO_MEMORY; return LDAP_NO_MEMORY;
} }
if ( ber_flatten( ber, &bvalp ) == -1 ) { if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
LDAP_FREE( ctrl ); LDAP_FREE( ctrl );
return LDAP_NO_MEMORY; return LDAP_NO_MEMORY;
} }
ctrl->ldctl_value = *bvalp;
ber_memfree( bvalp );
ctrl->ldctl_oid = LDAP_STRDUP( requestOID ); ctrl->ldctl_oid = LDAP_STRDUP( requestOID );
ctrl->ldctl_iscritical = iscritical; ctrl->ldctl_iscritical = iscritical;

View File

@ -76,7 +76,7 @@ main( int argc, char *argv[] )
static int filter2ber( char *filter ) static int filter2ber( char *filter )
{ {
int rc; int rc;
struct berval *bv = NULL; struct berval bv = {0};
BerElement *ber; BerElement *ber;
printf( "Filter: %s\n", filter ); printf( "Filter: %s\n", filter );
@ -93,17 +93,16 @@ static int filter2ber( char *filter )
return EXIT_FAILURE; return EXIT_FAILURE;
} }
rc = ber_flatten( ber, &bv ); rc = ber_flatten2( ber, &bv, 0 );
if( rc < 0 ) { if( rc < 0 ) {
perror( "ber_flatten" ); perror( "ber_flatten2" );
return EXIT_FAILURE; return EXIT_FAILURE;
} }
printf( "BER encoding (len=%ld):\n", (long) bv->bv_len ); printf( "BER encoding (len=%ld):\n", (long) bv.bv_len );
ber_bprint( bv->bv_val, bv->bv_len ); ber_bprint( bv.bv_val, bv.bv_len );
ber_free( ber, 0 ); ber_free( ber, 1 );
ber_bvfree( bv );
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -71,7 +71,8 @@ ldap_passwd( LDAP *ld,
int *msgidp ) int *msgidp )
{ {
int rc; int rc;
struct berval *bv = NULL; struct berval bv = {0};
BerElement *ber = NULL;
assert( ld != NULL ); assert( ld != NULL );
assert( LDAP_VALID( ld ) ); assert( LDAP_VALID( ld ) );
@ -79,7 +80,7 @@ ldap_passwd( LDAP *ld,
if( user != NULL || oldpw != NULL || newpw != NULL ) { if( user != NULL || oldpw != NULL || newpw != NULL ) {
/* build change password control */ /* build change password control */
BerElement *ber = ber_alloc_t( LBER_USE_DER ); ber = ber_alloc_t( LBER_USE_DER );
if( ber == NULL ) { if( ber == NULL ) {
ld->ld_errno = LDAP_NO_MEMORY; ld->ld_errno = LDAP_NO_MEMORY;
@ -105,9 +106,7 @@ ldap_passwd( LDAP *ld,
ber_printf( ber, /*{*/ "N}" ); ber_printf( ber, /*{*/ "N}" );
rc = ber_flatten( ber, &bv ); rc = ber_flatten2( ber, &bv, 0 );
ber_free( ber, 1 );
if( rc < 0 ) { if( rc < 0 ) {
ld->ld_errno = LDAP_ENCODING_ERROR; ld->ld_errno = LDAP_ENCODING_ERROR;
@ -117,7 +116,9 @@ ldap_passwd( LDAP *ld,
} }
rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD, 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; return rc;
} }