[Pkg-bitcoin-commits] [bitcoin] 79/126: [wallet] Fix leak in CDB constructor

Jonas Smedegaard dr at jones.dk
Mon Nov 13 20:02:32 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/0.15.1_dfsg-1
in repository bitcoin.

commit de7053f1141756158c2107bcba376401b4cc94e9
Author: João Barbosa <joao.paulo.barbosa at gmail.com>
Date:   Thu Oct 12 23:14:46 2017 +0100

    [wallet] Fix leak in CDB constructor
    
    Now using a std::unique_ptr, the Db instance is correctly released
    when CDB initialization fails.
    The internal CDB state and mapFileUseCount are only mutated when
    the CDB initialization succeeds.
    
    Github-Pull: #11492
    Rebased-From: 7104de8b1f3a31d3a60009b5dc376adbedac6a9c
---
 src/wallet/db.cpp | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)

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

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-bitcoin/bitcoin.git



More information about the Pkg-bitcoin-commits mailing list