Browse Source

Merge #11492: [wallet] Fix leak in CDB constructor

7104de8 [wallet] Fix leak in CDB constructor (João Barbosa)

Pull request description:

  First commit fixes a minor leak.
  Second commit improves the constructor in the failure cases.

Tree-SHA512: 5165413d60ed9fc28203c9fe128adbba03a9ea9e9aa3734d9ea2522dafd815ba0fb8b90fd0809dbc06eb3ad360e7764de01dadf653ade3350fe86f6b8f04bc90
0.16
Wladimir J. van der Laan 7 years ago
parent
commit
b645f368f2
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D
  1. 30
      src/wallet/db.cpp

30
src/wallet/db.cpp

@ -379,45 +379,43 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
if (!env->Open(GetDataDir())) if (!env->Open(GetDataDir()))
throw std::runtime_error("CDB: Failed to open database environment."); throw std::runtime_error("CDB: Failed to open database environment.");
strFile = strFilename; pdb = env->mapDb[strFilename];
++env->mapFileUseCount[strFile];
pdb = env->mapDb[strFile];
if (pdb == nullptr) { if (pdb == nullptr) {
int ret; int ret;
pdb = new Db(env->dbenv, 0); std::unique_ptr<Db> pdb_temp(new Db(env->dbenv, 0));
bool fMockDb = env->IsMock(); bool fMockDb = env->IsMock();
if (fMockDb) { if (fMockDb) {
DbMpoolFile* mpf = pdb->get_mpf(); DbMpoolFile* mpf = pdb_temp->get_mpf();
ret = mpf->set_flags(DB_MPOOL_NOFILE, 1); ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
if (ret != 0) if (ret != 0) {
throw std::runtime_error(strprintf("CDB: Failed to configure for no temp file backing for database %s", strFile)); throw std::runtime_error(strprintf("CDB: Failed to configure for no temp file backing for database %s", strFilename));
}
} }
ret = pdb->open(nullptr, // Txn pointer ret = pdb_temp->open(nullptr, // Txn pointer
fMockDb ? nullptr : strFile.c_str(), // Filename fMockDb ? nullptr : strFilename.c_str(), // Filename
fMockDb ? strFile.c_str() : "main", // Logical db name fMockDb ? strFilename.c_str() : "main", // Logical db name
DB_BTREE, // Database type DB_BTREE, // Database type
nFlags, // Flags nFlags, // Flags
0); 0);
if (ret != 0) { if (ret != 0) {
delete pdb;
pdb = nullptr;
--env->mapFileUseCount[strFile];
strFile = "";
throw std::runtime_error(strprintf("CDB: Error %d, can't open database %s", ret, strFilename)); throw std::runtime_error(strprintf("CDB: Error %d, can't open database %s", ret, strFilename));
} }
pdb = pdb_temp.release();
env->mapDb[strFilename] = pdb;
if (fCreate && !Exists(std::string("version"))) { if (fCreate && !Exists(std::string("version"))) {
bool fTmp = fReadOnly; bool fTmp = fReadOnly;
fReadOnly = false; fReadOnly = false;
WriteVersion(CLIENT_VERSION); WriteVersion(CLIENT_VERSION);
fReadOnly = fTmp; fReadOnly = fTmp;
} }
env->mapDb[strFile] = pdb;
} }
++env->mapFileUseCount[strFilename];
strFile = strFilename;
} }
} }

Loading…
Cancel
Save