]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/midl.c
ITS#8363 Fix off-by-one in mdb_midl_shrink
[openldap] / libraries / liblmdb / midl.c
index e7bd680cb015f5dad2cc9d0923c7d09f0d832e9e..5c6d841a7adcaeb713df9a21f6bdecec014cb564 100644 (file)
@@ -3,7 +3,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2000-2013 The OpenLDAP Foundation.
+ * Copyright 2000-2015 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <assert.h>
 #include "midl.h"
 
-/** @defgroup internal MDB Internals
+/** @defgroup internal LMDB Internals
  *     @{
  */
 /** @defgroup idls     ID List Management
@@ -31,8 +30,7 @@
  */
 #define CMP(x,y)        ( (x) < (y) ? -1 : (x) > (y) )
 
-#if 0  /* superseded by append/sort */
-static unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id )
+unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id )
 {
        /*
         * binary search of id in ids
@@ -60,13 +58,14 @@ static unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id )
                        return cursor;
                }
        }
-       
+
        if( val > 0 ) {
                ++cursor;
        }
        return cursor;
 }
 
+#if 0  /* superseded by append/sort */
 int mdb_midl_insert( MDB_IDL ids, MDB_ID id )
 {
        unsigned x, i;
@@ -89,7 +88,7 @@ int mdb_midl_insert( MDB_IDL ids, MDB_ID id )
                /* no room */
                --ids[0];
                return -2;
-       
+
        } else {
                /* insert id */
                for (i=ids[0]; i>x; i--)
@@ -117,17 +116,15 @@ void mdb_midl_free(MDB_IDL ids)
                free(ids-1);
 }
 
-int mdb_midl_shrink( MDB_IDL *idp )
+void mdb_midl_shrink( MDB_IDL *idp )
 {
        MDB_IDL ids = *idp;
        if (*(--ids) > MDB_IDL_UM_MAX &&
-               (ids = realloc(ids, (MDB_IDL_UM_MAX+1) * sizeof(MDB_ID))))
+               (ids = realloc(ids, (MDB_IDL_UM_MAX+2) * sizeof(MDB_ID))))
        {
                *ids++ = MDB_IDL_UM_MAX;
                *idp = ids;
-               return 1;
        }
-       return 0;
 }
 
 static int mdb_midl_grow( MDB_IDL *idp, int num )
@@ -150,7 +147,7 @@ int mdb_midl_need( MDB_IDL *idp, unsigned num )
                num = (num + num/4 + (256 + 2)) & -256;
                if (!(ids = realloc(ids-1, num * sizeof(MDB_ID))))
                        return ENOMEM;
-               *ids++ = num -= 2;
+               *ids++ = num - 2;
                *idp = ids;
        }
        return 0;
@@ -200,10 +197,24 @@ int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n )
        return 0;
 }
 
+void mdb_midl_xmerge( MDB_IDL idl, MDB_IDL merge )
+{
+       MDB_ID old_id, merge_id, i = merge[0], j = idl[0], k = i+j, total = k;
+       idl[0] = (MDB_ID)-1;            /* delimiter for idl scan below */
+       old_id = idl[j];
+       while (i) {
+               merge_id = merge[i--];
+               for (; old_id < merge_id; old_id = idl[--j])
+                       idl[k--] = old_id;
+               idl[k--] = merge_id;
+       }
+       idl[0] = total;
+}
+
 /* Quicksort + Insertion sort for small arrays */
 
 #define SMALL  8
-#define        SWAP(a,b)       { itmp=(a); (a)=(b); (b)=itmp; }
+#define        MIDL_SWAP(a,b)  { itmp=(a); (a)=(b); (b)=itmp; }
 
 void
 mdb_midl_sort( MDB_IDL ids )
@@ -231,15 +242,15 @@ mdb_midl_sort( MDB_IDL ids )
                        l = istack[jstack--];
                } else {
                        k = (l + ir) >> 1;      /* Choose median of left, center, right */
-                       SWAP(ids[k], ids[l+1]);
+                       MIDL_SWAP(ids[k], ids[l+1]);
                        if (ids[l] < ids[ir]) {
-                               SWAP(ids[l], ids[ir]);
+                               MIDL_SWAP(ids[l], ids[ir]);
                        }
                        if (ids[l+1] < ids[ir]) {
-                               SWAP(ids[l+1], ids[ir]);
+                               MIDL_SWAP(ids[l+1], ids[ir]);
                        }
                        if (ids[l] < ids[l+1]) {
-                               SWAP(ids[l], ids[l+1]);
+                               MIDL_SWAP(ids[l], ids[l+1]);
                        }
                        i = l+1;
                        j = ir;
@@ -248,7 +259,7 @@ mdb_midl_sort( MDB_IDL ids )
                                do i++; while(ids[i] > a);
                                do j--; while(ids[j] < a);
                                if (j < i) break;
-                               SWAP(ids[i],ids[j]);
+                               MIDL_SWAP(ids[i],ids[j]);
                        }
                        ids[l+1] = ids[j];
                        ids[j] = a;
@@ -306,7 +317,6 @@ int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )
        unsigned x, i;
 
        x = mdb_mid2l_search( ids, id->mid );
-       assert( x > 0 );
 
        if( x < 1 ) {
                /* internal error */