]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldbm/idl.c
Notices and acknowledgements
[openldap] / servers / slapd / back-ldbm / idl.c
index a3b0e89382b1e84f3e5110ada5cb47a61d4be643..16c3e6da519b6b7decbdaee40696cf4a07d909cc 100644 (file)
@@ -259,6 +259,8 @@ idl_fetch(
        }
        free( (char *) tmp );
 
+       assert( ID_BLOCK_NIDS(idl) == nids );
+
 #ifdef LDBM_DEBUG_IDL
        idl_check(idl);
 #endif
@@ -551,14 +553,13 @@ idl_insert_key(
 
 #ifndef USE_INDIRECT_NIDS
        /* select the block to try inserting into *//* XXX linear search XXX */
-       for ( i = 0; !ID_BLOCK_NOID(idl, i) && id > ID_BLOCK_ID(idl, i); i++ )
+       for ( i = 0; !ID_BLOCK_NOID(idl, i) && id >= ID_BLOCK_ID(idl, i); i++ )
                ;       /* NULL */
 #else
        i = idl_find(idl, id);
-       if (ID_BLOCK_ID(idl, i) < id)
+       if (ID_BLOCK_ID(idl, i) <= id)
                i++;
 #endif
-
        if ( i != 0 ) {
                i--;
                first = 0;
@@ -566,6 +567,11 @@ idl_insert_key(
                first = 1;
        }
 
+       /* At this point, the following condition must be true:
+        * ID_BLOCK_ID(idl, i) <= id && id < ID_BLOCK_ID(idl, i+1)
+        * except when i is the first or the last block.
+        */
+
        /* get the block */
        cont_alloc( &k2, &key );
        cont_id( &k2, ID_BLOCK_ID(idl, i) );
@@ -627,10 +633,11 @@ idl_insert_key(
 #else
                if ( !first && (unsigned long)(i + 1) < ID_BLOCK_NIDS(idl) ) {
 #endif
+                       Datum k3;
                        /* read it in */
-                       cont_alloc( &k2, &key );
-                       cont_id( &k2, ID_BLOCK_ID(idl, i + 1) );
-                       if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
+                       cont_alloc( &k3, &key );
+                       cont_id( &k3, ID_BLOCK_ID(idl, i + 1) );
+                       if ( (tmp2 = idl_fetch_one( be, db, k3 )) == NULL ) {
 #ifdef NEW_LOGGING
                                LDAP_LOG( INDEX, ERR,
                                           "idl_insert_key: idl_fetch_one returned NULL\n", 0, 0, 0);
@@ -641,7 +648,7 @@ idl_insert_key(
 #endif
 
                                /* split the original block */
-                               cont_free( &k2 );
+                               cont_free( &k3 );
                                goto split;
                        }
 
@@ -681,7 +688,7 @@ idl_insert_key(
                            db->dbc_maxids )) ) {
                        case 1:         /* id inserted first in block */
                                rc = idl_change_first( be, db, key, idl,
-                                   i + 1, k2, tmp2 );
+                                   i + 1, k3, tmp2 );
                                /* FALL */
 
                        case 2:         /* id already there - how? */
@@ -705,6 +712,8 @@ idl_insert_key(
 
                                idl_free( tmp );
                                idl_free( tmp2 );
+                               cont_free( &k3 );
+                               cont_free( &k2 );
                                idl_free( idl );
                                return( 0 );
 
@@ -713,6 +722,7 @@ idl_insert_key(
                        }
 
                        idl_free( tmp2 );
+                       cont_free( &k3 );
                }
 
 split: