]> git.sur5r.net Git - openldap/commitdiff
some memory handling cleanup: check that memory is freed by who allocated it, or...
authorPierangelo Masarati <ando@openldap.org>
Sat, 12 Jan 2002 17:35:01 +0000 (17:35 +0000)
committerPierangelo Masarati <ando@openldap.org>
Sat, 12 Jan 2002 17:35:01 +0000 (17:35 +0000)
servers/slapd/back-ldap/add.c
servers/slapd/back-ldap/attribute.c
servers/slapd/back-ldap/init.c
servers/slapd/back-ldap/map.c
servers/slapd/back-ldap/modify.c
servers/slapd/back-ldap/search.c
servers/slapd/back-ldap/unbind.c
servers/slapd/proto-slap.h

index 7dfcd9ad0897fafce4220b561e09c74d7ad98fe7..a4e6ebbabd7ddb087061dbb14339f386b729cca0 100644 (file)
@@ -77,7 +77,8 @@ ldap_back_add(
         * Rewrite the add dn, if needed
         */
 #ifdef ENABLE_REWRITE
-       switch (rewrite_session( li->rwinfo, "addDn", e->e_dn, conn, &mdn.bv_val )) {
+       switch (rewrite_session( li->rwinfo, "addDn", e->e_dn, conn, 
+                               &mdn.bv_val )) {
        case REWRITE_REGEXEC_OK:
                if ( mdn.bv_val != NULL && mdn.bv_val[ 0 ] != '\0' ) {
                        mdn.bv_len = strlen( mdn.bv_val );
@@ -171,10 +172,10 @@ ldap_back_add(
 
        ldap_add_s(lc->ld, mdn.bv_val, attrs);
        for (--i; i>= 0; --i) {
-               free(attrs[i]->mod_vals.modv_bvals);
-               free(attrs[i]);
+               ch_free(attrs[i]->mod_vals.modv_bvals);
+               ch_free(attrs[i]);
        }
-       free(attrs);
+       ch_free(attrs);
        if ( mdn.bv_val != e->e_dn ) {
                free( mdn.bv_val );
        }
@@ -212,7 +213,12 @@ ldap_dnattr_rewrite(
                                        a_vals->bv_val, mattr, "" );
 #endif /* !NEW_LOGGING */
 
-                       free( a_vals->bv_val );
+                       /*
+                        * FIXME: replacing server-allocated memory 
+                        * (ch_malloc) with librewrite allocated memory
+                        * (malloc)
+                        */
+                       ch_free( a_vals->bv_val );
                        a_vals->bv_val = mattr;
                        a_vals->bv_len = strlen( mattr );
                        
index 9f850df0de21675f147ea4db7aec8e467d37e53c..0fec6c5e804c1f55596ba5dc16ac9b3ad8ff96c9 100644 (file)
@@ -32,17 +32,16 @@ ldap_back_attribute(
 {
        struct ldapinfo *li = (struct ldapinfo *) be->be_private;    
        int rc = 1, i, j, count, is_oc;
-       Attribute *attr;
+       Attribute *attr = NULL;
        BVarray abv, v;
-       struct berval mapped;
-       char **vs;
-       LDAPMessage     *result, *e;
+       struct berval mapped = { 0, NULL };
+       char **vs = NULL;
+       LDAPMessage     *result = NULL, *e = NULL;
        char *gattr[2];
-       LDAP *ld;
+       LDAP *ld = NULL;
 
        *vals = NULL;
-       if (target != NULL && target->e_nname.bv_len == ndn->bv_len &&
-               strcmp(target->e_nname.bv_val, ndn->bv_val) == 0) {
+       if (target != NULL && dn_cmp( &target->e_nname, ndn )) {
                /* we already have a copy of the entry */
                /* attribute and objectclass mapping has already been done */
                if ((attr = attr_find(target->e_attrs, entry_at)) == NULL)
@@ -60,63 +59,79 @@ ldap_back_attribute(
                        }
                        v[j].bv_val = NULL;
                        *vals = v;
-                       rc = 0;
+                       return 0;
                }
 
-       } else {
-               ldap_back_map(&li->at_map, &entry_at->ad_cname, &mapped, 0);
-               if (mapped.bv_val == NULL)
-                       return(1);
+       }
+       ldap_back_map(&li->at_map, &entry_at->ad_cname, &mapped, 0);
+       if (mapped.bv_val == NULL) {
+               return 1;
+       }
 
-               if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
-                       return(1);
-               }
+       if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
+               return 1;
+       }
 
-               if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE) == LDAP_SUCCESS) {
-                       gattr[0] = mapped.bv_val;
-                       gattr[1] = NULL;
-                       if (ldap_search_ext_s(ld, ndn->bv_val, LDAP_SCOPE_BASE, "(objectclass=*)",
-                                                                       gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
-                                                                       LDAP_NO_LIMIT, &result) == LDAP_SUCCESS)
-                       {
-                               if ((e = ldap_first_entry(ld, result)) != NULL) {
-                                       vs = ldap_get_values(ld, e, mapped.bv_val);
-                                       if (vs != NULL) {
-                                               for ( count = 0; vs[count] != NULL; count++ ) { }
-                                               v = (BVarray) ch_calloc( (count + 1), sizeof(struct berval) );
-                                               if (v == NULL) {
-                                                       ldap_value_free(vs);
-                                               } else {
-                                                       is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
-                                                       for ( i = 0, j = 0; i < count; i++) {
-                                                               ber_str2bv(vs[i], 0, 0, &v[j] );
-                                                               if (!is_oc) {
-                                                                       if( v[j].bv_val == NULL )
-                                                                               ch_free(vs[i]);
-                                                                       else
-                                                                               j++;
-                                                               } else {
-                                                                       ldap_back_map(&li->oc_map, &v[j], &mapped, 1);
-                                                                       if (mapped.bv_val) {
-                                                                               ber_dupbv( &v[j], &mapped );
-                                                                               if (v[j].bv_val)
-                                                                                       j++;
-                                                                       }
-                                                                       ch_free(vs[i]);
-                                                               }
-                                                       }
-                                                       v[j].bv_val = NULL;
-                                                       *vals = v;
-                                                       rc = 0;
-                                                       ch_free(vs);
-                                               }
-                                       }
-                               }
-                               ldap_msgfree(result);
+       if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
+               goto cleanup;
+       }
+
+       gattr[0] = mapped.bv_val;
+       gattr[1] = NULL;
+       if (ldap_search_ext_s(ld, ndn->bv_val, LDAP_SCOPE_BASE, "(objectclass=*)",
+                               gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
+                               LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
+       {
+               goto cleanup;
+       }
+
+       if ((e = ldap_first_entry(ld, result)) == NULL) {
+               goto cleanup;
+       }
+               
+       vs = ldap_get_values(ld, e, mapped.bv_val);
+       if (vs == NULL) {
+               goto cleanup;
+       }
+
+       for ( count = 0; vs[count] != NULL; count++ ) { }
+       v = (BVarray) ch_calloc( (count + 1), sizeof(struct berval) );
+       if (v == NULL) {
+               goto cleanup;
+       }
+
+       is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
+       for ( i = 0, j = 0; i < count; i++) {
+               ber_str2bv(vs[i], 0, 0, &v[j] );
+               if (!is_oc) {
+                       if( v[j].bv_val == NULL )
+                               ch_free(vs[i]);
+                       else
+                               j++;
+               } else {
+                       ldap_back_map(&li->oc_map, &v[j], &mapped, 1);
+                       if (mapped.bv_val) {
+                               ber_dupbv( &v[j], &mapped );
+                               if (v[j].bv_val)
+                                       j++;
                        }
+                       ch_free(vs[i]);
                }
-               ldap_unbind(ld);
-    }
+       }
+       v[j].bv_val = NULL;
+       *vals = v;
+       rc = 0;
+       ch_free(vs);
+       vs = NULL;
+
+cleanup:
+       if (vs) {
+               ldap_value_free(vs);
+       }
+       if (result) {
+               ldap_msgfree(result);
+       }
+       ldap_unbind(ld);
 
        return(rc);
 }
index a78a33236a1b09b089c730ada760b3d7de8df233..0a15816a1e16a0d2d01c11bdd9259b2a4526b153 100644 (file)
@@ -132,9 +132,11 @@ conn_free(
        struct ldapconn *lc
 )
 {
-       ldap_unbind(lc->ld);
-       if ( lc->bound_dn.bv_val) free( lc->bound_dn.bv_val );
-       free( lc );
+       ldap_unbind( lc->ld );
+       if ( lc->bound_dn.bv_val ) {
+               ch_free( lc->bound_dn.bv_val );
+       }
+       ch_free( lc );
 }
 
 void
@@ -158,15 +160,15 @@ ldap_back_db_destroy(
                ldap_pvt_thread_mutex_lock( &li->conn_mutex );
 
                if (li->url) {
-                       free(li->url);
+                       ch_free(li->url);
                        li->url = NULL;
                }
                if (li->binddn) {
-                       free(li->binddn);
+                       ch_free(li->binddn);
                        li->binddn = NULL;
                }
                if (li->bindpw) {
-                       free(li->bindpw);
+                       ch_free(li->bindpw);
                        li->bindpw = NULL;
                }
                 if (li->conntree) {
@@ -191,6 +193,6 @@ ldap_back_db_destroy(
                ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
        }
 
-       free( be->be_private );
+       ch_free( be->be_private );
        return 0;
 }
index f3e58b4ab2606bd79e0982270178ca345e73bc68..a21cf538656bcc575ccddfc4503ece0e997cf82d 100644 (file)
@@ -183,16 +183,18 @@ ldap_back_map_filter(
                                plen = m.bv_len;
                                extra -= plen;
                                if (extra < 0) {
+                                       char *tmpnf;
                                        while (extra < 0) {
                                                extra += len;
                                                len *= 2;
                                        }
                                        s -= (long)nf;
-                                       nf = ch_realloc(nf, len + 1);
-                                       if (nf == NULL) {
-                                               free(nf);
+                                       tmpnf = ch_realloc(nf, len + 1);
+                                       if (tmpnf == NULL) {
+                                               ch_free(nf);
                                                return(NULL);
                                        }
+                                       nf = tmpnf;
                                        s += (long)nf;
                                }
                                AC_MEMCPY(s, m.bv_val, plen);
index 1c355fff4128ad7b85aecdbbf3453cc966b23fc3..4b46242c783d517e1582e0367dfd853682f02bf9 100644 (file)
@@ -157,9 +157,9 @@ cleanup:;
        }
 #endif /* ENABLE_REWRITE */
        for (i=0; modv[i]; i++)
-               free(modv[i]->mod_bvalues);
-       free(mods);
-       free(modv);
+               ch_free(modv[i]->mod_bvalues);
+       ch_free(mods);
+       ch_free(modv);
        return( ldap_back_op_result( lc, op ));
 }
 
index 69cb44963681ea045797dc6e8d56b3ca2dda5d9d..8a8f4327de6241e7d80a2642a1db2ac352d481af 100644 (file)
@@ -228,7 +228,7 @@ ldap_back_search(
 
 #ifdef ENABLE_REWRITE
        if ( mfilter.bv_val != filterstr->bv_val ) {
-               ldap_memfree( mfilter.bv_val );
+               free( mfilter.bv_val );
        }
 #endif /* ENABLE_REWRITE */
 
@@ -342,16 +342,16 @@ finish:;
                        free( mmatch );
                }
 #endif /* ENABLE_REWRITE */
-               free(match);
+               LDAP_FREE(match);
        }
        if ( err ) {
-               free( err );
+               LDAP_FREE( err );
        }
        if ( mapped_attrs ) {
-               free( mapped_attrs );
+               ch_free( mapped_attrs );
        }
        if ( mapped_filter != filterstr->bv_val ) {
-               free( mapped_filter );
+               ch_free( mapped_filter );
        }
        if ( mbase.bv_val != base->bv_val ) {
                free( mbase.bv_val );
@@ -460,7 +460,7 @@ ldap_send_entry(
                        for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
                                ldap_back_map(&li->oc_map, bv, &mapped, 1);
                                if (mapped.bv_val == NULL) {
-                                       free(bv->bv_val);
+                                       LBER_FREE(bv->bv_val);
                                        bv->bv_val = NULL;
                                        if (--last < 0)
                                                break;
@@ -468,7 +468,12 @@ ldap_send_entry(
                                        attr->a_vals[last].bv_val = NULL;
                                        i--;
                                } else if ( mapped.bv_val != bv->bv_val ) {
-                                       free(bv->bv_val);
+                                       /*
+                                        * FIXME: after LBER_FREEing
+                                        * the value is replaced by
+                                        * ch_alloc'ed memory
+                                        */
+                                       LBER_FREE(bv->bv_val);
                                        ber_dupbv( bv, &mapped );
                                }
                        }
@@ -543,7 +548,7 @@ ldap_send_entry(
                ent.e_attrs = attr->a_next;
                if (attr->a_vals != &dummy)
                        bvarray_free(attr->a_vals);
-               free(attr);
+               ch_free(attr);
        }
        
        if ( ent.e_dn && ent.e_dn != bdn.bv_val )
index 171581df54f73dd10609dee7a0658f1b8ce1855c..a0b11f40a498b0f47cb1a39d3f445d50603ba736 100644 (file)
@@ -89,9 +89,9 @@ ldap_back_conn_destroy(
                 */
                ldap_unbind(lc->ld);
                if ( lc->bound_dn.bv_val ) {
-                       free( lc->bound_dn.bv_val );
+                       ch_free( lc->bound_dn.bv_val );
                }
-               free( lc );
+               ch_free( lc );
        }
 
        /* no response to unbind */
index 595c4936c657a37e58bff38d265d4869b23979b3..5341fbbfae0be5fb350b95afd637e63a6e93945b 100644 (file)
@@ -352,6 +352,10 @@ LDAP_SLAPD_F (void) connection_internal_close( Connection *conn );
  * dn.c
  */
 
+#define dn_cmp(dn1, dn2) \
+       (((dn1)->bv_len == (dn2)->bv_len) \
+        && (strcmp((dn1)->bv_val, (dn2)->bv_val) == 0))
+
 LDAP_SLAPD_F (int) dnValidate LDAP_P((
        Syntax *syntax, 
        struct berval *val ));