LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue_get.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 88.1 % 42 37
Test Date: 2026-04-21 06:38:47 Functions: 100.0 % 9 9
Branches: 50.0 % 38 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 <optional>
      11                 :             : #include <sstream>
      12                 :             : #include <stdexcept>
      13                 :             : #include <string>
      14                 :             : #include <vector>
      15                 :             : 
      16                 :             : namespace
      17                 :             : {
      18                 :          29 : static bool ParsePrechecks(const std::string& str)
      19                 :             : {
      20         [ +  - ]:          29 :     if (str.empty()) // No empty string allowed
      21                 :             :         return false;
      22   [ -  +  +  -  :          29 :     if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
                   +  - ]
      23                 :             :         return false;
      24         [ -  + ]:          29 :     if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
      25                 :           0 :         return false;
      26                 :             :     return true;
      27                 :             : }
      28                 :             : 
      29                 :          29 : std::optional<double> ParseDouble(const std::string& str)
      30                 :             : {
      31         [ -  + ]:          29 :     if (!ParsePrechecks(str))
      32                 :           0 :         return std::nullopt;
      33   [ -  +  +  +  :          29 :     if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
             -  +  -  - ]
      34                 :           0 :         return std::nullopt;
      35                 :          29 :     std::istringstream text(str);
      36   [ +  -  +  - ]:          29 :     text.imbue(std::locale::classic());
      37                 :          29 :     double result;
      38         [ +  - ]:          29 :     text >> result;
      39   [ +  -  +  - ]:          29 :     if (!text.eof() || text.fail()) {
      40                 :           0 :         return std::nullopt;
      41                 :             :     }
      42                 :          29 :     return result;
      43                 :          29 : }
      44                 :             : }
      45                 :             : 
      46                 :      109339 : const std::vector<std::string>& UniValue::getKeys() const
      47                 :             : {
      48                 :      109339 :     checkType(VOBJ);
      49                 :      109338 :     return keys;
      50                 :             : }
      51                 :             : 
      52                 :      110215 : const std::vector<UniValue>& UniValue::getValues() const
      53                 :             : {
      54         [ +  + ]:      110215 :     if (typ != VOBJ && typ != VARR)
      55         [ +  - ]:           1 :         throw std::runtime_error("JSON value is not an object or array as expected");
      56                 :      110214 :     return values;
      57                 :             : }
      58                 :             : 
      59                 :       11864 : bool UniValue::get_bool() const
      60                 :             : {
      61                 :       11864 :     checkType(VBOOL);
      62                 :       11862 :     return isTrue();
      63                 :             : }
      64                 :             : 
      65                 :     1254442 : const std::string& UniValue::get_str() const
      66                 :             : {
      67                 :     1254442 :     checkType(VSTR);
      68                 :     1254425 :     return getValStr();
      69                 :             : }
      70                 :             : 
      71                 :          29 : double UniValue::get_real() const
      72                 :             : {
      73                 :          29 :     checkType(VNUM);
      74         [ +  - ]:          29 :     if (const auto retval{ParseDouble(getValStr())}) {
      75                 :          29 :         return *retval;
      76                 :             :     }
      77         [ #  # ]:           0 :     throw std::runtime_error("JSON double out of range");
      78                 :             : }
      79                 :             : 
      80                 :      840134 : const UniValue& UniValue::get_obj() const
      81                 :             : {
      82                 :      840134 :     checkType(VOBJ);
      83                 :      840132 :     return *this;
      84                 :             : }
      85                 :             : 
      86                 :     2327300 : const UniValue& UniValue::get_array() const
      87                 :             : {
      88                 :     2327300 :     checkType(VARR);
      89                 :     2327298 :     return *this;
      90                 :             : }
        

Generated by: LCOV version 2.0-1