From: Howard Chu Date: Sat, 2 Jun 2012 12:24:47 +0000 (-0700) Subject: Fix mdb_open, re-use closed dbi slots X-Git-Tag: OPENLDAP_REL_ENG_2_4_32~102^2~1 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=4cabb24b830af0ecf1b726837f6b5097239e27d1;p=openldap Fix mdb_open, re-use closed dbi slots --- diff --git a/libraries/libmdb/mdb.c b/libraries/libmdb/mdb.c index 5bdf8d1230..ad235f6010 100644 --- a/libraries/libmdb/mdb.c +++ b/libraries/libmdb/mdb.c @@ -6184,6 +6184,7 @@ int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi) MDB_dbi i; MDB_cursor mc; int rc, dbflag, exact; + unsigned int unused = 0; size_t len; if (txn->mt_dbxs[FREE_DBI].md_cmp == NULL) { @@ -6206,6 +6207,11 @@ int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi) /* Is the DB already open? */ len = strlen(name); for (i=2; imt_numdbs; i++) { + if (!txn->mt_dbxs[i].md_name.mv_size) { + /* Remember this free slot */ + unused = i; + continue; + } if (len == txn->mt_dbxs[i].md_name.mv_size && !strncmp(name, txn->mt_dbxs[i].md_name.mv_data, len)) { *dbi = i; @@ -6213,7 +6219,8 @@ int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi) } } - if (txn->mt_numdbs >= txn->mt_env->me_maxdbs - 1) + /* If no free slot and max hit, fail */ + if (!unused && txn->mt_numdbs >= txn->mt_env->me_maxdbs - 1) return ENFILE; /* Find the DB info */ @@ -6242,16 +6249,20 @@ int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi) /* OK, got info, add to table */ if (rc == MDB_SUCCESS) { - txn->mt_dbxs[txn->mt_numdbs].md_name.mv_data = strdup(name); - txn->mt_dbxs[txn->mt_numdbs].md_name.mv_size = len; - txn->mt_dbxs[txn->mt_numdbs].md_rel = NULL; - txn->mt_dbflags[txn->mt_numdbs] = dbflag; - memcpy(&txn->mt_dbs[txn->mt_numdbs], data.mv_data, sizeof(MDB_db)); - *dbi = txn->mt_numdbs; - txn->mt_env->me_dbs[0][txn->mt_numdbs] = txn->mt_dbs[txn->mt_numdbs]; - txn->mt_env->me_dbs[1][txn->mt_numdbs] = txn->mt_dbs[txn->mt_numdbs]; - mdb_default_cmp(txn, txn->mt_numdbs); - txn->mt_numdbs++; + unsigned int slot = unused ? unused : txn->mt_numdbs; + txn->mt_dbxs[slot].md_name.mv_data = strdup(name); + txn->mt_dbxs[slot].md_name.mv_size = len; + txn->mt_dbxs[slot].md_rel = NULL; + txn->mt_dbflags[slot] = dbflag; + memcpy(&txn->mt_dbs[slot], data.mv_data, sizeof(MDB_db)); + *dbi = slot; + txn->mt_env->me_dbs[0][slot] = txn->mt_dbs[slot]; + txn->mt_env->me_dbs[1][slot] = txn->mt_dbs[slot]; + mdb_default_cmp(txn, slot); + if (!unused) { + txn->mt_numdbs++; + txn->mt_env->me_numdbs++; + } } return rc;