]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/sssvlv.c
check for NULL backend (ITS#6490)
[openldap] / servers / slapd / overlays / sssvlv.c
index 4db21d3b3b3219aeac376669b45cd65e513c21d9..0ee623da4fab9255880b7136eed73f3c592937c1 100644 (file)
@@ -34,6 +34,8 @@
 #include "lutil.h"
 #include "config.h"
 
+#include "../../../libraries/liblber/lber-int.h"       /* ber_rewind */
+
 /* RFC2891: Server Side Sorting
  * RFC2696: Paged Results
  */
@@ -76,7 +78,7 @@ typedef struct sort_key
 
 typedef struct sort_ctrl {
        int sc_nkeys;
-       sort_key sc_keys[0];
+       sort_key sc_keys[1];
 } sort_ctrl;
 
 
@@ -111,6 +113,7 @@ typedef struct sort_op
 /* There is only one conn table for all overlay instances */
 static sort_op **sort_conns;
 static ldap_pvt_thread_mutex_t sort_conns_mutex;
+static int ov_count;
 static const char *debug_header = "sssvlv";
 
 static int sss_cid;
@@ -694,7 +697,7 @@ static int sssvlv_op_search(
        int                                             rc                      = SLAP_CB_CONTINUE;
        int     ok;
        sort_op *so, so2;
-       sort_ctrl *sc = op->o_controls[sss_cid];
+       sort_ctrl *sc;
        PagedResultsState *ps;
        vlv_ctrl *vc;
 
@@ -723,6 +726,7 @@ static int sssvlv_op_search(
                op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
                op->o_ctrlflag[sss_cid]);
 
+       sc = op->o_controls[sss_cid];
        if ( sc->sc_nkeys > si->svi_max_keys ) {
                rs->sr_text = "Too many sort keys";
                rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
@@ -1000,7 +1004,7 @@ static int sss_parseCtrl(
        i = count_key( ber );
 
        sc = op->o_tmpalloc( sizeof(sort_ctrl) +
-               i * sizeof(sort_key), op->o_tmpmemctx );
+               (i-1) * sizeof(sort_key), op->o_tmpmemctx );
        sc->sc_nkeys = i;
        op->o_controls[sss_cid] = sc;
 
@@ -1162,6 +1166,7 @@ static int sssvlv_db_init(
                sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
                sort_conns++;
        }
+       ov_count++;
 
        return LDAP_SUCCESS;
 }
@@ -1173,6 +1178,13 @@ static int sssvlv_db_destroy(
        slap_overinst   *on = (slap_overinst *)be->bd_info;
        sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
        
+       ov_count--;
+       if ( !ov_count && sort_conns) {
+               sort_conns--;
+               ch_free(sort_conns);
+               ldap_pvt_thread_mutex_destroy( &sort_conns_mutex );
+       }
+       
        if ( si ) {
                ch_free( si );
                on->on_bi.bi_private = NULL;