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: 88.1 % 42 37
Test Date: 2026-04-07 04:31:02 Functions: 100.0 % 9 9
Branches: 47.4 % 38 18

             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 <optional>
      11                 :             : #include <sstream>
      12                 :             : #include <stdexcept>
      13                 :             : #include <string>
      14                 :             : #include <vector>
      15                 :             : 
      16                 :             : namespace
      17                 :             : {
      18                 :           6 : static bool ParsePrechecks(const std::string& str)
      19                 :             : {
      20         [ +  - ]:           6 :     if (str.empty()) // No empty string allowed
      21                 :             :         return false;
      22   [ -  +  +  -  :           6 :     if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
                   +  - ]
      23                 :             :         return false;
      24         [ -  + ]:           6 :     if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
      25                 :           0 :         return false;
      26                 :             :     return true;
      27                 :             : }
      28                 :             : 
      29                 :           6 : std::optional<double> ParseDouble(const std::string& str)
      30                 :             : {
      31         [ -  + ]:           6 :     if (!ParsePrechecks(str))
      32                 :           0 :         return std::nullopt;
      33   [ -  +  +  -  :           6 :     if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
             -  +  -  - ]
      34                 :           0 :         return std::nullopt;
      35                 :           6 :     std::istringstream text(str);
      36   [ +  -  +  - ]:           6 :     text.imbue(std::locale::classic());
      37                 :           6 :     double result;
      38         [ +  - ]:           6 :     text >> result;
      39   [ +  -  +  - ]:           6 :     if (!text.eof() || text.fail()) {
      40                 :           0 :         return std::nullopt;
      41                 :             :     }
      42                 :           6 :     return result;
      43                 :           6 : }
      44                 :             : }
      45                 :             : 
      46                 :          26 : const std::vector<std::string>& UniValue::getKeys() const
      47                 :             : {
      48                 :          26 :     checkType(VOBJ);
      49                 :          25 :     return keys;
      50                 :             : }
      51                 :             : 
      52                 :          38 : const std::vector<UniValue>& UniValue::getValues() const
      53                 :             : {
      54         [ +  + ]:          38 :     if (typ != VOBJ && typ != VARR)
      55         [ +  - ]:           1 :         throw std::runtime_error("JSON value is not an object or array as expected");
      56                 :          37 :     return values;
      57                 :             : }
      58                 :             : 
      59                 :         271 : bool UniValue::get_bool() const
      60                 :             : {
      61                 :         271 :     checkType(VBOOL);
      62                 :         269 :     return isTrue();
      63                 :             : }
      64                 :             : 
      65                 :      134871 : const std::string& UniValue::get_str() const
      66                 :             : {
      67                 :      134871 :     checkType(VSTR);
      68                 :      134856 :     return getValStr();
      69                 :             : }
      70                 :             : 
      71                 :           6 : double UniValue::get_real() const
      72                 :             : {
      73                 :           6 :     checkType(VNUM);
      74         [ +  - ]:           6 :     if (const auto retval{ParseDouble(getValStr())}) {
      75                 :           6 :         return *retval;
      76                 :             :     }
      77         [ #  # ]:           0 :     throw std::runtime_error("JSON double out of range");
      78                 :             : }
      79                 :             : 
      80                 :         171 : const UniValue& UniValue::get_obj() const
      81                 :             : {
      82                 :         171 :     checkType(VOBJ);
      83                 :         170 :     return *this;
      84                 :             : }
      85                 :             : 
      86                 :        1816 : const UniValue& UniValue::get_array() const
      87                 :             : {
      88                 :        1816 :     checkType(VARR);
      89                 :        1815 :     return *this;
      90                 :             : }
        

Generated by: LCOV version 2.0-1