From c37e32af0d2f8723f89c5304d41a4a41d4d34ea3 Mon Sep 17 00:00:00 2001 From: NicolasDorier Date: Sat, 8 Apr 2017 03:41:27 +0000 Subject: [PATCH] [Wallet] Prevent CInputCoin to be in a null state --- src/wallet/wallet.cpp | 18 +++++++++--------- src/wallet/wallet.h | 20 ++++++++------------ 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 540352c7d..ff135fb05 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2085,7 +2085,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin nValueRet = 0; // List of values less than target - CInputCoin coinLowestLarger; + boost::optional coinLowestLarger; std::vector vValue; CAmount nTotalLower = 0; @@ -2119,7 +2119,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin vValue.push_back(coin); nTotalLower += coin.txout.nValue; } - else if (coinLowestLarger.IsNull() || coin.txout.nValue < coinLowestLarger.txout.nValue) + else if (!coinLowestLarger || coin.txout.nValue < coinLowestLarger->txout.nValue) { coinLowestLarger = coin; } @@ -2137,10 +2137,10 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin if (nTotalLower < nTargetValue) { - if (coinLowestLarger.IsNull()) + if (!coinLowestLarger) return false; - setCoinsRet.insert(coinLowestLarger); - nValueRet += coinLowestLarger.txout.nValue; + setCoinsRet.insert(coinLowestLarger.get()); + nValueRet += coinLowestLarger->txout.nValue; return true; } @@ -2156,11 +2156,11 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin // If we have a bigger coin and (either the stochastic approximation didn't find a good solution, // or the next bigger coin is closer), return the bigger coin - if (!coinLowestLarger.IsNull() && - ((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || coinLowestLarger.txout.nValue <= nBest)) + if (coinLowestLarger && + ((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || coinLowestLarger->txout.nValue <= nBest)) { - setCoinsRet.insert(coinLowestLarger); - nValueRet += coinLowestLarger.txout.nValue; + setCoinsRet.insert(coinLowestLarger.get()); + nValueRet += coinLowestLarger->txout.nValue; } else { for (unsigned int i = 0; i < vValue.size(); i++) diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index d43bd4b59..b95d3de52 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -477,21 +477,17 @@ public: class CInputCoin { public: - CInputCoin() - { - } CInputCoin(const CWalletTx* walletTx, unsigned int i) { - if (walletTx != nullptr && i < walletTx->tx->vout.size()) - { - outpoint = COutPoint(walletTx->GetHash(), i); - txout = walletTx->tx->vout[i]; - } - } - bool IsNull() const - { - return outpoint.IsNull() && txout.IsNull(); + if (!walletTx) + throw std::invalid_argument("walletTx should not be null"); + if (i >= walletTx->tx->vout.size()) + throw std::out_of_range("The output index is out of range"); + + outpoint = COutPoint(walletTx->GetHash(), i); + txout = walletTx->tx->vout[i]; } + COutPoint outpoint; CTxOut txout;