]> git.ipfire.org Git - thirdparty/openldap.git/commitdiff
ITS#8977 don't use any stack allocated IDLs
authorHoward Chu <hyc@openldap.org>
Thu, 11 Jul 2019 13:18:37 +0000 (14:18 +0100)
committerHoward Chu <hyc@openldap.org>
Thu, 11 Jul 2019 14:47:03 +0000 (15:47 +0100)
Trying again, fixed previous attempt

servers/slapd/back-mdb/search.c

index 153c2d391658dedf0e77cb9c320c9f7467906285..1bfd0bc29c83395d79f0760924857fb5c6cc279b 100644 (file)
@@ -416,8 +416,7 @@ mdb_search( Operation *op, SlapReply *rs )
        struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
        ID              id, cursor, nsubs, ncand, cscope;
        ID              lastid = NOID;
-       ID              candidates[MDB_idl_um_size];
-       ID              iscopes[MDB_idl_db_size];
+       ID              *candidates, *iscopes, *c0;
        ID2             *scopes;
        void    *stack;
        Entry           *e = NULL, *base = NULL;
@@ -465,7 +464,14 @@ mdb_search( Operation *op, SlapReply *rs )
        }
 
        scopes = scope_chunk_get( op );
-       stack = search_stack( op );
+       candidates = c0 = search_stack( op );
+       iscopes = candidates + MDB_idl_um_size;
+       stack = iscopes + MDB_idl_db_size;
+       /* if candidates already in use, alloc a new array */
+       if ( c0[0] ) {
+               candidates = ch_malloc(( MDB_idl_um_size + MDB_idl_db_size ) * sizeof ( ID ));
+               iscopes = candidates + MDB_idl_um_size;
+       }
        isc.mt = ltid;
        isc.mc = mcd;
        isc.scopes = scopes;
@@ -1215,6 +1221,11 @@ done:
        if (base)
                mdb_entry_return( op, base );
        scope_chunk_ret( op, scopes );
+       if ( candidates != c0 ) {
+               ch_free( candidates );
+       } else {
+               MDB_IDL_ZERO( candidates );
+       }
 
        return rs->sr_err;
 }
@@ -1268,6 +1279,11 @@ static int oc_filter(
        return rc;
 }
 
+typedef struct IDLchunk {
+       unsigned int logn;
+       unsigned int pad;
+} IDLchunk;
+
 static void search_stack_free( void *key, void *data )
 {
        ber_memfree_x(data, NULL);
@@ -1276,26 +1292,34 @@ static void search_stack_free( void *key, void *data )
 static void *search_stack( Operation *op )
 {
        struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
-       void *ret = NULL;
+       IDLchunk *ic = NULL;
 
        if ( op->o_threadctx ) {
                ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
-                       &ret, NULL );
+                       (void **)&ic, NULL );
        } else {
-               ret = mdb->mi_search_stack;
+               ic = mdb->mi_search_stack;
        }
 
-       if ( !ret ) {
-               ret = ch_malloc( mdb->mi_search_stack_depth * MDB_idl_um_size
-                       * sizeof( ID ) );
+       if ( ic && ic->logn != MDB_idl_logn ) {
+               ber_memfree_x( ic, NULL );
+               ic = NULL;
+       }
+
+       if ( !ic ) {
+               ic = ch_malloc(( mdb->mi_search_stack_depth + 2 ) * MDB_idl_um_size
+                       * sizeof( ID ) + sizeof( IDLchunk ) );
+               ic->logn = MDB_idl_logn;
                if ( op->o_threadctx ) {
                        ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
-                               ret, search_stack_free, NULL, NULL );
+                               ic, search_stack_free, NULL, NULL );
                } else {
-                       mdb->mi_search_stack = ret;
+                       mdb->mi_search_stack = ic;
                }
+               ID *idl = (ID *)( ic+1 );
+               MDB_IDL_ZERO( idl );
        }
-       return ret;
+       return ic+1;
 }
 
 static int search_candidates(