Skip to content

Commit 1c63052

Browse files
UdjinM6schinzelh
authored andcommitted
fix recently observed crash on IsValid (#1375)
* fix recently observed crash on IsValid +some small cleanup * txin.prevout.n should not exceed txOutpointCreated.vout.size() * fix log output
1 parent 295e94d commit 1c63052

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/instantx.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,8 @@ bool CTxLockRequest::IsValid(bool fRequireUnspent) const
848848
return false;
849849
}
850850

851-
int64_t nValueIn = 0;
852-
int64_t nValueOut = 0;
851+
CAmount nValueIn = 0;
852+
CAmount nValueOut = 0;
853853

854854
BOOST_FOREACH(const CTxOut& txout, vout) {
855855
// InstantSend supports normal scripts and unspendable (i.e. data) scripts.
@@ -865,6 +865,8 @@ bool CTxLockRequest::IsValid(bool fRequireUnspent) const
865865

866866
CCoins coins;
867867
int nPrevoutHeight = 0;
868+
CAmount nValue = 0;
869+
868870
if(!pcoinsTip->GetCoins(txin.prevout.hash, coins) ||
869871
(unsigned int)txin.prevout.n>=coins.vout.size() ||
870872
coins.vout[txin.prevout.n].IsNull()) {
@@ -875,20 +877,29 @@ bool CTxLockRequest::IsValid(bool fRequireUnspent) const
875877
CTransaction txOutpointCreated;
876878
uint256 nHashOutpointConfirmed;
877879
if(!GetTransaction(txin.prevout.hash, txOutpointCreated, Params().GetConsensus(), nHashOutpointConfirmed, true) || nHashOutpointConfirmed == uint256()) {
878-
LogPrint("instantsend", "txLockRequest::IsValid -- Failed to find outpoint %s\n", txin.prevout.ToStringShort());
880+
LogPrint("instantsend", "CTxLockRequest::IsValid -- Failed to find outpoint %s\n", txin.prevout.ToStringShort());
881+
return false;
882+
}
883+
if(txin.prevout.n >= txOutpointCreated.vout.size()) {
884+
LogPrint("instantsend", "CTxLockRequest::IsValid -- Outpoint %s is out of bounds, size() = %lld\n",
885+
txin.prevout.ToStringShort(), txOutpointCreated.vout.size());
879886
return false;
880887
}
881-
LOCK(cs_main);
882888
BlockMap::iterator mi = mapBlockIndex.find(nHashOutpointConfirmed);
883-
if(mi == mapBlockIndex.end()) {
884-
// not on this chain?
885-
LogPrint("instantsend", "txLockRequest::IsValid -- Failed to find block %s for outpoint %s\n", nHashOutpointConfirmed.ToString(), txin.prevout.ToStringShort());
889+
if(mi == mapBlockIndex.end() || !mi->second) {
890+
// shouldn't happen
891+
LogPrint("instantsend", "CTxLockRequest::IsValid -- Failed to find block %s for outpoint %s\n",
892+
nHashOutpointConfirmed.ToString(), txin.prevout.ToStringShort());
886893
return false;
887894
}
888-
nPrevoutHeight = mi->second ? mi->second->nHeight : 0;
895+
nPrevoutHeight = mi->second->nHeight;
896+
nValue = txOutpointCreated.vout[txin.prevout.n].nValue;
897+
} else {
898+
nPrevoutHeight = coins.nHeight;
899+
nValue = coins.vout[txin.prevout.n].nValue;
889900
}
890901

891-
int nTxAge = chainActive.Height() - (nPrevoutHeight ? nPrevoutHeight : coins.nHeight) + 1;
902+
int nTxAge = chainActive.Height() - nPrevoutHeight + 1;
892903
// 1 less than the "send IX" gui requires, in case of a block propagating the network at the time
893904
int nConfirmationsRequired = INSTANTSEND_CONFIRMATIONS_REQUIRED - 1;
894905

@@ -898,7 +909,7 @@ bool CTxLockRequest::IsValid(bool fRequireUnspent) const
898909
return false;
899910
}
900911

901-
nValueIn += coins.vout[txin.prevout.n].nValue;
912+
nValueIn += nValue;
902913
}
903914

904915
if(nValueOut > sporkManager.GetSporkValue(SPORK_5_INSTANTSEND_MAX_VALUE)*COIN) {

0 commit comments

Comments
 (0)