Coverage Report

Created: 2026-06-01 18:35

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