From: Pierangelo Masarati Date: Wed, 17 Aug 2005 08:25:48 +0000 (+0000) Subject: fix "matched" return and return code (ITS#3944) X-Git-Tag: OPENLDAP_REL_ENG_2_2_MP~690 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=76f52279ef551197f43c5f46e95de7a4edb62c8d;p=openldap fix "matched" return and return code (ITS#3944) --- diff --git a/servers/slapd/back-meta/search.c b/servers/slapd/back-meta/search.c index 30e0d9ba22..f6cb31a9db 100644 --- a/servers/slapd/back-meta/search.c +++ b/servers/slapd/back-meta/search.c @@ -693,17 +693,52 @@ really_bad:; * * FIXME: only the last one gets caught! */ - if ( candidate_match > 0 && rs->sr_nentries > 0 ) { + savepriv = op->o_private; + op->o_private = (void *)mi->mi_ntargets; + if ( candidate_match > 0 ) { + struct berval pmatched = BER_BVNULL; + /* we use the first one */ for ( i = 0; i < mi->mi_ntargets; i++ ) { if ( candidates[ i ].sr_tag == META_CANDIDATE && candidates[ i ].sr_matched ) { - matched = (char *)candidates[ i ].sr_matched; - candidates[ i ].sr_matched = NULL; - break; + struct berval bv, pbv; + int rc; + + ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv ); + rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx ); + + if ( rc == LDAP_SUCCESS ) { + + /* NOTE: if they all are superiors + * of the baseDN, the shorter is also + * superior of the longer... */ + if ( pbv.bv_len > pmatched.bv_len ) { + if ( !BER_BVISNULL( &pmatched ) ) { + op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx ); + } + pmatched = pbv; + op->o_private = (void *)i; + + } else { + op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx ); + } + } + + if ( candidates[ i ].sr_matched != NULL ) { + free( (char *)candidates[ i ].sr_matched ); + candidates[ i ].sr_matched = NULL; + } } } + + if ( !BER_BVISNULL( &pmatched ) ) { + matched = pmatched.bv_val; + } + + } else if ( sres == LDAP_NO_SUCH_OBJECT ) { + matched = ch_strdup( op->o_bd->be_suffix[ 0 ].bv_val ); } #if 0 @@ -742,8 +777,6 @@ really_bad:; rs->sr_err = sres; rs->sr_matched = matched; rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL ); - savepriv = op->o_private; - op->o_private = (void *)mi->mi_ntargets; send_ldap_result( op, rs ); op->o_private = savepriv; rs->sr_matched = NULL; @@ -751,7 +784,7 @@ really_bad:; finish:; if ( matched ) { - free( matched ); + op->o_tmpfree( matched, op->o_tmpmemctx ); } if ( rs->sr_v2ref ) { @@ -786,7 +819,7 @@ finish:; meta_back_release_conn( op, mc ); - return rc; + return rs->sr_err; } static int