From: Pierangelo Masarati Date: Fri, 6 Aug 2004 16:39:45 +0000 (+0000) Subject: fix attr_merge() usage; cleanup X-Git-Tag: OPENDLAP_REL_ENG_2_2_MP~4 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=41569c5ed756112c212a565470de2017e9876cf6;p=openldap fix attr_merge() usage; cleanup --- diff --git a/servers/slapd/attr.c b/servers/slapd/attr.c index 762c90cb6f..87bfb97a5b 100644 --- a/servers/slapd/attr.c +++ b/servers/slapd/attr.c @@ -61,33 +61,39 @@ attrs_free( Attribute *a ) } } -Attribute *attr_dup( Attribute *a ) +Attribute * +attr_dup( Attribute *a ) { Attribute *tmp; - if( a == NULL) return NULL; + if ( a == NULL) return NULL; tmp = ch_malloc( sizeof(Attribute) ); - if( a->a_vals != NULL ) { + if ( a->a_vals != NULL ) { int i; - for( i=0; a->a_vals[i].bv_val != NULL; i++ ) { + for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) { /* EMPTY */ ; } - tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval)); - for( i=0; a->a_vals[i].bv_val != NULL; i++ ) { + tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) ); + for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) { ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] ); - if( BER_BVISNULL( &tmp->a_vals[i] ) ) break; + if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break; + /* FIXME: error? */ } BER_BVZERO( &tmp->a_vals[i] ); - if( a->a_nvals != a->a_vals ) { - tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval)); - for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) { + /* a_nvals must be non null; it may be equal to a_vals */ + assert( a->a_nvals ); + + if ( a->a_nvals != a->a_vals ) { + tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) ); + for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) { ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] ); - if( BER_BVISNULL( &tmp->a_nvals[i] ) ) break; + if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break; + /* FIXME: error? */ } BER_BVZERO( &tmp->a_nvals[i] ); @@ -107,7 +113,8 @@ Attribute *attr_dup( Attribute *a ) return tmp; } -Attribute *attrs_dup( Attribute *a ) +Attribute * +attrs_dup( Attribute *a ) { Attribute *tmp, **next; @@ -166,8 +173,14 @@ attr_merge( rc = value_add( &(*a)->a_vals, vals ); - if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals ); - else (*a)->a_nvals = (*a)->a_vals; + if ( rc == LDAP_SUCCESS ) { + if ( nvals ) { + rc = value_add( &(*a)->a_nvals, nvals ); + /* FIXME: what if rc != LDAP_SUCCESS ? */ + } else { + (*a)->a_nvals = (*a)->a_vals; + } + } return rc; } @@ -187,10 +200,10 @@ attr_merge_normalize( { int i; - for ( i = 0; vals[i].bv_val; i++ ); + for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ); nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx ); - for ( i = 0; vals[i].bv_val; i++ ) { + for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) { rc = (*desc->ad_type->sat_equality->smr_normalize)( SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX, desc->ad_type->sat_syntax, @@ -198,7 +211,7 @@ attr_merge_normalize( &vals[i], &nvals[i], memctx ); if ( rc != LDAP_SUCCESS ) { - BER_BVZERO( &nvals[i+1] ); + BER_BVZERO( &nvals[i + 1] ); goto error_return; } } @@ -241,8 +254,14 @@ attr_merge_one( rc = value_add_one( &(*a)->a_vals, val ); - if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval ); - else (*a)->a_nvals = (*a)->a_vals; + if ( rc == LDAP_SUCCESS ) { + if ( nval ) { + rc = value_add_one( &(*a)->a_nvals, nval ); + /* FIXME: what if rc != LDAP_SUCCESS ? */ + } else { + (*a)->a_nvals = (*a)->a_vals; + } + } return rc; } @@ -254,7 +273,7 @@ attr_merge_normalize_one( void *memctx ) { struct berval nval; - struct berval *nvalp; + struct berval *nvalp = NULL; int rc; if ( desc->ad_type->sat_equality && @@ -270,8 +289,6 @@ attr_merge_normalize_one( return rc; } nvalp = &nval; - } else { - nvalp = NULL; } rc = attr_merge_one( e, desc, val, nvalp ); diff --git a/servers/slapd/schema.c b/servers/slapd/schema.c index ef142d0811..0d4b99af68 100644 --- a/servers/slapd/schema.c +++ b/servers/slapd/schema.c @@ -65,25 +65,20 @@ schema_info( Entry **entry, const char **text ) ber_dupbv( &e->e_nname, &frontendDB->be_schemandn ); e->e_private = NULL; - vals[0].bv_val = "subentry"; - vals[0].bv_len = sizeof("subentry")-1; - if( attr_merge_one( e, ad_structuralObjectClass, vals, vals ) ) { + BER_BVSTR( &vals[0], "subentry" ); + if( attr_merge_one( e, ad_structuralObjectClass, vals, NULL ) ) { /* Out of memory, do something about it */ entry_free( e ); *text = "out of memory"; return LDAP_OTHER; } - vals[0].bv_val = "top"; - vals[0].bv_len = sizeof("top")-1; - vals[1].bv_val = "subentry"; - vals[1].bv_len = sizeof("subentry")-1; - vals[2].bv_val = "subschema"; - vals[2].bv_len = sizeof("subschema")-1; - vals[3].bv_val = "extensibleObject"; - vals[3].bv_len = sizeof("extensibleObject")-1; - vals[4].bv_val = NULL; - if( attr_merge( e, ad_objectClass, vals, vals ) ) { + BER_BVSTR( &vals[0], "top" ); + BER_BVSTR( &vals[1], "subentry" ); + BER_BVSTR( &vals[2], "subschema" ); + BER_BVSTR( &vals[3], "extensibleObject" ); + BER_BVZERO( &vals[4] ); + if ( attr_merge( e, ad_objectClass, vals, NULL ) ) { /* Out of memory, do something about it */ entry_free( e ); *text = "out of memory"; @@ -119,7 +114,7 @@ schema_info( Entry **entry, const char **text ) nvals[0].bv_len = frontendDB->be_schemandn.bv_len - (nvals[0].bv_val - frontendDB->be_schemandn.bv_val); - if( attr_merge_one( e, desc, vals, nvals ) ) { + if ( attr_merge_one( e, desc, vals, nvals ) ) { /* Out of memory, do something about it */ entry_free( e ); *text = "out of memory"; @@ -159,13 +154,13 @@ schema_info( Entry **entry, const char **text ) vals[0].bv_val = timebuf; vals[0].bv_len = strlen( timebuf ); - if( attr_merge_one( e, ad_createTimestamp, vals, vals ) ) { + if( attr_merge_one( e, ad_createTimestamp, vals, NULL ) ) { /* Out of memory, do something about it */ entry_free( e ); *text = "out of memory"; return LDAP_OTHER; } - if( attr_merge_one( e, ad_modifyTimestamp, vals, vals ) ) { + if( attr_merge_one( e, ad_modifyTimestamp, vals, NULL ) ) { /* Out of memory, do something about it */ entry_free( e ); *text = "out of memory"; diff --git a/servers/slapd/value.c b/servers/slapd/value.c index 5295cd8330..e22cab5144 100644 --- a/servers/slapd/value.c +++ b/servers/slapd/value.c @@ -40,14 +40,16 @@ int value_add( - BerVarray *vals, - BerVarray addvals ) + BerVarray *vals, + BerVarray addvals ) { - int n, nn; - BerVarray v2; + int n, nn = 0; + BerVarray v2; - for ( nn = 0; addvals != NULL && addvals[nn].bv_val != NULL; nn++ ) - ; /* NULL */ + if ( addvals != NULL ) { + for ( ; !BER_BVISNULL( &addvals[nn] ); nn++ ) + ; /* NULL */ + } if ( *vals == NULL ) { *vals = (BerVarray) SLAP_MALLOC( (nn + 1) @@ -63,8 +65,9 @@ value_add( return LBER_ERROR_MEMORY; } n = 0; + } else { - for ( n = 0; (*vals)[n].bv_val != NULL; n++ ) { + for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) { ; /* Empty */ } *vals = (BerVarray) SLAP_REALLOC( (char *) *vals, @@ -81,24 +84,23 @@ value_add( } } - v2 = *vals + n; - for ( ; addvals->bv_val; v2++, addvals++ ) { - ber_dupbv(v2, addvals); - if (v2->bv_val == NULL) break; + v2 = &(*vals)[n]; + for ( ; !BER_BVISNULL( addvals ); v2++, addvals++ ) { + ber_dupbv( v2, addvals ); + if ( BER_BVISNULL( v2 ) ) break; } - v2->bv_val = NULL; - v2->bv_len = 0; + BER_BVZERO( v2 ); return LDAP_SUCCESS; } int value_add_one( - BerVarray *vals, - struct berval *addval ) + BerVarray *vals, + struct berval *addval ) { - int n; - BerVarray v2; + int n; + BerVarray v2; if ( *vals == NULL ) { *vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) ); @@ -113,8 +115,9 @@ value_add_one( return LBER_ERROR_MEMORY; } n = 0; + } else { - for ( n = 0; (*vals)[n].bv_val != NULL; n++ ) { + for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) { ; /* Empty */ } *vals = (BerVarray) SLAP_REALLOC( (char *) *vals, @@ -131,12 +134,11 @@ value_add_one( } } - v2 = *vals + n; + v2 = &(*vals)[n]; ber_dupbv(v2, addval); v2++; - v2->bv_val = NULL; - v2->bv_len = 0; + BER_BVZERO( v2 ); return LDAP_SUCCESS; }