]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapi/slapi_utils.c
Fix slapi_valueset_count() for no value case
[openldap] / servers / slapd / slapi / slapi_utils.c
index 98951cef6696403bc563a1678fe5ec04679619bd..39cd1ed19539dc74407948667628ba43fcb64e15 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2002-2005 The OpenLDAP Foundation.
+ * Copyright 2002-2006 The OpenLDAP Foundation.
  * Portions Copyright 1997,2002-2003 IBM Corporation.
  * All rights reserved.
  *
@@ -110,16 +110,7 @@ slapi_str2entry(
        char            *s, 
        int             flags )
 {
-       Slapi_Entry     *e = NULL;
-       char            *pTmpS;
-
-       pTmpS = slapi_ch_strdup( s );
-       if ( pTmpS != NULL ) {
-               e = str2entry( pTmpS ); 
-               slapi_ch_free( (void **)&pTmpS );
-       }
-
-       return e;
+       return str2entry( s );
 }
 
 char *
@@ -127,10 +118,13 @@ slapi_entry2str(
        Slapi_Entry     *e, 
        int             *len ) 
 {
-       char            *ret;
+       char            *ret = NULL;
+       char            *s;
 
        ldap_pvt_thread_mutex_lock( &entry2str_mutex );
-       ret = entry2str( e, len );
+       s = entry2str( e, len );
+       if ( s != NULL )
+               ret = slapi_ch_strdup( s );
        ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
 
        return ret;
@@ -232,12 +226,12 @@ slapi_entry_attr_merge(
        BerVarray               bv;
        int                     rc;
 
-       rc = bvptr2obj( vals, &bv );
+       rc = slap_str2ad( type, &ad, &text );
        if ( rc != LDAP_SUCCESS ) {
                return -1;
        }
        
-       rc = slap_str2ad( type, &ad, &text );
+       rc = bvptr2obj( vals, &bv );
        if ( rc != LDAP_SUCCESS ) {
                return -1;
        }
@@ -294,7 +288,7 @@ slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
 
                p = slapi_value_get_string( &attr->a_vals[0] );
                if ( p != NULL ) {
-                       return slapi_ch_strdup( (char *)p );
+                       return slapi_ch_strdup( p );
                }
        }
 
@@ -909,6 +903,7 @@ slapi_dn_parent( const char *_dn )
 {
        struct berval   dn, prettyDN;
        struct berval   parentDN;
+       char            *ret;
 
        if ( _dn == NULL ) {
                return NULL;
@@ -927,13 +922,15 @@ slapi_dn_parent( const char *_dn )
 
        dnParent( &prettyDN, &parentDN ); /* in-place */
 
-       slapi_ch_free( (void **)&prettyDN.bv_val );
-
        if ( parentDN.bv_len == 0 ) {
+               slapi_ch_free_string( &prettyDN.bv_val );
                return NULL;
        }
 
-       return slapi_ch_strdup( parentDN.bv_val );
+       ret = slapi_ch_strdup( parentDN.bv_val );
+       slapi_ch_free_string( &prettyDN.bv_val );
+
+       return ret;
 }
 
 int slapi_dn_isbesuffix( Slapi_PBlock *pb, char *ldn )
@@ -1094,15 +1091,15 @@ slapi_ch_realloc(
 }
 
 char *
-slapi_ch_strdup( char *s ) 
+slapi_ch_strdup( const char *s ) 
 {
-       return ch_strdup( (const char *)s );
+       return ch_strdup( s );
 }
 
 size_t
-slapi_ch_stlen( char *s ) 
+slapi_ch_stlen( const char *s ) 
 {
-       return strlen( (const char *)s );
+       return strlen( s );
 }
 
 int 
@@ -1481,77 +1478,7 @@ slapi_filter_free(
 Slapi_Filter *
 slapi_filter_dup( Slapi_Filter *filter )
 {
-       Filter *f;
-
-       f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
-       f->f_next = NULL;
-       f->f_choice = filter->f_choice;
-
-       switch ( f->f_choice ) {
-       case LDAP_FILTER_AND:
-       case LDAP_FILTER_NOT:
-       case LDAP_FILTER_OR: {
-               Filter *pFilter, **ppF;
-
-               for ( pFilter = filter->f_list, ppF = &f->f_list;
-                     pFilter != NULL;
-                     pFilter = pFilter->f_next, ppF = &f->f_next )
-               {
-                       *ppF = slapi_filter_dup( pFilter );
-                       if ( *ppF == NULL )
-                               break;
-               }
-               break;
-       }
-       case LDAP_FILTER_PRESENT:
-               f->f_desc = filter->f_desc;
-               break;
-       case LDAP_FILTER_EQUALITY:
-       case LDAP_FILTER_GE:
-       case LDAP_FILTER_LE:
-       case LDAP_FILTER_APPROX:
-               f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
-               f->f_ava->aa_desc = filter->f_ava->aa_desc;
-               ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
-               break;
-       case LDAP_FILTER_EXT:
-               f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
-               f->f_mra->ma_rule = filter->f_mra->ma_rule;
-               f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
-               f->f_mra->ma_desc = filter->f_mra->ma_desc;
-               f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
-               ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
-               break;
-       case LDAP_FILTER_SUBSTRINGS: {
-               int i;
-
-               f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
-               f->f_sub->sa_desc = filter->f_sub->sa_desc;
-               ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
-               if ( filter->f_sub_any != NULL ) {
-                       for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
-                               ;
-                       f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
-                       for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
-                               ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
-                       }
-                       f->f_sub_any[i].bv_val = NULL;
-               } else {
-                       f->f_sub_any = NULL;
-               }
-               ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
-               break;
-       }
-       case SLAPD_FILTER_COMPUTED:
-               f->f_result = filter->f_result;
-               break;
-       default:
-               slapi_ch_free( (void **)&f );
-               f = NULL;
-               break;
-       }
-
-       return f;
+       return filter_dup( filter, NULL );
 }
 
 int 
@@ -2376,7 +2303,7 @@ Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
 
 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
 {
-       v->bv_val = slapi_ch_strdup( (char *)s );
+       v->bv_val = slapi_ch_strdup( s );
        v->bv_len = strlen( s );
 
        return v;
@@ -2533,9 +2460,9 @@ void slapi_valueset_free(Slapi_ValueSet *vs)
                BerVarray vp = *vs;
 
                ber_bvarray_free( vp );
-               slapi_ch_free( (void **)&vp );
+               vp = NULL;
 
-               *vs = NULL;
+               slapi_ch_free( (void **)&vp );
        }
 }
 
@@ -2605,6 +2532,9 @@ int slapi_valueset_count( const Slapi_ValueSet *vs )
 
        vp = *vs;
 
+       if ( vp == NULL )
+               return 0;
+
        for ( i = 0; vp[i].bv_val != NULL; i++ )
                ;
 
@@ -2697,20 +2627,14 @@ int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char
 
 /*
  * Synthesise an LDAPMod array from a Modifications list to pass
- * to SLAPI. This synthesis is destructive and as such the 
- * Modifications list may not be used after calling this 
- * function.
- * 
- * This function must also be called before slap_mods_check().
+ * to SLAPI.
  */
-LDAPMod **slapi_int_modifications2ldapmods( Modifications **pmodlist )
+LDAPMod **slapi_int_modifications2ldapmods( Modifications *modlist )
 {
-       Modifications *ml, *modlist;
+       Modifications *ml;
        LDAPMod **mods, *modp;
        int i, j;
 
-       modlist = *pmodlist;
-
        for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
                ;
 
@@ -2720,8 +2644,13 @@ LDAPMod **slapi_int_modifications2ldapmods( Modifications **pmodlist )
                mods[i] = (LDAPMod *)slapi_ch_malloc( sizeof(LDAPMod) );
                modp = mods[i];
                modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
-               modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val );
-               ml->sml_type.bv_val = NULL;
+               if ( BER_BVISNULL( &ml->sml_type ) ) {
+                       /* may happen for internally generated mods */
+                       assert( ml->sml_desc != NULL );
+                       modp->mod_type = slapi_ch_strdup( ml->sml_desc->ad_cname.bv_val );
+               } else {
+                       modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val );
+               }
 
                if ( ml->sml_values != NULL ) {
                        for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
@@ -2731,14 +2660,9 @@ LDAPMod **slapi_int_modifications2ldapmods( Modifications **pmodlist )
                        for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
                                modp->mod_bvalues[j] = (struct berval *)slapi_ch_malloc(
                                                sizeof(struct berval) );
-                               /* Take ownership of original values. */
-                               modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
-                               modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
-                               ml->sml_values[j].bv_len = 0;
-                               ml->sml_values[j].bv_val = NULL;
+                               ber_dupbv( modp->mod_bvalues[j], &ml->sml_values[j] );
                        }
                        modp->mod_bvalues[j] = NULL;
-                       slapi_ch_free( (void **)&ml->sml_values );
                } else {
                        modp->mod_bvalues = NULL;
                }
@@ -2747,9 +2671,6 @@ LDAPMod **slapi_int_modifications2ldapmods( Modifications **pmodlist )
 
        mods[i] = NULL;
 
-       slap_mods_free( modlist, 1 );
-       *pmodlist = NULL;
-
        return mods;
 }
 
@@ -3111,7 +3032,7 @@ int slapi_int_access_allowed( Operation *op,
                break;
         }
 
-       rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
+       rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
        if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
                /* nothing to do; allowed access */
                return 1;
@@ -3479,20 +3400,13 @@ const char *slapi_x_be_get_updatedn( Slapi_Backend *be )
        return be->be_update_ndn.bv_val;
 }
 
-Slapi_Backend *slapi_x_be_select(const char *dn)
+Slapi_Backend *slapi_be_select( const Slapi_DN *sdn )
 {
-       struct berval bdn;
-       struct berval ndn;
        Slapi_Backend *be;
 
-       bdn.bv_val = (char *)dn;
-       bdn.bv_len = (dn != NULL) ? strlen(dn) : 0;
-
-       if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS )
-               return NULL;
+       slapi_sdn_get_ndn( sdn );
 
-       be = select_backend( &ndn, 0, 0 );
-       slapi_ch_free_string( &ndn.bv_val );
+       be = select_backend( (struct berval *)&sdn->ndn, 0, 0 );
 
        return be;
 }