LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue_get.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 95.0 % 40 38
Test Date: 2026-02-04 04:43:42 Functions: 100.0 % 9 9
Branches: 47.5 % 40 19

             Branch data     Line data    Source code
       1                 :             : // Copyright 2014 BitPay Inc.
       2                 :             : // Copyright 2015 Bitcoin Core Developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or https://opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #include <univalue.h>
       7                 :             : 
       8                 :             : #include <cstring>
       9                 :             : #include <locale>
      10                 :             : #include <sstream>
      11                 :             : #include <stdexcept>
      12                 :             : #include <string>
      13                 :             : #include <vector>
      14                 :             : 
      15                 :             : namespace
      16                 :             : {
      17                 :           6 : static bool ParsePrechecks(const std::string& str)
      18                 :             : {
      19         [ +  - ]:           6 :     if (str.empty()) // No empty string allowed
      20                 :             :         return false;
      21   [ -  +  +  -  :           6 :     if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
                   +  - ]
      22                 :             :         return false;
      23         [ -  + ]:           6 :     if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
      24                 :           0 :         return false;
      25                 :             :     return true;
      26                 :             : }
      27                 :             : 
      28                 :           6 : bool ParseDouble(const std::string& str, double *out)
      29                 :             : {
      30         [ +  - ]:           6 :     if (!ParsePrechecks(str))
      31                 :             :         return false;
      32   [ -  +  +  -  :           6 :     if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
             -  +  -  - ]
      33                 :             :         return false;
      34                 :           6 :     std::istringstream text(str);
      35   [ +  -  +  - ]:           6 :     text.imbue(std::locale::classic());
      36                 :           6 :     double result;
      37         [ +  - ]:           6 :     text >> result;
      38         [ +  - ]:           6 :     if(out) *out = result;
      39   [ +  -  -  + ]:           6 :     return text.eof() && !text.fail();
      40                 :           6 : }
      41                 :             : }
      42                 :             : 
      43                 :          26 : const std::vector<std::string>& UniValue::getKeys() const
      44                 :             : {
      45                 :          26 :     checkType(VOBJ);
      46                 :          25 :     return keys;
      47                 :             : }
      48                 :             : 
      49                 :          38 : const std::vector<UniValue>& UniValue::getValues() const
      50                 :             : {
      51         [ +  + ]:          38 :     if (typ != VOBJ && typ != VARR)
      52         [ +  - ]:           1 :         throw std::runtime_error("JSON value is not an object or array as expected");
      53                 :          37 :     return values;
      54                 :             : }
      55                 :             : 
      56                 :         271 : bool UniValue::get_bool() const
      57                 :             : {
      58                 :         271 :     checkType(VBOOL);
      59                 :         269 :     return isTrue();
      60                 :             : }
      61                 :             : 
      62                 :      134645 : const std::string& UniValue::get_str() const
      63                 :             : {
      64                 :      134645 :     checkType(VSTR);
      65                 :      134630 :     return getValStr();
      66                 :             : }
      67                 :             : 
      68                 :           6 : double UniValue::get_real() const
      69                 :             : {
      70                 :           6 :     checkType(VNUM);
      71                 :           6 :     double retval;
      72         [ -  + ]:           6 :     if (!ParseDouble(getValStr(), &retval))
      73         [ #  # ]:           0 :         throw std::runtime_error("JSON double out of range");
      74                 :           6 :     return retval;
      75                 :             : }
      76                 :             : 
      77                 :         171 : const UniValue& UniValue::get_obj() const
      78                 :             : {
      79                 :         171 :     checkType(VOBJ);
      80                 :         170 :     return *this;
      81                 :             : }
      82                 :             : 
      83                 :        1812 : const UniValue& UniValue::get_array() const
      84                 :             : {
      85                 :        1812 :     checkType(VARR);
      86                 :        1811 :     return *this;
      87                 :             : }
        

Generated by: LCOV version 2.0-1