@@ -9476,6 +9476,7 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
9476
9476
MDB_db dummy ;
9477
9477
int rc , dbflag , exact ;
9478
9478
unsigned int unused = 0 , seq ;
9479
+ char * namedup ;
9479
9480
size_t len ;
9480
9481
9481
9482
if (flags & ~VALID_FLAGS )
@@ -9537,8 +9538,16 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
9537
9538
MDB_node * node = NODEPTR (mc .mc_pg [mc .mc_top ], mc .mc_ki [mc .mc_top ]);
9538
9539
if ((node -> mn_flags & (F_DUPDATA |F_SUBDATA )) != F_SUBDATA )
9539
9540
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 */
9542
9551
data .mv_size = sizeof (MDB_db );
9543
9552
data .mv_data = & dummy ;
9544
9553
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
9548
9557
dbflag |= DB_DIRTY ;
9549
9558
}
9550
9559
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 */
9553
9564
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 ;
9555
9566
txn -> mt_dbxs [slot ].md_name .mv_size = len ;
9556
9567
txn -> mt_dbxs [slot ].md_rel = NULL ;
9557
9568
txn -> mt_dbflags [slot ] = dbflag ;
0 commit comments