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

Generated by: LCOV version 2.0-1