From 7b9f0acbddaab1ce0c4801a5a60e8b446388a879 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Sat, 6 Aug 2022 00:13:42 +0700 Subject: [PATCH] fix: resolve some memory leaks (#4939) - In wallet/rpcdump memory the leak would happen, if wallet is not correct JSON - In BLS the leak would happen, if object hasn't been pushed in queue by some reason. Need to remove temporary object also --- src/bls/bls_worker.cpp | 8 +++++++- src/wallet/rpcdump.cpp | 11 ++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/bls/bls_worker.cpp b/src/bls/bls_worker.cpp index 08272653e7..ccf93d715c 100644 --- a/src/bls/bls_worker.cpp +++ b/src/bls/bls_worker.cpp @@ -269,7 +269,13 @@ struct Aggregator : public std::enable_shared_from_this> { void PushAggQueue(const T& v) { - aggQueue.push(new T(v)); + auto copyT = new T(v); + try { + aggQueue.push(copyT); + } catch (...) { + delete copyT; + throw; + } if (++aggQueueSize >= batchSize) { // we've collected enough intermediate results to form a new batch. diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 403de4518e..f8ce4db210 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -730,12 +730,13 @@ UniValue importelectrumwallet(const JSONRPCRequest& request) } } else { // json - char* buffer = new char [nFilesize]; - file.read(buffer, nFilesize); UniValue data(UniValue::VOBJ); - if(!data.read(buffer)) - throw JSONRPCError(RPC_TYPE_ERROR, "Cannot parse Electrum wallet export file"); - delete[] buffer; + { + auto buffer = std::make_unique(nFilesize); + file.read(buffer.get(), nFilesize); + if(!data.read(buffer.get())) + throw JSONRPCError(RPC_TYPE_ERROR, "Cannot parse Electrum wallet export file"); + } std::vector vKeys = data.getKeys();