aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/db.h
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2019-01-16 14:55:24 +0100
committerWladimir J. van der Laan <laanwj@gmail.com>2019-01-16 14:56:21 +0100
commitd44b01f028e27c02a026ba1470d631ded51e07c1 (patch)
tree9e1069434181bbb41b61f983b77aafa77c5e43b2 /src/wallet/db.h
parent19c60ca4975d5cf3847d5ba43c65eb02462c2d0f (diff)
parent4a86a0acd9ac3ca392f0584a5fd079a856e5e4ba (diff)
downloadbitcoin-d44b01f028e27c02a026ba1470d631ded51e07c1.tar.xz
Merge #14268: Introduce SafeDbt to handle Dbt with free or memory_cleanse raii-style
4a86a0acd9ac3ca392f0584a5fd079a856e5e4ba Make SafeDbt DB_DBT_MALLOC on default initialization (Ben Woosley) 1a9f9f7e5e2e73fb832f5b96ad7e9e57954f3f3c Introduce SafeDbt to handle DB_DBT_MALLOC raii-style (Ben Woosley) 951a44e9cd6cf2b8058244f3f95181c5ba683fdd Drop unused setRange arg to BerkeleyBatch::ReadAtCursor (Ben Woosley) Pull request description: This provides additional exception-safety and case handling for the proper freeing of the associated buffers. Tree-SHA512: a038d728290cdb3905e7d881608052a6675b6425729ceaf7cfe69a6e91c2ee293cdb01e4b695a20963459ffdd9d4a1f9a08b3c07b1b5ba1aa8590a8149f686db
Diffstat (limited to 'src/wallet/db.h')
-rw-r--r--src/wallet/db.h81
1 files changed, 34 insertions, 47 deletions
diff --git a/src/wallet/db.h b/src/wallet/db.h
index e453d441d7..9dc373c89b 100644
--- a/src/wallet/db.h
+++ b/src/wallet/db.h
@@ -191,10 +191,29 @@ private:
bool IsDummy() { return env == nullptr; }
};
-
/** RAII class that provides access to a Berkeley database */
class BerkeleyBatch
{
+ /** RAII class that automatically cleanses its data on destruction */
+ class SafeDbt final
+ {
+ Dbt m_dbt;
+
+ public:
+ // construct Dbt with internally-managed data
+ SafeDbt();
+ // construct Dbt with provided data
+ SafeDbt(void* data, size_t size);
+ ~SafeDbt();
+
+ // delegate to Dbt
+ const void* get_data() const;
+ u_int32_t get_size() const;
+
+ // conversion operator to access the underlying Dbt
+ operator Dbt*();
+ };
+
protected:
Db* pdb;
std::string strFile;
@@ -222,7 +241,6 @@ public:
/* verifies the database file */
static bool VerifyDatabaseFile(const fs::path& file_path, std::string& warningStr, std::string& errorStr, BerkeleyEnvironment::recoverFunc_type recoverFunc);
-public:
template <typename K, typename T>
bool Read(const K& key, T& value)
{
@@ -233,13 +251,11 @@ public:
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000);
ssKey << key;
- Dbt datKey(ssKey.data(), ssKey.size());
+ SafeDbt datKey(ssKey.data(), ssKey.size());
// Read
- Dbt datValue;
- datValue.set_flags(DB_DBT_MALLOC);
- int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
- memory_cleanse(datKey.get_data(), datKey.get_size());
+ SafeDbt datValue;
+ int ret = pdb->get(activeTxn, datKey, datValue, 0);
bool success = false;
if (datValue.get_data() != nullptr) {
// Unserialize value
@@ -250,10 +266,6 @@ public:
} catch (const std::exception&) {
// In this case success remains 'false'
}
-
- // Clear and free memory
- memory_cleanse(datValue.get_data(), datValue.get_size());
- free(datValue.get_data());
}
return ret == 0 && success;
}
@@ -270,20 +282,16 @@ public:
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000);
ssKey << key;
- Dbt datKey(ssKey.data(), ssKey.size());
+ SafeDbt datKey(ssKey.data(), ssKey.size());
// Value
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.reserve(10000);
ssValue << value;
- Dbt datValue(ssValue.data(), ssValue.size());
+ SafeDbt datValue(ssValue.data(), ssValue.size());
// Write
- int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
-
- // Clear memory in case it was a private key
- memory_cleanse(datKey.get_data(), datKey.get_size());
- memory_cleanse(datValue.get_data(), datValue.get_size());
+ int ret = pdb->put(activeTxn, datKey, datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
return (ret == 0);
}
@@ -299,13 +307,10 @@ public:
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000);
ssKey << key;
- Dbt datKey(ssKey.data(), ssKey.size());
+ SafeDbt datKey(ssKey.data(), ssKey.size());
// Erase
- int ret = pdb->del(activeTxn, &datKey, 0);
-
- // Clear memory
- memory_cleanse(datKey.get_data(), datKey.get_size());
+ int ret = pdb->del(activeTxn, datKey, 0);
return (ret == 0 || ret == DB_NOTFOUND);
}
@@ -319,13 +324,10 @@ public:
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000);
ssKey << key;
- Dbt datKey(ssKey.data(), ssKey.size());
+ SafeDbt datKey(ssKey.data(), ssKey.size());
// Exists
- int ret = pdb->exists(activeTxn, &datKey, 0);
-
- // Clear memory
- memory_cleanse(datKey.get_data(), datKey.get_size());
+ int ret = pdb->exists(activeTxn, datKey, 0);
return (ret == 0);
}
@@ -340,20 +342,12 @@ public:
return pcursor;
}
- int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, bool setRange = false)
+ int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue)
{
// Read at cursor
- Dbt datKey;
- unsigned int fFlags = DB_NEXT;
- if (setRange) {
- datKey.set_data(ssKey.data());
- datKey.set_size(ssKey.size());
- fFlags = DB_SET_RANGE;
- }
- Dbt datValue;
- datKey.set_flags(DB_DBT_MALLOC);
- datValue.set_flags(DB_DBT_MALLOC);
- int ret = pcursor->get(&datKey, &datValue, fFlags);
+ SafeDbt datKey;
+ SafeDbt datValue;
+ int ret = pcursor->get(datKey, datValue, DB_NEXT);
if (ret != 0)
return ret;
else if (datKey.get_data() == nullptr || datValue.get_data() == nullptr)
@@ -366,16 +360,9 @@ public:
ssValue.SetType(SER_DISK);
ssValue.clear();
ssValue.write((char*)datValue.get_data(), datValue.get_size());
-
- // Clear and free memory
- memory_cleanse(datKey.get_data(), datKey.get_size());
- memory_cleanse(datValue.get_data(), datValue.get_size());
- free(datKey.get_data());
- free(datValue.get_data());
return 0;
}
-public:
bool TxnBegin()
{
if (!pdb || activeTxn)