Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/univalue/lib/univalue_get.cpp
Line
Count
Source
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
static bool ParsePrechecks(const std::string& str)
22
0
{
23
0
    if (str.empty()) // No empty string allowed
  Branch (23:9): [True: 0, False: 0]
24
0
        return false;
25
0
    if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
  Branch (25:9): [True: 0, False: 0]
  Branch (25:29): [True: 0, False: 0]
  Branch (25:53): [True: 0, False: 0]
26
0
        return false;
27
0
    if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
  Branch (27:9): [True: 0, False: 0]
28
0
        return false;
29
0
    return true;
30
0
}
31
32
bool ParseDouble(const std::string& str, double *out)
33
0
{
34
0
    if (!ParsePrechecks(str))
  Branch (34:9): [True: 0, False: 0]
35
0
        return false;
36
0
    if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
  Branch (36:9): [True: 0, False: 0]
  Branch (36:28): [True: 0, False: 0]
  Branch (36:45): [True: 0, False: 0]
37
0
        return false;
38
0
    std::istringstream text(str);
39
0
    text.imbue(std::locale::classic());
40
0
    double result;
41
0
    text >> result;
42
0
    if(out) *out = result;
  Branch (42:8): [True: 0, False: 0]
43
0
    return text.eof() && !text.fail();
  Branch (43:12): [True: 0, False: 0]
  Branch (43:26): [True: 0, False: 0]
44
0
}
45
}
46
47
const std::vector<std::string>& UniValue::getKeys() const
48
0
{
49
0
    checkType(VOBJ);
50
0
    return keys;
51
0
}
52
53
const std::vector<UniValue>& UniValue::getValues() const
54
0
{
55
0
    if (typ != VOBJ && typ != VARR)
  Branch (55:9): [True: 0, False: 0]
  Branch (55:24): [True: 0, False: 0]
56
0
        throw std::runtime_error("JSON value is not an object or array as expected");
57
0
    return values;
58
0
}
59
60
bool UniValue::get_bool() const
61
55.4k
{
62
55.4k
    checkType(VBOOL);
63
55.4k
    return isTrue();
64
55.4k
}
65
66
const std::string& UniValue::get_str() const
67
7.87M
{
68
7.87M
    checkType(VSTR);
69
7.87M
    return getValStr();
70
7.87M
}
71
72
double UniValue::get_real() const
73
0
{
74
0
    checkType(VNUM);
75
0
    double retval;
76
0
    if (!ParseDouble(getValStr(), &retval))
  Branch (76:9): [True: 0, False: 0]
77
0
        throw std::runtime_error("JSON double out of range");
78
0
    return retval;
79
0
}
80
81
const UniValue& UniValue::get_obj() const
82
2.35M
{
83
2.35M
    checkType(VOBJ);
84
2.35M
    return *this;
85
2.35M
}
86
87
const UniValue& UniValue::get_array() const
88
0
{
89
0
    checkType(VARR);
90
0
    return *this;
91
0
}