some memory handling cleanup: check that memory is freed by who allocated it, or at least put a FIXME where not

This commit is contained in:
Pierangelo Masarati 2002-01-12 17:35:01 +00:00
parent 011b4763c5
commit e89d7b1280
8 changed files with 120 additions and 86 deletions

View 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 );

View 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);
}

View 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;
}

View 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);

View 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 ));
}

View 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 )

View 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 */

View 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 ));