From 6a4ff45e8aef34c2a54d246d14c3d6abd5467cc6 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 1 May 2020 09:41:27 +1200 Subject: [PATCH] Use BOOST_SCOPE_EXIT_TPL to clean and free datValue in CDB::Read --- src/wallet/db.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/wallet/db.h b/src/wallet/db.h index a1185da06..5fd512024 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -17,6 +17,7 @@ #include #include +#include #include @@ -129,22 +130,22 @@ protected: datValue.set_flags(DB_DBT_MALLOC); int ret = pdb->get(activeTxn, &datKey, &datValue, 0); memory_cleanse(datKey.get_data(), datKey.get_size()); - bool success = false; if (datValue.get_data() != NULL) { + BOOST_SCOPE_EXIT_TPL(&datValue) { + // Clear and free memory + memory_cleanse(datValue.get_data(), datValue.get_size()); + free(datValue.get_data()); + } BOOST_SCOPE_EXIT_END + // Unserialize value try { CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION); ssValue >> value; - success = true; } catch (const std::exception&) { - // In this case success remains 'false' + return false; } - - // Clear and free memory - memory_cleanse(datValue.get_data(), datValue.get_size()); - free(datValue.get_data()); } - return ret == 0 && success; + return (ret == 0); } template