Skip to content

Commit

Permalink
Check each input for IsMine() in GetAddressGroupings
Browse files Browse the repository at this point in the history
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: bitcoin/bitcoin#1872.

Very strange our code did not already have this change.
  • Loading branch information
jamescowens committed Jul 18, 2021
1 parent ad067e0 commit dd9d8cc
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2523,27 +2523,40 @@ set< set<CTxDestination> > 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
Expand Down

0 comments on commit dd9d8cc

Please sign in to comment.