Skip to content

Commit dd9d8cc

Browse files
committed
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: bitcoin/bitcoin#1872. Very strange our code did not already have this change.
1 parent ad067e0 commit dd9d8cc

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/wallet/wallet.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,27 +2523,40 @@ set< set<CTxDestination> > CWallet::GetAddressGroupings()
25232523

25242524
if (pcoin->vin.size() > 0 && (IsMine(pcoin->vin[0]) != ISMINE_NO))
25252525
{
2526+
bool any_mine = false;
2527+
25262528
// group all input addresses with each other
25272529
for (auto const& txin : pcoin->vin)
25282530
{
25292531
CTxDestination address;
2530-
if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address))
2531-
continue;
2532+
2533+
// If the input is not mine, ignore it.
2534+
if (IsMine(txin) == ISMINE_NO) continue;
2535+
2536+
CScript& scriptPubKey = mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey;
2537+
2538+
if (!ExtractDestination(scriptPubKey, address)) continue;
2539+
25322540
grouping.insert(address);
2541+
any_mine = true;
25332542
}
25342543

25352544
// group change with input addresses
2536-
for (auto const& txout : pcoin->vout)
2537-
if (IsChange(txout))
2538-
{
2539-
CWalletTx tx = mapWallet[pcoin->vin[0].prevout.hash];
2540-
CTxDestination txoutAddr;
2541-
if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
2542-
continue;
2543-
grouping.insert(txoutAddr);
2544-
}
2545-
groupings.insert(grouping);
2546-
grouping.clear();
2545+
if (any_mine) {
2546+
for (auto const& txout : pcoin->vout)
2547+
if (IsChange(txout))
2548+
{
2549+
CTxDestination txoutAddr;
2550+
if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
2551+
continue;
2552+
grouping.insert(txoutAddr);
2553+
}
2554+
}
2555+
2556+
if (grouping.size() > 0) {
2557+
groupings.insert(grouping);
2558+
grouping.clear();
2559+
}
25472560
}
25482561

25492562
// group lone addrs by themselves

0 commit comments

Comments
 (0)