LCOV - code coverage report
Current view: top level - src/script - script.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 89.7 % 263 236
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 23 23
Branches: 78.4 % 366 287

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #include <script/script.h>
       7                 :             : 
       8                 :             : #include <crypto/common.h>
       9                 :             : #include <crypto/hex_base.h>
      10                 :             : #include <hash.h>
      11                 :             : #include <memusage.h>
      12                 :             : #include <streams.h>
      13                 :             : #include <uint256.h>
      14                 :             : #include <util/check.h>
      15                 :             : #include <util/hash_type.h>
      16                 :             : #include <util/string.h>
      17                 :             : 
      18                 :             : #include <cassert>
      19                 :             : #include <compare>
      20                 :             : #include <limits>
      21                 :             : #include <memory>
      22                 :             : #include <span>
      23                 :             : #include <stdexcept>
      24                 :             : #include <string>
      25                 :             : #include <vector>
      26                 :             : 
      27                 :       81922 : CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {}
      28                 :             : 
      29                 :         460 : std::string GetOpName(opcodetype opcode)
      30                 :             : {
      31   [ -  -  -  -  :         460 :     switch (opcode)
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
             +  +  -  -  
                      - ]
      32                 :             :     {
      33                 :             :     // push value
      34                 :           0 :     case OP_0                      : return "0";
      35                 :           0 :     case OP_PUSHDATA1              : return "OP_PUSHDATA1";
      36                 :           0 :     case OP_PUSHDATA2              : return "OP_PUSHDATA2";
      37                 :           0 :     case OP_PUSHDATA4              : return "OP_PUSHDATA4";
      38                 :           0 :     case OP_1NEGATE                : return "-1";
      39                 :           3 :     case OP_RESERVED               : return "OP_RESERVED";
      40                 :           0 :     case OP_1                      : return "1";
      41                 :           0 :     case OP_2                      : return "2";
      42                 :           0 :     case OP_3                      : return "3";
      43                 :           0 :     case OP_4                      : return "4";
      44                 :           0 :     case OP_5                      : return "5";
      45                 :           0 :     case OP_6                      : return "6";
      46                 :           0 :     case OP_7                      : return "7";
      47                 :           0 :     case OP_8                      : return "8";
      48                 :           0 :     case OP_9                      : return "9";
      49                 :           0 :     case OP_10                     : return "10";
      50                 :           0 :     case OP_11                     : return "11";
      51                 :           0 :     case OP_12                     : return "12";
      52                 :           0 :     case OP_13                     : return "13";
      53                 :           0 :     case OP_14                     : return "14";
      54                 :           0 :     case OP_15                     : return "15";
      55                 :           0 :     case OP_16                     : return "16";
      56                 :             : 
      57                 :             :     // control
      58                 :           3 :     case OP_NOP                    : return "OP_NOP";
      59                 :           3 :     case OP_VER                    : return "OP_VER";
      60                 :           3 :     case OP_IF                     : return "OP_IF";
      61                 :           3 :     case OP_NOTIF                  : return "OP_NOTIF";
      62                 :           3 :     case OP_VERIF                  : return "OP_VERIF";
      63                 :           3 :     case OP_VERNOTIF               : return "OP_VERNOTIF";
      64                 :           3 :     case OP_ELSE                   : return "OP_ELSE";
      65                 :           3 :     case OP_ENDIF                  : return "OP_ENDIF";
      66                 :           3 :     case OP_VERIFY                 : return "OP_VERIFY";
      67                 :           3 :     case OP_RETURN                 : return "OP_RETURN";
      68                 :             : 
      69                 :             :     // stack ops
      70                 :           3 :     case OP_TOALTSTACK             : return "OP_TOALTSTACK";
      71                 :           3 :     case OP_FROMALTSTACK           : return "OP_FROMALTSTACK";
      72                 :           3 :     case OP_2DROP                  : return "OP_2DROP";
      73                 :           3 :     case OP_2DUP                   : return "OP_2DUP";
      74                 :           3 :     case OP_3DUP                   : return "OP_3DUP";
      75                 :           3 :     case OP_2OVER                  : return "OP_2OVER";
      76                 :           3 :     case OP_2ROT                   : return "OP_2ROT";
      77                 :           3 :     case OP_2SWAP                  : return "OP_2SWAP";
      78                 :           3 :     case OP_IFDUP                  : return "OP_IFDUP";
      79                 :           3 :     case OP_DEPTH                  : return "OP_DEPTH";
      80                 :           3 :     case OP_DROP                   : return "OP_DROP";
      81                 :           9 :     case OP_DUP                    : return "OP_DUP";
      82                 :           3 :     case OP_NIP                    : return "OP_NIP";
      83                 :           3 :     case OP_OVER                   : return "OP_OVER";
      84                 :           3 :     case OP_PICK                   : return "OP_PICK";
      85                 :           3 :     case OP_ROLL                   : return "OP_ROLL";
      86                 :           3 :     case OP_ROT                    : return "OP_ROT";
      87                 :           3 :     case OP_SWAP                   : return "OP_SWAP";
      88                 :           3 :     case OP_TUCK                   : return "OP_TUCK";
      89                 :             : 
      90                 :             :     // splice ops
      91                 :           3 :     case OP_CAT                    : return "OP_CAT";
      92                 :           3 :     case OP_SUBSTR                 : return "OP_SUBSTR";
      93                 :           3 :     case OP_LEFT                   : return "OP_LEFT";
      94                 :           3 :     case OP_RIGHT                  : return "OP_RIGHT";
      95                 :           3 :     case OP_SIZE                   : return "OP_SIZE";
      96                 :             : 
      97                 :             :     // bit logic
      98                 :           3 :     case OP_INVERT                 : return "OP_INVERT";
      99                 :           3 :     case OP_AND                    : return "OP_AND";
     100                 :           3 :     case OP_OR                     : return "OP_OR";
     101                 :           3 :     case OP_XOR                    : return "OP_XOR";
     102                 :          39 :     case OP_EQUAL                  : return "OP_EQUAL";
     103                 :           7 :     case OP_EQUALVERIFY            : return "OP_EQUALVERIFY";
     104                 :           3 :     case OP_RESERVED1              : return "OP_RESERVED1";
     105                 :           3 :     case OP_RESERVED2              : return "OP_RESERVED2";
     106                 :             : 
     107                 :             :     // numeric
     108                 :           3 :     case OP_1ADD                   : return "OP_1ADD";
     109                 :           3 :     case OP_1SUB                   : return "OP_1SUB";
     110                 :           3 :     case OP_2MUL                   : return "OP_2MUL";
     111                 :           3 :     case OP_2DIV                   : return "OP_2DIV";
     112                 :           3 :     case OP_NEGATE                 : return "OP_NEGATE";
     113                 :           3 :     case OP_ABS                    : return "OP_ABS";
     114                 :          27 :     case OP_NOT                    : return "OP_NOT";
     115                 :           3 :     case OP_0NOTEQUAL              : return "OP_0NOTEQUAL";
     116                 :           3 :     case OP_ADD                    : return "OP_ADD";
     117                 :           3 :     case OP_SUB                    : return "OP_SUB";
     118                 :           3 :     case OP_MUL                    : return "OP_MUL";
     119                 :           3 :     case OP_DIV                    : return "OP_DIV";
     120                 :           3 :     case OP_MOD                    : return "OP_MOD";
     121                 :           3 :     case OP_LSHIFT                 : return "OP_LSHIFT";
     122                 :           3 :     case OP_RSHIFT                 : return "OP_RSHIFT";
     123                 :           3 :     case OP_BOOLAND                : return "OP_BOOLAND";
     124                 :           3 :     case OP_BOOLOR                 : return "OP_BOOLOR";
     125                 :           3 :     case OP_NUMEQUAL               : return "OP_NUMEQUAL";
     126                 :           3 :     case OP_NUMEQUALVERIFY         : return "OP_NUMEQUALVERIFY";
     127                 :           3 :     case OP_NUMNOTEQUAL            : return "OP_NUMNOTEQUAL";
     128                 :           3 :     case OP_LESSTHAN               : return "OP_LESSTHAN";
     129                 :           3 :     case OP_GREATERTHAN            : return "OP_GREATERTHAN";
     130                 :           3 :     case OP_LESSTHANOREQUAL        : return "OP_LESSTHANOREQUAL";
     131                 :           3 :     case OP_GREATERTHANOREQUAL     : return "OP_GREATERTHANOREQUAL";
     132                 :           3 :     case OP_MIN                    : return "OP_MIN";
     133                 :           3 :     case OP_MAX                    : return "OP_MAX";
     134                 :           3 :     case OP_WITHIN                 : return "OP_WITHIN";
     135                 :             : 
     136                 :             :     // crypto
     137                 :           3 :     case OP_RIPEMD160              : return "OP_RIPEMD160";
     138                 :           3 :     case OP_SHA1                   : return "OP_SHA1";
     139                 :           3 :     case OP_SHA256                 : return "OP_SHA256";
     140                 :          43 :     case OP_HASH160                : return "OP_HASH160";
     141                 :           3 :     case OP_HASH256                : return "OP_HASH256";
     142                 :           3 :     case OP_CODESEPARATOR          : return "OP_CODESEPARATOR";
     143                 :          51 :     case OP_CHECKSIG               : return "OP_CHECKSIG";
     144                 :           3 :     case OP_CHECKSIGVERIFY         : return "OP_CHECKSIGVERIFY";
     145                 :          27 :     case OP_CHECKMULTISIG          : return "OP_CHECKMULTISIG";
     146                 :           3 :     case OP_CHECKMULTISIGVERIFY    : return "OP_CHECKMULTISIGVERIFY";
     147                 :             : 
     148                 :             :     // expansion
     149                 :           3 :     case OP_NOP1                   : return "OP_NOP1";
     150                 :           7 :     case OP_CHECKLOCKTIMEVERIFY    : return "OP_CHECKLOCKTIMEVERIFY";
     151                 :           3 :     case OP_CHECKSEQUENCEVERIFY    : return "OP_CHECKSEQUENCEVERIFY";
     152                 :           3 :     case OP_NOP4                   : return "OP_NOP4";
     153                 :           3 :     case OP_NOP5                   : return "OP_NOP5";
     154                 :           3 :     case OP_NOP6                   : return "OP_NOP6";
     155                 :           3 :     case OP_NOP7                   : return "OP_NOP7";
     156                 :           7 :     case OP_NOP8                   : return "OP_NOP8";
     157                 :           3 :     case OP_NOP9                   : return "OP_NOP9";
     158                 :           3 :     case OP_NOP10                  : return "OP_NOP10";
     159                 :             : 
     160                 :             :     // Opcode added by BIP 342 (Tapscript)
     161                 :           0 :     case OP_CHECKSIGADD            : return "OP_CHECKSIGADD";
     162                 :             : 
     163                 :           0 :     case OP_INVALIDOPCODE          : return "OP_INVALIDOPCODE";
     164                 :             : 
     165                 :             :     } // no default case, so the compiler can warn about missing cases
     166                 :           0 :     return "OP_UNKNOWN";
     167                 :             : }
     168                 :             : 
     169                 :      173136 : unsigned int CScript::GetSigOpCount(bool fAccurate) const
     170                 :             : {
     171                 :      173136 :     unsigned int n = 0;
     172         [ +  + ]:      173136 :     const_iterator pc = begin();
     173                 :      173136 :     opcodetype lastOpcode = OP_INVALIDOPCODE;
     174   [ +  +  +  + ]:     1195204 :     while (pc < end())
     175                 :             :     {
     176                 :      424466 :         opcodetype opcode;
     177         [ +  - ]:      424466 :         if (!GetOp(pc, opcode))
     178                 :             :             break;
     179         [ +  + ]:      424466 :         if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
     180                 :       71200 :             n++;
     181         [ +  + ]:      353266 :         else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
     182                 :             :         {
     183   [ +  +  +  + ]:        7171 :             if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
     184                 :          21 :                 n += DecodeOP_N(lastOpcode);
     185                 :             :             else
     186                 :        7150 :                 n += MAX_PUBKEYS_PER_MULTISIG;
     187                 :             :         }
     188                 :             :         lastOpcode = opcode;
     189                 :             :     }
     190                 :      173136 :     return n;
     191                 :             : }
     192                 :             : 
     193                 :        1375 : unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
     194                 :             : {
     195         [ +  + ]:        1375 :     if (!IsPayToScriptHash())
     196                 :         188 :         return GetSigOpCount(true);
     197                 :             : 
     198                 :             :     // This is a pay-to-script-hash scriptPubKey;
     199                 :             :     // get the last item that the scriptSig
     200                 :             :     // pushes onto the stack:
     201         [ +  + ]:        1187 :     const_iterator pc = scriptSig.begin();
     202                 :        1187 :     std::vector<unsigned char> vData;
     203   [ +  +  +  + ]:        4770 :     while (pc < scriptSig.end())
     204                 :             :     {
     205                 :        1199 :         opcodetype opcode;
     206   [ +  -  +  - ]:        1199 :         if (!scriptSig.GetOp(pc, opcode, vData))
     207                 :             :             return 0;
     208         [ +  + ]:        1199 :         if (opcode > OP_16)
     209                 :             :             return 0;
     210                 :             :     }
     211                 :             : 
     212                 :             :     /// ... and return its opcount:
     213                 :        1186 :     CScript subscript(vData.begin(), vData.end());
     214         [ +  - ]:        1186 :     return subscript.GetSigOpCount(true);
     215                 :        2373 : }
     216                 :             : 
     217                 :         162 : bool CScript::IsPayToAnchor() const
     218                 :             : {
     219         [ +  + ]:         162 :     return (this->size() == 4 &&
     220   [ +  +  +  + ]:          94 :         (*this)[0] == OP_1 &&
     221   [ +  +  +  - ]:          26 :         (*this)[1] == 0x02 &&
     222   [ +  +  +  +  :         188 :         (*this)[2] == 0x4e &&
                   +  + ]
     223   [ +  +  -  + ]:          16 :         (*this)[3] == 0x73);
     224                 :             : }
     225                 :             : 
     226                 :         708 : bool CScript::IsPayToAnchor(int version, const std::vector<unsigned char>& program)
     227                 :             : {
     228                 :        1232 :     return version == 1 &&
     229         [ +  + ]:         524 :         program.size() == 2 &&
     230   [ +  +  +  + ]:         717 :         program[0] == 0x4e &&
     231         [ -  + ]:           8 :         program[1] == 0x73;
     232                 :             : }
     233                 :             : 
     234                 :      194058 : bool CScript::IsPayToScriptHash() const
     235                 :             : {
     236                 :             :     // Extra-fast test for pay-to-script-hash CScripts:
     237         [ +  + ]:      194058 :     return (this->size() == 23 &&
     238   [ +  -  +  + ]:       87220 :             (*this)[0] == OP_HASH160 &&
     239   [ +  +  +  -  :      279426 :             (*this)[1] == 0x14 &&
                   +  - ]
     240   [ +  -  +  + ]:       85368 :             (*this)[22] == OP_EQUAL);
     241                 :             : }
     242                 :             : 
     243                 :          12 : bool CScript::IsPayToWitnessScriptHash() const
     244                 :             : {
     245                 :             :     // Extra-fast test for pay-to-witness-script-hash CScripts:
     246         [ +  + ]:          12 :     return (this->size() == 34 &&
     247   [ +  +  +  -  :          16 :             (*this)[0] == OP_0 &&
                   +  + ]
     248   [ +  -  -  + ]:           6 :             (*this)[1] == 0x20);
     249                 :             : }
     250                 :             : 
     251                 :          12 : bool CScript::IsPayToTaproot() const
     252                 :             : {
     253         [ -  + ]:          12 :     return (this->size() == 34 &&
     254   [ -  +  -  -  :          12 :             (*this)[0] == OP_1 &&
                   -  - ]
     255   [ #  #  #  # ]:           0 :             (*this)[1] == 0x20);
     256                 :             : }
     257                 :             : 
     258                 :             : // A witness program is any valid CScript that consists of a 1-byte push opcode
     259                 :             : // followed by a data push between 2 and 40 bytes.
     260                 :      169799 : bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
     261                 :             : {
     262   [ +  +  +  +  :      181756 :     if (this->size() < 4 || this->size() > 42) {
             +  +  +  + ]
     263                 :             :         return false;
     264                 :             :     }
     265   [ +  +  +  +  :      328578 :     if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
             +  +  +  + ]
     266                 :             :         return false;
     267                 :             :     }
     268   [ +  +  +  + ]:      205650 :     if ((size_t)((*this)[1] + 2) == this->size()) {
     269                 :      102636 :         version = DecodeOP_N((opcodetype)(*this)[0]);
     270         [ +  + ]:      307908 :         program = std::vector<unsigned char>(this->begin() + 2, this->end());
     271                 :      102636 :         return true;
     272                 :             :     }
     273                 :             :     return false;
     274                 :             : }
     275                 :             : 
     276                 :      227595 : bool CScript::IsPushOnly(const_iterator pc) const
     277                 :             : {
     278   [ +  +  +  + ]:     2591566 :     while (pc < end())
     279                 :             :     {
     280                 :     1078215 :         opcodetype opcode;
     281         [ +  + ]:     1078215 :         if (!GetOp(pc, opcode))
     282                 :             :             return false;
     283                 :             :         // Note that IsPushOnly() *does* consider OP_RESERVED to be a
     284                 :             :         // push-type opcode, however execution of OP_RESERVED fails, so
     285                 :             :         // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
     286                 :             :         // the P2SH special validation code being executed.
     287         [ +  + ]:     1077913 :         if (opcode > OP_16)
     288                 :             :             return false;
     289                 :             :     }
     290                 :             :     return true;
     291                 :             : }
     292                 :             : 
     293                 :      227570 : bool CScript::IsPushOnly() const
     294                 :             : {
     295         [ +  + ]:      455140 :     return this->IsPushOnly(begin());
     296                 :             : }
     297                 :             : 
     298                 :           8 : std::string CScriptWitness::ToString() const
     299                 :             : {
     300   [ +  -  +  - ]:          20 :     return "CScriptWitness(" + util::Join(*this, ", ", [](auto element) { return HexStr(element); }) + ")";
     301                 :             : }
     302                 :             : 
     303         [ +  + ]:       41616 : CScriptWitness::CScriptWitness(const std::vector<std::vector<unsigned char>>& stack)
     304                 :             : {
     305         [ +  + ]:       41616 :     if (stack.empty()) return;
     306         [ +  - ]:       82004 :     SpanWriter{Allocate(::GetSerializeSize(stack)), stack};
     307         [ -  - ]:           0 : }
     308                 :             : 
     309                 :       40388 : std::span<std::byte> CScriptWitness::Allocate(uint64_t size)
     310                 :             : {
     311                 :             :     // The header stores the size of the serialization in 32 bits. Together with the header, it must also fit in a
     312                 :             :     // size_t, which may be 32 bits too.
     313   [ -  +  -  - ]:       40388 :     if (size > std::numeric_limits<uint32_t>::max() - HEADER_SIZE) throw std::length_error("CScriptWitness too large");
     314         [ -  + ]:       40388 :     m_data = std::make_unique_for_overwrite<std::byte[]>(HEADER_SIZE + size);
     315                 :       40388 :     WriteLE32(m_data.get(), size);
     316                 :       40388 :     return {m_data.get() + HEADER_SIZE, static_cast<size_t>(size)};
     317                 :             : }
     318                 :             : 
     319         [ +  + ]:      806537 : CScriptWitness::CScriptWitness(const CScriptWitness& other)
     320                 :             : {
     321         [ +  + ]:      806537 :     if (other.empty()) return;
     322                 :       38776 :     const auto total = HEADER_SIZE + other.Data().size();
     323                 :       38776 :     m_data = std::make_unique_for_overwrite<std::byte[]>(total);
     324                 :       38776 :     std::copy_n(other.m_data.get(), total, m_data.get());
     325                 :             : }
     326                 :             : 
     327                 :       44082 : std::span<const unsigned char> CScriptWitness::operator[](size_t index) const noexcept
     328                 :             : {
     329                 :       44082 :     Assume(index < size());
     330                 :       44082 :     auto it = begin();
     331         [ +  + ]:      796656 :     for (size_t i = 0; i < index; ++i) ++it;
     332                 :       44082 :     return *it;
     333                 :             : }
     334                 :             : 
     335                 :       82803 : std::vector<std::vector<unsigned char>> CScriptWitness::ToVectors() const
     336                 :             : {
     337                 :       82803 :     std::vector<std::vector<unsigned char>> ret;
     338         [ +  - ]:       82803 :     ret.reserve(size());
     339   [ +  -  +  + ]:      355134 :     for (const auto element : *this) ret.emplace_back(element.begin(), element.end());
     340                 :       82803 :     return ret;
     341                 :           0 : }
     342                 :             : 
     343                 :      220927 : size_t CScriptWitness::DynamicMemoryUsage() const noexcept
     344                 :             : {
     345   [ +  +  +  - ]:      220927 :     return m_data ? memusage::MallocUsage(HEADER_SIZE + Data().size()) : 0;
     346                 :             : }
     347                 :             : 
     348                 :          13 : bool CScript::HasValidOps() const
     349                 :             : {
     350         [ +  + ]:          13 :     CScript::const_iterator it = begin();
     351   [ +  +  +  + ]:          90 :     while (it < end()) {
     352                 :          35 :         opcodetype opcode;
     353                 :          35 :         std::vector<unsigned char> item;
     354   [ +  -  +  +  :          67 :         if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
             +  +  -  + ]
     355                 :           3 :             return false;
     356                 :             :         }
     357                 :          35 :     }
     358                 :             :     return true;
     359                 :             : }
     360                 :             : 
     361                 :     8691405 : bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
     362                 :             : {
     363                 :     8691405 :     opcodeRet = OP_INVALIDOPCODE;
     364         [ +  + ]:     8691405 :     if (pvchRet)
     365         [ +  + ]:     4690037 :         pvchRet->clear();
     366   [ +  +  +  + ]:    17382810 :     if (pc >= end)
     367                 :             :         return false;
     368                 :             : 
     369                 :             :     // Read instruction
     370         [ +  - ]:     8332040 :     if (end - pc < 1)
     371                 :             :         return false;
     372         [ +  + ]:     8332040 :     unsigned int opcode = *pc++;
     373                 :             : 
     374                 :             :     // Immediate operand
     375         [ +  + ]:     8332040 :     if (opcode <= OP_PUSHDATA4)
     376                 :             :     {
     377                 :     4205122 :         unsigned int nSize = 0;
     378         [ +  + ]:     4205122 :         if (opcode < OP_PUSHDATA1)
     379                 :             :         {
     380                 :             :             nSize = opcode;
     381                 :             :         }
     382         [ +  + ]:       30279 :         else if (opcode == OP_PUSHDATA1)
     383                 :             :         {
     384         [ +  - ]:        9306 :             if (end - pc < 1)
     385                 :             :                 return false;
     386                 :        9306 :             nSize = *pc++;
     387                 :             :         }
     388         [ +  + ]:       20973 :         else if (opcode == OP_PUSHDATA2)
     389                 :             :         {
     390         [ +  - ]:       20050 :             if (end - pc < 2)
     391                 :             :                 return false;
     392                 :       20050 :             nSize = ReadLE16(&pc[0]);
     393                 :       20050 :             pc += 2;
     394                 :             :         }
     395         [ +  - ]:         923 :         else if (opcode == OP_PUSHDATA4)
     396                 :             :         {
     397         [ +  - ]:         923 :             if (end - pc < 4)
     398                 :             :                 return false;
     399                 :         923 :             nSize = ReadLE32(&pc[0]);
     400                 :         923 :             pc += 4;
     401                 :             :         }
     402   [ +  -  +  + ]:     4205122 :         if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
     403                 :         591 :             return false;
     404         [ +  + ]:     4204531 :         if (pvchRet)
     405                 :     2593411 :             pvchRet->assign(pc, pc + nSize);
     406                 :     4204531 :         pc += nSize;
     407                 :             :     }
     408                 :             : 
     409                 :     8331449 :     opcodeRet = static_cast<opcodetype>(opcode);
     410                 :     8331449 :     return true;
     411                 :             : }
     412                 :             : 
     413                 :       48492 : bool IsOpSuccess(const opcodetype& opcode)
     414                 :             : {
     415   [ +  -  +  - ]:       48492 :     return opcode == 80 || opcode == 98 || (opcode >= 126 && opcode <= 129) ||
     416   [ +  -  +  - ]:       48492 :            (opcode >= 131 && opcode <= 134) || (opcode >= 137 && opcode <= 138) ||
     417   [ +  -  +  -  :       96984 :            (opcode >= 141 && opcode <= 142) || (opcode >= 149 && opcode <= 153) ||
                   +  - ]
     418         [ +  - ]:       48492 :            (opcode >= 187 && opcode <= 254);
     419                 :             : }
     420                 :             : 
     421                 :      716519 : bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode) {
     422                 :             :     // Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal
     423         [ -  + ]:      716519 :     assert(0 <= opcode && opcode <= OP_PUSHDATA4);
     424   [ -  +  +  + ]:      716519 :     if (data.size() == 0) {
     425                 :             :         // Should have used OP_0.
     426                 :      406793 :         return opcode == OP_0;
     427   [ +  +  +  +  :      309726 :     } else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
                   +  + ]
     428                 :             :         // Should have used OP_1 .. OP_16.
     429                 :             :         return false;
     430   [ +  +  +  + ]:      307654 :     } else if (data.size() == 1 && data[0] == 0x81) {
     431                 :             :         // Should have used OP_1NEGATE.
     432                 :             :         return false;
     433         [ +  + ]:      307423 :     } else if (data.size() <= 75) {
     434                 :             :         // Must have used a direct push (opcode indicating number of bytes pushed + those bytes).
     435                 :      306362 :         return opcode == data.size();
     436         [ +  + ]:        1061 :     } else if (data.size() <= 255) {
     437                 :             :         // Must have used OP_PUSHDATA.
     438                 :         660 :         return opcode == OP_PUSHDATA1;
     439         [ +  - ]:         401 :     } else if (data.size() <= 65535) {
     440                 :             :         // Must have used OP_PUSHDATA2.
     441                 :         401 :         return opcode == OP_PUSHDATA2;
     442                 :             :     }
     443                 :             :     return true;
     444                 :             : }
        

Generated by: LCOV version 2.0-1