Coverage Report

Created: 2026-08-14 20:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/consensus/tx_check.cpp
Line
Count
Source
1
// Copyright (c) 2017-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <consensus/tx_check.h>
6
7
#include <consensus/amount.h>
8
#include <consensus/consensus.h>
9
#include <consensus/validation.h>
10
#include <primitives/transaction.h>
11
#include <script/script.h>
12
#include <serialize.h>
13
14
#include <set>
15
#include <string>
16
#include <utility>
17
#include <vector>
18
19
bool CheckTransaction(const CTransaction& tx, TxValidationState& state)
20
367k
{
21
    // Basic checks that don't depend on any context
22
367k
    if (tx.vin.empty())
23
4
        return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty");
24
367k
    if (tx.vout.empty())
25
5
        return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty");
26
    // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
27
367k
    if (::GetSerializeSize(TX_NO_WITNESS(tx)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) {
28
2
        return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");
29
2
    }
30
31
    // Check for negative or overflow output values (see CVE-2010-5139)
32
367k
    CAmount nValueOut = 0;
33
367k
    for (const auto& txout : tx.vout)
34
894k
    {
35
894k
        if (txout.nValue < 0)
36
5
            return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative");
37
894k
        if (txout.nValue > MAX_MONEY)
38
5
            return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge");
39
894k
        nValueOut += txout.nValue;
40
894k
        if (!MoneyRange(nValueOut))
41
5
            return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge");
42
894k
    }
43
44
    // Check for duplicate inputs (see CVE-2018-17144)
45
    // While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs
46
    // of a tx as spent, it does not check if the tx has duplicate inputs.
47
    // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
48
    // the underlying coins database.
49
367k
    std::set<COutPoint> vInOutPoints;
50
447k
    for (const auto& txin : tx.vin) {
51
447k
        if (!vInOutPoints.insert(txin.prevout).second)
52
232
            return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
53
447k
    }
54
55
367k
    if (tx.IsCoinBase())
56
235k
    {
57
235k
        if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
58
5
            return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
59
235k
    }
60
131k
    else
61
131k
    {
62
131k
        for (const auto& txin : tx.vin)
63
211k
            if (txin.prevout.IsNull())
64
6
                return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
65
131k
    }
66
67
367k
    return true;
68
367k
}