Bitcoin Core Fuzz Coverage Report

Coverage Report

Created: 2026-03-24 13:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/univalue/lib/univalue.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 <iomanip>
9
#include <map>
10
#include <sstream>
11
#include <string>
12
#include <utility>
13
#include <vector>
14
15
const UniValue NullUniValue;
16
17
void UniValue::clear()
18
0
{
19
0
    typ = VNULL;
20
0
    val.clear();
21
0
    keys.clear();
22
0
    values.clear();
23
0
}
24
25
void UniValue::setNull()
26
0
{
27
0
    clear();
28
0
}
29
30
void UniValue::setBool(bool val_)
31
0
{
32
0
    clear();
33
0
    typ = VBOOL;
34
0
    if (val_)
35
0
        val = "1";
36
0
}
37
38
static bool validNumStr(const std::string& s)
39
0
{
40
0
    std::string tokenVal;
41
0
    unsigned int consumed;
42
0
    enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
43
0
    return (tt == JTOK_NUMBER);
44
0
}
45
46
void UniValue::setNumStr(std::string str)
47
0
{
48
0
    if (!validNumStr(str)) {
49
0
        throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};
50
0
    }
51
52
0
    clear();
53
0
    typ = VNUM;
54
0
    val = std::move(str);
55
0
}
56
57
void UniValue::setInt(uint64_t val_)
58
0
{
59
0
    std::ostringstream oss;
60
61
0
    oss << val_;
62
63
0
    return setNumStr(oss.str());
64
0
}
65
66
void UniValue::setInt(int64_t val_)
67
0
{
68
0
    std::ostringstream oss;
69
70
0
    oss << val_;
71
72
0
    return setNumStr(oss.str());
73
0
}
74
75
void UniValue::setFloat(double val_)
76
0
{
77
0
    std::ostringstream oss;
78
79
0
    oss << std::setprecision(16) << val_;
80
81
0
    return setNumStr(oss.str());
82
0
}
83
84
void UniValue::setStr(std::string str)
85
0
{
86
0
    clear();
87
0
    typ = VSTR;
88
0
    val = std::move(str);
89
0
}
90
91
void UniValue::setArray()
92
0
{
93
0
    clear();
94
0
    typ = VARR;
95
0
}
96
97
void UniValue::setObject()
98
0
{
99
0
    clear();
100
0
    typ = VOBJ;
101
0
}
102
103
void UniValue::push_back(UniValue val)
104
0
{
105
0
    checkType(VARR);
106
107
0
    values.push_back(std::move(val));
108
0
}
109
110
void UniValue::push_backV(const std::vector<UniValue>& vec)
111
0
{
112
0
    checkType(VARR);
113
114
0
    values.insert(values.end(), vec.begin(), vec.end());
115
0
}
116
117
void UniValue::pushKVEnd(std::string key, UniValue val)
118
0
{
119
0
    checkType(VOBJ);
120
121
0
    keys.push_back(std::move(key));
122
0
    values.push_back(std::move(val));
123
0
}
124
125
void UniValue::pushKV(std::string key, UniValue val)
126
0
{
127
0
    checkType(VOBJ);
128
129
0
    size_t idx;
130
0
    if (findKey(key, idx))
131
0
        values[idx] = std::move(val);
132
0
    else
133
0
        pushKVEnd(std::move(key), std::move(val));
134
0
}
135
136
void UniValue::pushKVs(UniValue obj)
137
0
{
138
0
    checkType(VOBJ);
139
0
    obj.checkType(VOBJ);
140
141
0
    for (size_t i = 0; i < obj.keys.size(); i++)
142
0
        pushKVEnd(std::move(obj.keys.at(i)), std::move(obj.values.at(i)));
143
0
}
144
145
void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
146
0
{
147
0
    if (typ != VOBJ)
148
0
        return;
149
150
0
    kv.clear();
151
0
    for (size_t i = 0; i < keys.size(); i++)
152
0
        kv[keys[i]] = values[i];
153
0
}
154
155
bool UniValue::findKey(const std::string& key, size_t& retIdx) const
156
0
{
157
0
    for (size_t i = 0; i < keys.size(); i++) {
158
0
        if (keys[i] == key) {
159
0
            retIdx = i;
160
0
            return true;
161
0
        }
162
0
    }
163
164
0
    return false;
165
0
}
166
167
bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
168
0
{
169
0
    if (typ != VOBJ) {
170
0
        return false;
171
0
    }
172
173
0
    for (const auto& object: t) {
174
0
        size_t idx = 0;
175
0
        if (!findKey(object.first, idx)) {
176
0
            return false;
177
0
        }
178
179
0
        if (values.at(idx).getType() != object.second) {
180
0
            return false;
181
0
        }
182
0
    }
183
184
0
    return true;
185
0
}
186
187
const UniValue& UniValue::operator[](const std::string& key) const
188
0
{
189
0
    if (typ != VOBJ)
190
0
        return NullUniValue;
191
192
0
    size_t index = 0;
193
0
    if (!findKey(key, index))
194
0
        return NullUniValue;
195
196
0
    return values.at(index);
197
0
}
198
199
const UniValue& UniValue::operator[](size_t index) const
200
0
{
201
0
    if (typ != VOBJ && typ != VARR)
202
0
        return NullUniValue;
203
0
    if (index >= values.size())
204
0
        return NullUniValue;
205
206
0
    return values.at(index);
207
0
}
208
209
void UniValue::checkType(const VType& expected) const
210
0
{
211
0
    if (typ != expected) {
212
0
        throw type_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
213
0
                                 std::string{uvTypeName(expected)}};
214
0
    }
215
0
}
216
217
const char *uvTypeName(UniValue::VType t)
218
0
{
219
0
    switch (t) {
220
0
    case UniValue::VNULL: return "null";
221
0
    case UniValue::VBOOL: return "bool";
222
0
    case UniValue::VOBJ: return "object";
223
0
    case UniValue::VARR: return "array";
224
0
    case UniValue::VSTR: return "string";
225
0
    case UniValue::VNUM: return "number";
226
0
    }
227
228
    // not reached
229
0
    return nullptr;
230
0
}
231
232
const UniValue& UniValue::find_value(std::string_view key) const
233
0
{
234
0
    for (unsigned int i = 0; i < keys.size(); ++i) {
235
0
        if (keys[i] == key) {
236
0
            return values.at(i);
237
0
        }
238
0
    }
239
0
    return NullUniValue;
240
0
}
241
242
void UniValue::reserve(size_t new_cap)
243
0
{
244
0
    values.reserve(new_cap);
245
0
    if (typ == VOBJ) {
246
0
        keys.reserve(new_cap);
247
0
    }
248
0
}