Skip to content
Snippets Groups Projects
Commit 41569c5e authored by Pierangelo Masarati's avatar Pierangelo Masarati
Browse files

fix attr_merge() usage; cleanup

parent 4297cbae
No related branches found
No related tags found
No related merge requests found
...@@ -61,33 +61,39 @@ attrs_free( Attribute *a ) ...@@ -61,33 +61,39 @@ attrs_free( Attribute *a )
} }
} }
Attribute *attr_dup( Attribute *a ) Attribute *
attr_dup( Attribute *a )
{ {
Attribute *tmp; Attribute *tmp;
if( a == NULL) return NULL; if ( a == NULL) return NULL;
tmp = ch_malloc( sizeof(Attribute) ); tmp = ch_malloc( sizeof(Attribute) );
if( a->a_vals != NULL ) { if ( a->a_vals != NULL ) {
int i; int i;
for( i=0; a->a_vals[i].bv_val != NULL; i++ ) { for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
/* EMPTY */ ; /* EMPTY */ ;
} }
tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval)); tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
for( i=0; a->a_vals[i].bv_val != NULL; i++ ) { for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
ber_dupbv( &tmp->a_vals[i], &a->a_vals[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] ); BER_BVZERO( &tmp->a_vals[i] );
if( a->a_nvals != a->a_vals ) { /* a_nvals must be non null; it may be equal to a_vals */
tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval)); assert( a->a_nvals );
for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
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] ); 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] ); BER_BVZERO( &tmp->a_nvals[i] );
...@@ -107,7 +113,8 @@ Attribute *attr_dup( Attribute *a ) ...@@ -107,7 +113,8 @@ Attribute *attr_dup( Attribute *a )
return tmp; return tmp;
} }
Attribute *attrs_dup( Attribute *a ) Attribute *
attrs_dup( Attribute *a )
{ {
Attribute *tmp, **next; Attribute *tmp, **next;
...@@ -166,8 +173,14 @@ attr_merge( ...@@ -166,8 +173,14 @@ attr_merge(
rc = value_add( &(*a)->a_vals, vals ); rc = value_add( &(*a)->a_vals, vals );
if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals ); if ( rc == LDAP_SUCCESS ) {
else (*a)->a_nvals = (*a)->a_vals; if ( nvals ) {
rc = value_add( &(*a)->a_nvals, nvals );
/* FIXME: what if rc != LDAP_SUCCESS ? */
} else {
(*a)->a_nvals = (*a)->a_vals;
}
}
return rc; return rc;
} }
...@@ -187,10 +200,10 @@ attr_merge_normalize( ...@@ -187,10 +200,10 @@ attr_merge_normalize(
{ {
int i; 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 ); 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)( rc = (*desc->ad_type->sat_equality->smr_normalize)(
SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX, SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
desc->ad_type->sat_syntax, desc->ad_type->sat_syntax,
...@@ -198,7 +211,7 @@ attr_merge_normalize( ...@@ -198,7 +211,7 @@ attr_merge_normalize(
&vals[i], &nvals[i], memctx ); &vals[i], &nvals[i], memctx );
if ( rc != LDAP_SUCCESS ) { if ( rc != LDAP_SUCCESS ) {
BER_BVZERO( &nvals[i+1] ); BER_BVZERO( &nvals[i + 1] );
goto error_return; goto error_return;
} }
} }
...@@ -241,8 +254,14 @@ attr_merge_one( ...@@ -241,8 +254,14 @@ attr_merge_one(
rc = value_add_one( &(*a)->a_vals, val ); rc = value_add_one( &(*a)->a_vals, val );
if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval ); if ( rc == LDAP_SUCCESS ) {
else (*a)->a_nvals = (*a)->a_vals; 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; return rc;
} }
...@@ -254,7 +273,7 @@ attr_merge_normalize_one( ...@@ -254,7 +273,7 @@ attr_merge_normalize_one(
void *memctx ) void *memctx )
{ {
struct berval nval; struct berval nval;
struct berval *nvalp; struct berval *nvalp = NULL;
int rc; int rc;
if ( desc->ad_type->sat_equality && if ( desc->ad_type->sat_equality &&
...@@ -270,8 +289,6 @@ attr_merge_normalize_one( ...@@ -270,8 +289,6 @@ attr_merge_normalize_one(
return rc; return rc;
} }
nvalp = &nval; nvalp = &nval;
} else {
nvalp = NULL;
} }
rc = attr_merge_one( e, desc, val, nvalp ); rc = attr_merge_one( e, desc, val, nvalp );
......
...@@ -65,25 +65,20 @@ schema_info( Entry **entry, const char **text ) ...@@ -65,25 +65,20 @@ schema_info( Entry **entry, const char **text )
ber_dupbv( &e->e_nname, &frontendDB->be_schemandn ); ber_dupbv( &e->e_nname, &frontendDB->be_schemandn );
e->e_private = NULL; e->e_private = NULL;
vals[0].bv_val = "subentry"; BER_BVSTR( &vals[0], "subentry" );
vals[0].bv_len = sizeof("subentry")-1; if( attr_merge_one( e, ad_structuralObjectClass, vals, NULL ) ) {
if( attr_merge_one( e, ad_structuralObjectClass, vals, vals ) ) {
/* Out of memory, do something about it */ /* Out of memory, do something about it */
entry_free( e ); entry_free( e );
*text = "out of memory"; *text = "out of memory";
return LDAP_OTHER; return LDAP_OTHER;
} }
vals[0].bv_val = "top"; BER_BVSTR( &vals[0], "top" );
vals[0].bv_len = sizeof("top")-1; BER_BVSTR( &vals[1], "subentry" );
vals[1].bv_val = "subentry"; BER_BVSTR( &vals[2], "subschema" );
vals[1].bv_len = sizeof("subentry")-1; BER_BVSTR( &vals[3], "extensibleObject" );
vals[2].bv_val = "subschema"; BER_BVZERO( &vals[4] );
vals[2].bv_len = sizeof("subschema")-1; if ( attr_merge( e, ad_objectClass, vals, NULL ) ) {
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 ) ) {
/* Out of memory, do something about it */ /* Out of memory, do something about it */
entry_free( e ); entry_free( e );
*text = "out of memory"; *text = "out of memory";
...@@ -119,7 +114,7 @@ schema_info( Entry **entry, const char **text ) ...@@ -119,7 +114,7 @@ schema_info( Entry **entry, const char **text )
nvals[0].bv_len = frontendDB->be_schemandn.bv_len - nvals[0].bv_len = frontendDB->be_schemandn.bv_len -
(nvals[0].bv_val - frontendDB->be_schemandn.bv_val); (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 */ /* Out of memory, do something about it */
entry_free( e ); entry_free( e );
*text = "out of memory"; *text = "out of memory";
...@@ -159,13 +154,13 @@ schema_info( Entry **entry, const char **text ) ...@@ -159,13 +154,13 @@ schema_info( Entry **entry, const char **text )
vals[0].bv_val = timebuf; vals[0].bv_val = timebuf;
vals[0].bv_len = strlen( 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 */ /* Out of memory, do something about it */
entry_free( e ); entry_free( e );
*text = "out of memory"; *text = "out of memory";
return LDAP_OTHER; 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 */ /* Out of memory, do something about it */
entry_free( e ); entry_free( e );
*text = "out of memory"; *text = "out of memory";
......
...@@ -40,14 +40,16 @@ ...@@ -40,14 +40,16 @@
int int
value_add( value_add(
BerVarray *vals, BerVarray *vals,
BerVarray addvals ) BerVarray addvals )
{ {
int n, nn; int n, nn = 0;
BerVarray v2; BerVarray v2;
for ( nn = 0; addvals != NULL && addvals[nn].bv_val != NULL; nn++ ) if ( addvals != NULL ) {
; /* NULL */ for ( ; !BER_BVISNULL( &addvals[nn] ); nn++ )
; /* NULL */
}
if ( *vals == NULL ) { if ( *vals == NULL ) {
*vals = (BerVarray) SLAP_MALLOC( (nn + 1) *vals = (BerVarray) SLAP_MALLOC( (nn + 1)
...@@ -63,8 +65,9 @@ value_add( ...@@ -63,8 +65,9 @@ value_add(
return LBER_ERROR_MEMORY; return LBER_ERROR_MEMORY;
} }
n = 0; n = 0;
} else { } else {
for ( n = 0; (*vals)[n].bv_val != NULL; n++ ) { for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) {
; /* Empty */ ; /* Empty */
} }
*vals = (BerVarray) SLAP_REALLOC( (char *) *vals, *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
...@@ -81,24 +84,23 @@ value_add( ...@@ -81,24 +84,23 @@ value_add(
} }
} }
v2 = *vals + n; v2 = &(*vals)[n];
for ( ; addvals->bv_val; v2++, addvals++ ) { for ( ; !BER_BVISNULL( addvals ); v2++, addvals++ ) {
ber_dupbv(v2, addvals); ber_dupbv( v2, addvals );
if (v2->bv_val == NULL) break; if ( BER_BVISNULL( v2 ) ) break;
} }
v2->bv_val = NULL; BER_BVZERO( v2 );
v2->bv_len = 0;
return LDAP_SUCCESS; return LDAP_SUCCESS;
} }
int int
value_add_one( value_add_one(
BerVarray *vals, BerVarray *vals,
struct berval *addval ) struct berval *addval )
{ {
int n; int n;
BerVarray v2; BerVarray v2;
if ( *vals == NULL ) { if ( *vals == NULL ) {
*vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) ); *vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) );
...@@ -113,8 +115,9 @@ value_add_one( ...@@ -113,8 +115,9 @@ value_add_one(
return LBER_ERROR_MEMORY; return LBER_ERROR_MEMORY;
} }
n = 0; n = 0;
} else { } else {
for ( n = 0; (*vals)[n].bv_val != NULL; n++ ) { for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) {
; /* Empty */ ; /* Empty */
} }
*vals = (BerVarray) SLAP_REALLOC( (char *) *vals, *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
...@@ -131,12 +134,11 @@ value_add_one( ...@@ -131,12 +134,11 @@ value_add_one(
} }
} }
v2 = *vals + n; v2 = &(*vals)[n];
ber_dupbv(v2, addval); ber_dupbv(v2, addval);
v2++; v2++;
v2->bv_val = NULL; BER_BVZERO( v2 );
v2->bv_len = 0;
return LDAP_SUCCESS; return LDAP_SUCCESS;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment