]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/attr.c
ITS#7735 fix memctx usage in prev commit
[openldap] / servers / slapd / attr.c
index 9f90dd58b33055f7026b68a4418f09b6f166d4a2..b97ee9abb0faf5ecec893df027454ce0d62e2c49 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2009 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;
 }
@@ -230,13 +232,16 @@ attr_dup2( Attribute *tmp, Attribute *a )
                if ( a->a_nvals != a->a_vals ) {
 
                        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 {
@@ -293,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;
@@ -316,11 +321,12 @@ 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;
                        rc = value_match( &match, a->a_desc, mr, flags,
@@ -333,12 +339,12 @@ attr_valfind(
                        } 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,
@@ -347,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 );