From dd9d8cc0a3f77329d1c8d47a2097595f0c9d750b Mon Sep 17 00:00:00 2001 From: jamescowens Date: Sat, 17 Jul 2021 20:42:09 -0400 Subject: [PATCH] Check each input for IsMine() in GetAddressGroupings This commit fixes a crash from an out-of-range vector element access attempt that stems from the same situation with Gridcoin as in the following Bitcoin PR: https://github.com/bitcoin/bitcoin/pull/1872/. Very strange our code did not already have this change. --- src/wallet/wallet.cpp | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 5a3762fe4f..f42c743a86 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2523,27 +2523,40 @@ set< set > CWallet::GetAddressGroupings() if (pcoin->vin.size() > 0 && (IsMine(pcoin->vin[0]) != ISMINE_NO)) { + bool any_mine = false; + // group all input addresses with each other for (auto const& txin : pcoin->vin) { CTxDestination address; - if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address)) - continue; + + // If the input is not mine, ignore it. + if (IsMine(txin) == ISMINE_NO) continue; + + CScript& scriptPubKey = mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey; + + if (!ExtractDestination(scriptPubKey, address)) continue; + grouping.insert(address); + any_mine = true; } // group change with input addresses - for (auto const& txout : pcoin->vout) - if (IsChange(txout)) - { - CWalletTx tx = mapWallet[pcoin->vin[0].prevout.hash]; - CTxDestination txoutAddr; - if(!ExtractDestination(txout.scriptPubKey, txoutAddr)) - continue; - grouping.insert(txoutAddr); - } - groupings.insert(grouping); - grouping.clear(); + if (any_mine) { + for (auto const& txout : pcoin->vout) + if (IsChange(txout)) + { + CTxDestination txoutAddr; + if(!ExtractDestination(txout.scriptPubKey, txoutAddr)) + continue; + grouping.insert(txoutAddr); + } + } + + if (grouping.size() > 0) { + groupings.insert(grouping); + grouping.clear(); + } } // group lone addrs by themselves