Skip to content

Commit c37e32a

Browse files
committed
[Wallet] Prevent CInputCoin to be in a null state
1 parent f597dcb commit c37e32a

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

src/wallet/wallet.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,7 +2085,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
20852085
nValueRet = 0;
20862086

20872087
// List of values less than target
2088-
CInputCoin coinLowestLarger;
2088+
boost::optional<CInputCoin> coinLowestLarger;
20892089
std::vector<CInputCoin> vValue;
20902090
CAmount nTotalLower = 0;
20912091

@@ -2119,7 +2119,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
21192119
vValue.push_back(coin);
21202120
nTotalLower += coin.txout.nValue;
21212121
}
2122-
else if (coinLowestLarger.IsNull() || coin.txout.nValue < coinLowestLarger.txout.nValue)
2122+
else if (!coinLowestLarger || coin.txout.nValue < coinLowestLarger->txout.nValue)
21232123
{
21242124
coinLowestLarger = coin;
21252125
}
@@ -2137,10 +2137,10 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
21372137

21382138
if (nTotalLower < nTargetValue)
21392139
{
2140-
if (coinLowestLarger.IsNull())
2140+
if (!coinLowestLarger)
21412141
return false;
2142-
setCoinsRet.insert(coinLowestLarger);
2143-
nValueRet += coinLowestLarger.txout.nValue;
2142+
setCoinsRet.insert(coinLowestLarger.get());
2143+
nValueRet += coinLowestLarger->txout.nValue;
21442144
return true;
21452145
}
21462146

@@ -2156,11 +2156,11 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
21562156

21572157
// If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
21582158
// or the next bigger coin is closer), return the bigger coin
2159-
if (!coinLowestLarger.IsNull() &&
2160-
((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || coinLowestLarger.txout.nValue <= nBest))
2159+
if (coinLowestLarger &&
2160+
((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || coinLowestLarger->txout.nValue <= nBest))
21612161
{
2162-
setCoinsRet.insert(coinLowestLarger);
2163-
nValueRet += coinLowestLarger.txout.nValue;
2162+
setCoinsRet.insert(coinLowestLarger.get());
2163+
nValueRet += coinLowestLarger->txout.nValue;
21642164
}
21652165
else {
21662166
for (unsigned int i = 0; i < vValue.size(); i++)

src/wallet/wallet.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -477,21 +477,17 @@ class CWalletTx : public CMerkleTx
477477

478478
class CInputCoin {
479479
public:
480-
CInputCoin()
481-
{
482-
}
483480
CInputCoin(const CWalletTx* walletTx, unsigned int i)
484481
{
485-
if (walletTx != nullptr && i < walletTx->tx->vout.size())
486-
{
487-
outpoint = COutPoint(walletTx->GetHash(), i);
488-
txout = walletTx->tx->vout[i];
489-
}
490-
}
491-
bool IsNull() const
492-
{
493-
return outpoint.IsNull() && txout.IsNull();
482+
if (!walletTx)
483+
throw std::invalid_argument("walletTx should not be null");
484+
if (i >= walletTx->tx->vout.size())
485+
throw std::out_of_range("The output index is out of range");
486+
487+
outpoint = COutPoint(walletTx->GetHash(), i);
488+
txout = walletTx->tx->vout[i];
494489
}
490+
495491
COutPoint outpoint;
496492
CTxOut txout;
497493

0 commit comments

Comments
 (0)