aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/db.h
diff options
context:
space:
mode:
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)