Skip to content

Commit 18caeaa

Browse files
hfuruhyc
authored andcommitted
mdb_dbi_open(): Catch strdup failure
1 parent 14ce42f commit 18caeaa

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

libraries/liblmdb/mdb.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9476,6 +9476,7 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
94769476
MDB_db dummy;
94779477
int rc, dbflag, exact;
94789478
unsigned int unused = 0, seq;
9479+
char *namedup;
94799480
size_t len;
94809481

94819482
if (flags & ~VALID_FLAGS)
@@ -9537,8 +9538,16 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
95379538
MDB_node *node = NODEPTR(mc.mc_pg[mc.mc_top], mc.mc_ki[mc.mc_top]);
95389539
if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) != F_SUBDATA)
95399540
return MDB_INCOMPATIBLE;
9540-
} else if (rc == MDB_NOTFOUND && (flags & MDB_CREATE)) {
9541-
/* Create if requested */
9541+
} else if (! (rc == MDB_NOTFOUND && (flags & MDB_CREATE))) {
9542+
return rc;
9543+
}
9544+
9545+
/* Done here so we cannot fail after creating a new DB */
9546+
if ((namedup = strdup(name)) == NULL)
9547+
return ENOMEM;
9548+
9549+
if (rc) {
9550+
/* MDB_NOTFOUND and MDB_CREATE: Create new DB */
95429551
data.mv_size = sizeof(MDB_db);
95439552
data.mv_data = &dummy;
95449553
memset(&dummy, 0, sizeof(dummy));
@@ -9548,10 +9557,12 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
95489557
dbflag |= DB_DIRTY;
95499558
}
95509559

9551-
/* OK, got info, add to table */
9552-
if (rc == MDB_SUCCESS) {
9560+
if (rc) {
9561+
free(namedup);
9562+
} else {
9563+
/* Got info, register DBI in this txn */
95539564
unsigned int slot = unused ? unused : txn->mt_numdbs;
9554-
txn->mt_dbxs[slot].md_name.mv_data = strdup(name);
9565+
txn->mt_dbxs[slot].md_name.mv_data = namedup;
95559566
txn->mt_dbxs[slot].md_name.mv_size = len;
95569567
txn->mt_dbxs[slot].md_rel = NULL;
95579568
txn->mt_dbflags[slot] = dbflag;

0 commit comments

Comments
 (0)