]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/attr.c
ITS#7735 fix memctx usage in prev commit
[openldap] / servers / slapd / attr.c
index be18e429ef9015daf83bad648b9597d21a88aca9..b97ee9abb0faf5ecec893df027454ce0d62e2c49 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2008 The OpenLDAP Foundation.
+ * Copyright 1998-2013 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -88,6 +88,8 @@ attr_alloc( AttributeDescription *ad )
        ldap_pvt_thread_mutex_unlock( &attr_mutex );
        
        a->a_desc = ad;
+       if ( ad && ( ad->ad_type->sat_flags & SLAP_AT_SORTED_VAL ))
+               a->a_flags |= SLAP_ATTR_SORTED_VALS;
 
        return a;
 }
@@ -124,14 +126,14 @@ attrs_alloc( int num )
 
 
 void
-attr_clean_x( Attribute *a, void *ctx )
+attr_clean( Attribute *a )
 {
        if ( a->a_nvals && a->a_nvals != a->a_vals &&
                !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
                if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
-                       ber_memfree_x( a->a_nvals, ctx );
+                       free( a->a_nvals );
                } else {
-                       ber_bvarray_free_x( a->a_nvals, ctx );
+                       ber_bvarray_free( a->a_nvals );
                }
        }
        /* a_vals may be equal to slap_dummy_bv, a static empty berval;
@@ -141,9 +143,9 @@ attr_clean_x( Attribute *a, void *ctx )
        if ( a->a_vals != &slap_dummy_bv &&
                !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
                if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
-                       ber_memfree_x( a->a_vals, ctx );
+                       free( a->a_vals );
                } else {
-                       ber_bvarray_free_x( a->a_vals, ctx );
+                       ber_bvarray_free( a->a_vals );
                }
        }
        a->a_desc = NULL;
@@ -156,12 +158,6 @@ attr_clean_x( Attribute *a, void *ctx )
        a->a_numvals = 0;
 }
 
-void
-attr_clean( Attribute *a )
-{
-       attr_clean_x( a, NULL );
-}
-
 void
 attr_free( Attribute *a )
 {
@@ -190,7 +186,7 @@ comp_tree_free( Attribute *a )
 #endif
 
 void
-attrs_free_x( Attribute *a, void *ctx )
+attrs_free( Attribute *a )
 {
        if ( a ) {
                Attribute *b = (Attribute *)0xBAD, *tail, *next;
@@ -199,7 +195,7 @@ attrs_free_x( Attribute *a, void *ctx )
                tail = a;
                do {
                        next = a->a_next;
-                       attr_clean_x( a, ctx );
+                       attr_clean( a );
                        a->a_next = b;
                        b = a;
                        a = next;
@@ -214,18 +210,12 @@ attrs_free_x( Attribute *a, void *ctx )
        }
 }
 
-void
-attrs_free( Attribute *a )
-{
-       attrs_free_x( a, NULL );
-}
-
 static void
 attr_dup2( Attribute *tmp, Attribute *a )
 {
        tmp->a_flags = a->a_flags & SLAP_ATTR_PERSISTENT_FLAGS;
        if ( a->a_vals != NULL ) {
-               int     i;
+               unsigned        i, j;
 
                tmp->a_numvals = a->a_numvals;
                tmp->a_vals = ch_malloc( (tmp->a_numvals + 1) * sizeof(struct berval) );
@@ -240,16 +230,18 @@ attr_dup2( Attribute *tmp, Attribute *a )
                assert( a->a_nvals != NULL );
 
                if ( a->a_nvals != a->a_vals ) {
-                       int     j;
 
                        tmp->a_nvals = ch_malloc( (tmp->a_numvals + 1) * sizeof(struct berval) );
-                       for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
-                               assert( j < i );
-                               ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
-                               if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
-                               /* FIXME: error? */
+                       j = 0;
+                       if ( i ) {
+                               for ( ; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
+                                       assert( j < i );
+                                       ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
+                                       if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
+                                       /* FIXME: error? */
+                               }
+                               assert( j == i );
                        }
-                       assert( j == i );
                        BER_BVZERO( &tmp->a_nvals[j] );
 
                } else {
@@ -306,7 +298,7 @@ attr_valfind(
        MatchingRule *mr;
        const char *text;
        int match = -1, rc;
-       unsigned i;
+       unsigned i, n;
 
        if ( flags & SLAP_MR_ORDERING )
                mr = a->a_desc->ad_type->sat_ordering;
@@ -329,30 +321,30 @@ attr_valfind(
                cval = val;
        }
 
-       if ( a->a_flags & SLAP_ATTR_SORTED_VALS ) {
+       n = a->a_numvals;
+       if ( (a->a_flags & SLAP_ATTR_SORTED_VALS) && n ) {
                /* Binary search */
-               unsigned base = 0, n = a->a_numvals;
+               unsigned base = 0;
 
-               while ( 0 < n ) {
+               do {
                        unsigned pivot = n >> 1;
                        i = base + pivot;
-                       if ( i >= a->a_numvals ) {
-                               i = a->a_numvals - 1;
-                               break;
-                       }
                        rc = value_match( &match, a->a_desc, mr, flags,
                                &a->a_nvals[i], cval, &text );
                        if ( rc == LDAP_SUCCESS && match == 0 )
                                break;
-                       n = pivot;
-                       if ( match < 0 )
+                       if ( match < 0 ) {
                                base = i+1;
-               }
+                               n -= pivot+1;
+                       } else {
+                               n = pivot;
+                       }
+               } while ( n );
                if ( match < 0 )
                        i++;
        } else {
        /* Linear search */
-               for ( i = 0; i < a->a_numvals; i++ ) {
+               for ( i = 0; i < n; i++ ) {
                        const char *text;
 
                        rc = ordered_value_match( &match, a->a_desc, mr, flags,
@@ -361,10 +353,10 @@ attr_valfind(
                                break;
                }
        }
-       if ( slot )
-               *slot = i;
        if ( match )
                rc = LDAP_NO_SUCH_ATTRIBUTE;
+       if ( slot )
+               *slot = i;
        if ( nval.bv_val )
                slap_sl_free( nval.bv_val, ctx );
 
@@ -417,7 +409,7 @@ attr_valadd(
                                        rc = LDAP_TYPE_OR_VALUE_EXISTS;
                                return rc;
                        }
-                       for ( j = a->a_numvals; j >= slot; j-- ) {
+                       for ( j = a->a_numvals; j >= (int)slot; j-- ) {
                                a->a_vals[j+1] = a->a_vals[j];
                                if ( nvals )
                                        a->a_nvals[j+1] = a->a_nvals[j];