Skip to content

Commit fa8a1c0

Browse files
author
MacroFake
committed
rpc: Fix Univalue push_backV OOM in listtransactions
1 parent f697c06 commit fa8a1c0

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/univalue/include/univalue.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class UniValue {
8484

8585
bool push_back(const UniValue& val);
8686
bool push_backV(const std::vector<UniValue>& vec);
87+
template <class It>
88+
bool push_backV(It first, It last);
8789

8890
void __pushKV(const std::string& key, const UniValue& val);
8991
bool pushKV(const std::string& key, const UniValue& val);
@@ -137,6 +139,14 @@ class UniValue {
137139
friend const UniValue& find_value( const UniValue& obj, const std::string& name);
138140
};
139141

142+
template <class It>
143+
bool UniValue::push_backV(It first, It last)
144+
{
145+
if (typ != VARR) return false;
146+
values.insert(values.end(), first, last);
147+
return true;
148+
}
149+
140150
enum jtokentype {
141151
JTOK_ERR = -1,
142152
JTOK_NONE = 0, // eof

src/wallet/rpc/transactions.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,12 @@ static void MaybePushAddress(UniValue & entry, const CTxDestination &dest)
329329
* @param wtx The wallet transaction.
330330
* @param nMinDepth The minimum confirmation depth.
331331
* @param fLong Whether to include the JSON version of the transaction.
332-
* @param ret The UniValue into which the result is stored.
332+
* @param ret The vector into which the result is stored.
333333
* @param filter_ismine The "is mine" filter flags.
334334
* @param filter_label Optional label string to filter incoming transactions.
335335
*/
336-
static void ListTransactions(const CWallet& wallet, const CWalletTx& wtx, int nMinDepth, bool fLong, UniValue& ret, const isminefilter& filter_ismine, const std::string* filter_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
336+
template <class Vec>
337+
static void ListTransactions(const CWallet& wallet, const CWalletTx& wtx, int nMinDepth, bool fLong, Vec& ret, const isminefilter& filter_ismine, const std::string* filter_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
337338
{
338339
CAmount nFee;
339340
std::list<COutputEntry> listReceived;
@@ -519,8 +520,7 @@ RPCHelpMan listtransactions()
519520
if (nFrom < 0)
520521
throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative from");
521522

522-
UniValue ret(UniValue::VARR);
523-
523+
std::vector<UniValue> ret;
524524
{
525525
LOCK(pwallet->cs_wallet);
526526

@@ -542,9 +542,9 @@ RPCHelpMan listtransactions()
542542
if ((nFrom + nCount) > (int)ret.size())
543543
nCount = ret.size() - nFrom;
544544

545-
const std::vector<UniValue>& txs = ret.getValues();
545+
auto txs_rev_it{std::make_move_iterator(ret.rend())};
546546
UniValue result{UniValue::VARR};
547-
result.push_backV({ txs.rend() - nFrom - nCount, txs.rend() - nFrom }); // Return oldest to newest
547+
result.push_backV(txs_rev_it - nFrom - nCount, txs_rev_it - nFrom); // Return oldest to newest
548548
return result;
549549
},
550550
};

0 commit comments

Comments
 (0)